【问题标题】:add a permanent label below a marker swift快速在标记下方添加永久标签
【发布时间】:2017-12-23 10:29:29
【问题描述】:

我想在地图视图中添加多个标记。每个标记始终在其下方有一个文本标签,即它应该始终可见。我还想添加我自己的图片作为它的图标。

在此附上我想要的截图。

代码工作

  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】:

我找到了解决方案,因为时间不够,我尝试了 Apple Map,但您也尝试了 google 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
      }


}

输出:

编辑:图像透明度

在函数下面使用这个

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()
}

函数调用

将上面的annotationview代码替换为下面

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

输出:

【讨论】:

  • 我已经尝试了谷歌地图的代码。请调查一下,因为我没有得到想要的结果。我在问题中附上了我的代码。
  • @PoojaMishra 你还没有像我说的那样做,这是 MKAnnotaionView,你问如何在 cmets 中处理它。您的代码是在 2 天前更新的。
  • 请再次检查。我已经添加了更新的代码
  • @PoojaMishra 你忘记了第 3 步和第 4 步。重新检查我的做法并进行比较。
  • 谢谢你...帮助
猜你喜欢
  • 2015-11-10
  • 1970-01-01
  • 1970-01-01
  • 2011-08-01
  • 2020-07-30
  • 2020-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多