如何使用swift在IOS地图上覆盖图像
我试图找出如何使用SWIFT在IOS地图上覆盖图像。我已经创建了下面的代码,它使用MapKit在地图上覆盖了一个绿色的圆圈。我想用矩形图像替换绿色圆圈tOver.png 500500我对iOS开发和swift都是新手。到目前为止,我找不到一个快速的例子或好的资源如何使用swift在IOS地图上覆盖图像,ios,swift,mapkit,mkoverlay,Ios,Swift,Mapkit,Mkoverlay,我试图找出如何使用SWIFT在IOS地图上覆盖图像。我已经创建了下面的代码,它使用MapKit在地图上覆盖了一个绿色的圆圈。我想用矩形图像替换绿色圆圈tOver.png 500500我对iOS开发和swift都是新手。到目前为止,我找不到一个快速的例子或好的资源 // // ViewController.swift // mapoverlaytest // import UIKit import MapKit class ViewController: UIViewController
//
// 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.
}
}
您应该实现
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!
在那里,构建MKAnnotationView并在返回它之前设置它的image属性。查看有关MKAnnotationView类的更多信息。正如图腾所解释的,如果图像注释适合您的目的,那么使用图像注释而不是覆盖将更简单。但是,它可能无法工作,具体取决于您希望使用此图像的目的。地图覆盖和地图注释之间的主要区别在于,缩放地图时,注释的大小保持不变(如图钉),覆盖随地图的大小而变化(如标记建筑)。如果你想让你的图像和地图一起缩放,它会变得更复杂一些 您将需要创建一个新的mkoverlyrenderer子类来绘制图像。您必须通过子类化drawMapRect(mapRect、zoomScale、inContext)函数将图像绘制到图像上下文中。在创建这个子类之后,您可以在自定义子类中替换mkcirclerender,这样您就可以开始了 有一篇很好的评论,你一定要去看看。它应该引导你了解你需要知道的一切