iOS Dev:在MapBox中为用户注释使用自定义图像 [英] iOS Dev: Using a custom image for the user annotation in MapBox
问题描述
我对IOS开发非常陌生,因此其中一些似乎很明显.我尝试结合自定义用户注释的示例和在地图上标记图像的地方
I'm very new to IOS development so some of this might seem obvious. I've tried combining the examples for customize the user annotation and marking a place on the map with an image
我觉得我需要添加以下代码行,并以某种方式将此代码附加到第一个链接中描述的用户注释中,但是我不知道如何执行此操作.我猜想我也可以将其中一些功能插入customUserLocationAnnotationView中,但是没有明显的迹象表明将其放置在该类中的位置.
I feel like I need to add the following lines of code and somehow attach this code to the user annotation described in the first link, but I have no idea how to do this. I'm guessing I could also insert some of these functions into the customUserLocationAnnotationView, but there is no obvious indicator of where to place this within that class.
func mapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? {
var annotationImage = mapView.dequeueReusableAnnotationImage(withIdentifier: "pisa")
if annotationImage == nil {
var image = UIImage(named: "pisavector")!
image = image.withAlignmentRectInsets(UIEdgeInsets(top: 0, left: 0, bottom: image.size.height/2, right: 0))
annotationImage = MGLAnnotationImage(image: image, reuseIdentifier: "pisa")
}
return annotationImage
}
func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
return true
}
编辑
我不只是想将图像放在这样的随机位置
I don't just want to put an image in a random location like this
我希望图像以用户注释为中心,并且当用户移动时,图像也会移动,就像下面的图像
I want the image to be centered on the user annotation, and when the user moves, the image will also move, like the image below
作为旁注
我还收到错误无法渲染和更新ViewController的自动自动布局状态(BYZ-38-tOr):代理崩溃了Main.storyboard",但我认为这并不重要,因为我的程序仍然可以在模拟器上正常构建并运行.
I'm also getting the error 'Failed to render and update auto auto layout status for ViewController (BYZ-38-tOr): The agent crashed Main.storyboard' but I don't think that's important, because my program still builds and runs on the simulator fine.
推荐答案
override func viewDidLoad() {
super.viewDidLoad()
let mapView = MGLMapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.delegate = self
mapView.userTrackingMode = .followWithHeading
mapView.showsUserHeadingIndicator = true
view.addSubview(mapView)
}
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
// Substitute our custom view for the user location annotation. This custom view is defined below.
if annotation is MGLUserLocation && mapView.userLocation != nil {
return CustomUserLocationAnnotationView()
}
return nil
}
// Create a subclass of MGLUserLocationAnnotationView.
class CustomUserLocationAnnotationView: MGLUserLocationAnnotationView {
...
}
请看以下示例: https://www.mapbox.com/ios-sdk/maps/examples/user-location-annotation/
CustomUserLocationAnnotationView中有一个名为setupLayers的方法.可变点是CALayer,因此您可以将UIImage添加到CALayer.如下更改私有函数setupLayers()中的代码:
There is a method called setupLayers in CustomUserLocationAnnotationView. variable dot is a CALayer, so you can add a UIImage to a CALayer. Change the code in private func setupLayers() like below:
dot = CALayer()
let myImage = UIImage(named: "star")?.cgImage
dot.contents = myImage
layer.addSublayer(dot)
这篇关于iOS Dev:在MapBox中为用户注释使用自定义图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!