【问题标题】:How to overlay an image on a IOS map using swift如何使用 swift 在 IOS 地图上叠加图像
【发布时间】:2014-10-15 14:11:59
【问题描述】:

我正在尝试了解如何使用 SWIFT 在 IOS 地图上叠加图像。我创建了以下代码,该代码使用地图套件在地图上覆盖了一个绿色圆圈。我想用矩形图像 tOver.png 500,500 替换绿色圆圈到目前为止,我找不到一个快速的例子或好的资源。

//
//  ViewController.swift
//  mapoverlaytest
//

import UIKit
import MapKit


class ViewController: UIViewController,MKMapViewDelegate {
    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.mapView.delegate = self;
        let location = CLLocationCoordinate2D(
            latitude: 51.50007773,
            longitude: -0.1246402
        )

        let span = MKCoordinateSpanMake(0.05, 0.05)
        let region = MKCoordinateRegion(center: location, span: span)

        mapView.setRegion(region, animated: true)

        let annotation = MKPointAnnotation()
        annotation.setCoordinate(location)
        annotation.title = "Big Ben"
        annotation.subtitle = "London"



        var overlay = MKCircle (centerCoordinate: location, radius: 500)

        mapView.addOverlay(overlay)

        mapView.addAnnotation(annotation)

    }

    func mapView(
        mapView: MKMapView!, rendererForOverlay
        overlay: MKOverlay!) -> MKOverlayRenderer! {
            if (overlay.isKindOfClass(MKCircle))
            {
                var circleRenderer = MKCircleRenderer(overlay: overlay)
                circleRenderer.strokeColor = UIColor.greenColor()
                circleRenderer.fillColor = UIColor(
                    red: 0,
                    green: 1.0,
                    blue: 0,
                    alpha: 0.5)

                return circleRenderer
            }
            return nil
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

【问题讨论】:

    标签: ios swift mapkit mkoverlay


    【解决方案1】:

    正如 Totem 所解释的,如果适合您的目的,使用图像注释而不是叠加层会更简单。但是,根据您要使用此图像的目的,它可能无法正常工作。地图叠加层和地图注释之间的主要区别在于,当您缩放地图(如大头针)时,注释的大小保持不变,而叠加层会随着地图的大小而变化(如标记建筑物)。如果您希望图像随地图一起缩放,那就有点复杂了。

    您将需要创建一个新的 MKOverlayRenderer 子类来绘制您的图像。您必须通过子类化 drawMapRect(mapRect, zoomScale, inContext) 函数自己将图像绘制到图像上下文中。制作完这个子类后,你可以用你的自定义子类代替 MKCircleRenderer,你应该很高兴。

    Raywenderlich.com 上有一篇很好的文章,你一定要看看。它应该会引导您了解您需要了解的所有内容。

    【讨论】:

      【解决方案2】:

      你应该实现而不是rendererForOverlay

      func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!
      

      在那里,构建您的 MKAnnotationView 并在返回之前设置其图像属性。查看https://developer.apple.com/LIBRARY/ios/documentation/MapKit/Reference/MKAnnotationView_Class/index.html 了解有关 MKAnnotationView 类的更多信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-28
        • 2018-10-16
        • 2020-10-14
        相关资源
        最近更新 更多