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

标签 ios swift google-maps mapkit

我想在 map View 中添加多个标记。每个标记下面都有一个文本标签,即它应该始终可见。另外我想添加我自己的图像作为它的图标。

在此附上我想要的截图。

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

最佳答案

我找到了解决方案,因为我没有时间尝试使用 Apple Map,但您也可以尝试使用 google map。

步骤

  1. 获取要显示注释的位置。
  2. 在 map 上添加此点注释。
  3. 创建一个包含您想要的文本的 UILabel(说,lbl)。
  4. 在 View 上添加此文本(例如,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
      }


}

输出:

enter image description here

Edit : image trasparency

在下面使用这个函数

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

函数调用

将上层注释 View 的代码替换为下面

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

关于ios - 在标记 swift 下方添加永久标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47951709/

相关文章:

ios - 拼贴应用程序的自定义 UIView

swift - 我的 CompletionHandler 将不会执行。为什么?

swift - 如何在 xcode 中将视觉效果 View 移到后面,以便我可以在 Storyboard 中看到我的 UI?

javascript - 谷歌地图 API : error thrown when setting LatLng values

ios - MapKit 中的 MapTypeStyle

javascript - 执行脚本Webview JavaFx

ios - GPUImage + Touch 模糊效果

ios - GLSL 程序在某些 iOS 硬件上间歇性失败

objective-c - AVCaptureVideoPreviewLayer 在简历上不显示任何内容

ios - 从 NSManaged 对象中过滤掉整数