Ios 如何在Swift 2中创建图像覆盖并添加到MKMapView?
我正在学习MapKit,现在正在尝试将图像(不是多边形、圆形、矩形等)作为覆盖添加到地图视图中。我似乎找不到任何示例代码来帮助解释如何做到这一点 到目前为止,我在ViewController.swift中的代码是:Ios 如何在Swift 2中创建图像覆盖并添加到MKMapView?,ios,swift,image,mapkit,mkoverlay,Ios,Swift,Image,Mapkit,Mkoverlay,我正在学习MapKit,现在正在尝试将图像(不是多边形、圆形、矩形等)作为覆盖添加到地图视图中。我似乎找不到任何示例代码来帮助解释如何做到这一点 到目前为止,我在ViewController.swift中的代码是: import UIKit import MapKit import CoreLocation class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { @I
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet var mapView: MKMapView!
var locationManager: CLLocationManager!
var mapOverlay: MKOverlay!
override func viewDidLoad() {
super.viewDidLoad()
//Setup our Location Manager
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
//Setup our Map View
mapView.delegate = self
mapView.mapType = MKMapType.Satellite
mapView.showsUserLocation = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我知道我需要使用:
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer
还有drawMapRect函数,但我不知道该放什么进去。此外,如果所有代码都在ViewController.swift中,那么它也会很有用
我只需要一个覆盖,所以我不需要一个类。我需要它是一个图像覆盖(以便它可以调整大小),而不是一个注释。我也试着在上学习教程,但是没有一个章节包含所有的代码,我发现很难学习。你们能帮助我如何创建图像MKOverlays并将其添加到MKMapKit吗
提前感谢…苹果确实有一些,但它是Objective-C格式的,所以您必须将其转换为Swift
基本上,您需要做两件事(这两件事都可以在视图控制器中完成)。第一种方法是创建覆盖并将其添加到地图中,即viewdiload()中的某个位置。
:
使用Swift 2.1/Xcode 7.2,这为我在澳大利亚的某个位置渲染了一个磁贴。因此,您可能需要调整lat/LON,以便在地图中为您提供覆盖-地图默认为您的大陆。Swift 3 以下是我在Swift 3中的作品
class ImageOverlay : NSObject, MKOverlay {
let image:UIImage
let boundingMapRect: MKMapRect
let coordinate:CLLocationCoordinate2D
init(image: UIImage, rect: MKMapRect) {
self.image = image
self.boundingMapRect = rect
self.coordinate = rect.centerCoordinate
}
}
class ImageOverlayRenderer : MKOverlayRenderer {
override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {
guard let overlay = self.overlay as? ImageOverlay else {
return
}
let rect = self.rect(for: overlay.boundingMapRect)
UIGraphicsPushContext(context)
overlay.image.draw(in: rect)
UIGraphicsPopContext()
}
}
然后像往常一样,在MapView代理中:
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is ImageOverlay {
return ImageOverlayRenderer(overlay: overlay)
}
...
}
每当您需要向地图添加图像时:
let overlay = ImageOverlay(image: myImage, rect: desiredLocationAsMapRect)
mapView.add(overlay)
注意,我始终确保rect与图像具有相同的纵横比。否则,我怀疑会导致图像失真。OK,但如何设置图像?polygonRenderer.fillColor=UIColor(patternImage:UIImage(名为:“mapOverlay”)!)如何确保矩形的大小适合于图像------我的代码------let heightInPoints=image.size.height let heightInPixels=heightInPoints*image.scale let widthinpixes=image.size.width let widthInPixels=widthInPoints*image.scale let mapsize=mkMapSize(宽度:双倍(宽度像素),高度:双倍(高度:双倍(高度像素))让mkMapPoint=MKMapPointForCoordinate(locationCoordinates)让mkMapRect=mkMapRect(origin:mkMapPoint,size:mkMapSize)---但不会出现覆盖图我编辑了我的答案以澄清关于rect大小的注意事项,我的意思是,如果rect的纵横比与图像相同,效果最好。我很难读懂你评论中的代码,但在我看来,你是在直接使用图像大小作为地图大小,这不是你想要的。map rect应该由您希望渲染图像的地图上的地理坐标确定。@biomiker似乎认为此解决方案强制图像与map rect的精确大小相同。我们如何设置图像大小?@grantespo我不确定我是否理解你的问题。map rect将确定地图上覆盖的大小。图像将被渲染到map rect并进行适当缩放。你想改变什么?
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is ImageOverlay {
return ImageOverlayRenderer(overlay: overlay)
}
...
}
let overlay = ImageOverlay(image: myImage, rect: desiredLocationAsMapRect)
mapView.add(overlay)