在标记swift下方添加永久标签

问题描述 投票:0回答:1

我想在地图视图中添加多个标记。每个标记下面都有一个文本标签,总是应该是可见的。另外,我想添加自己的图像作为其图标。

在这里,我附上了我想要的截图。

enter image description here

代码工作

  func addGroundOverlay(position: CLLocationCoordinate2D, veh_num: String) {

    let overlay = GMSGroundOverlay(position: position, icon: newImage(text: veh_num, size: CGSize(width: 150.0, height: 150.0)), zoomLevel: 10)
    overlay.bearing = 0
    overlay.map = (self.view as! GMSMapView)
}

func newImage(text: String, size: CGSize) -> UIImage {

    let data = text.data(using: String.Encoding.utf8, allowLossyConversion: true)
    let drawText = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)

    let textFontAttributes = [
        NSAttributedStringKey.font: UIFont(name: "Helvetica Bold", size: 10)!,
        NSAttributedStringKey.foregroundColor: UIColor.red,
        ]

    UIGraphicsBeginImageContextWithOptions(size, false, 0)
    drawText?.draw(in: CGRect(x: 0,y: 0, width: size.width, height: size.height), withAttributes: textFontAttributes)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage!
}

Image for what I have tried

result image

ios swift google-maps mapkit
1个回答
1
投票

我找到了解决方案,因为我为Apple Map尝试的时间不多,但你也尝试谷歌地图。

脚步

  1. 获取要显示注释的位置。
  2. 在地图上添加此点注释。
  3. 根据需要创建一个带有文本的UILabel(说,lbl)。
  4. 在视图上添加此文本(说,viewAn)。
  5. 现在捕捉viewAn并使其成像。
  6. 将此图像用于位置标记。

下面是Apple Map的代码工作,在它下面添加了模拟器,它正常工作。按照上面的步骤,确定它也适用于谷歌地图。

代码工作

    import UIKit
    import MapKit

    class MapVC: UIViewController, MKMapViewDelegate {


    @IBOutlet weak var mapView: MKMapView! // Apple mapview Outlet

    var location: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 28.5961279, longitude: 77.1587375)

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let anno = MKPointAnnotation();
        anno.coordinate = location;
        mapView.addAnnotation(anno);
    }
    // To capture view
    func captureScreen(_ viewcapture : UIView) -> UIImage {

        UIGraphicsBeginImageContextWithOptions(viewcapture.frame.size, viewcapture.isOpaque, 0.0)
        viewcapture.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!;
    }

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        // Don't want to show a custom image if the annotation is the user's location.
        guard !(annotation is MKUserLocation) else {
            return nil
        }

        // Better to make this class property
        let annotationIdentifier = "AnnotationIdentifier"

        var annotationView: MKAnnotationView?
        if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
            annotationView = dequeuedAnnotationView
            annotationView?.annotation = annotation
        }
        else {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
            annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
        }

        if let annotationView = annotationView {
            // Configure your annotation view here
            // view for annotation
            let viewAn = UIView()
            viewAn.frame = CGRect(x: 0, y: 0, width: 80, height: 18)
            // label as required
            let lbl = UILabel()
            lbl.text = "ABC 123"
            lbl.textColor = UIColor.black
            lbl.backgroundColor = UIColor.cyan
            // add label to viewAn
            lbl.frame = viewAn.bounds
            viewAn.addSubview(lbl)
            // capture viewAn
            let img = self.captureScreen(viewAn)

            annotationView.canShowCallout = true
            // set marker
            annotationView.image = img
        }

        return annotationView
      }


}

OutPut:

enter image description here

编辑:图像透明度

使用下面的函数

func changeWhiteColorTransparent(_ image: UIImage) -> UIImage {
    let rawImageRef = image.cgImage as! CGImage
    let colorMasking : [CGFloat] = [222, 255, 222, 255, 222, 255]
    UIGraphicsBeginImageContext(image.size)
    let maskedImageRef: CGImage = rawImageRef.copy(maskingColorComponents: colorMasking)!
    do {
        //if in iphone
        UIGraphicsGetCurrentContext()?.translateBy(x: 0.0, y: image.size.height)
        UIGraphicsGetCurrentContext()?.scaleBy(x: 1.0, y: -1.0)
    }

    UIGraphicsGetCurrentContext()?.draw(maskedImageRef, in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))

   let result = UIGraphicsGetImageFromCurrentImageContext() as! UIImage


    UIGraphicsEndImageContext()
    return result ?? UIImage()
}

Func callie

用下面替换上部注释视图的代码

let viewAn = UIView()
        viewAn.frame = CGRect(x: 0, y: 0, width: 80, height: 18)
        let lbl = UILabel()
        lbl.text = "ABC 123"
        lbl.textColor = UIColor.black
        lbl.backgroundColor = UIColor.clear
        viewAn.backgroundColor = UIColor.white
        lbl.frame = viewAn.bounds
        viewAn.addSubview(lbl)
        let img = self.captureScreen(viewAn)

        let aImgNew = self.changeWhiteColorTransparent(img)

        annotationView.backgroundColor = UIColor.clear
        annotationView.canShowCallout = true
        annotationView.image = aImgNew

输出:

enter image description here

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.