我正在尝试找出如何使用SWIFT在IOS映射上覆盖镜像。我使用地图工具包创建了以下代码,在地图上覆盖一个绿色圆圈。我想将绿色的圆圈替换为矩形图像tOver.png 500,500我对iOS开发和swift来说都是新手。到目前为止,我找不到一个快速的例子或好的资源。
//
// 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.
}
}
发布于 2015-02-19 07:35:56
正如Totem所解释的那样,使用图像注释而不是覆盖会更简单,如果这对您的目的有效的话。然而,它可能不起作用,这取决于您想要使用此图像的用途。地图覆盖和地图注解之间的主要区别是,当您缩放地图时,注解的大小保持不变(像大头针一样),而覆盖会随着地图的大小而变化(就像标记建筑物一样)。如果你想让你的图像和地图一起缩放,那就有点复杂了。
您将需要创建一个新的MKOverlayRenderer子类来绘制图像。您必须自己通过继承drawMapRect(mapRect,zoomScale,inContext)函数将图像绘制到图像上下文中。在创建这个子类之后,您只需替换自定义子类中的MKCircleRenderer,就可以了。
在Raywenderlich.com上有一篇非常好的文章,你一定要去看看。它将引导您了解您需要了解的所有内容。
https://stackoverflow.com/questions/26384748
复制相似问题