【发布时间】:2018-11-19 07:36:43
【问题描述】:
我在我的应用程序中使用 iOS Mapbox SDK。我将注释的图像更改为自定义图像(它看起来像地图标记)。当我将注释添加到地图视图上的特定坐标时,它将被添加,但我的自定义注释图像(标记)的中心将设置在坐标上。我需要更改标记位置以在坐标上设置标记的底部。我找到了一种方法,但我不知道有没有更好的方法? 我将坐标转换为一个点,然后改变了点的y位置,然后将点转换为一个新的坐标。
func mapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? {
let reuseIdentifier = "annotationImage"
var annotationImage = mapView.dequeueReusableAnnotationImage(withIdentifier: reuseIdentifier)
if annotationImage == nil {
annotationImage = MGLAnnotationImage(image: UIImage(named: "Orange")!, reuseIdentifier: reuseIdentifier)
}
return annotationImage
}
func addDestinationMarker(coordinate: CLLocationCoordinate2D) {
guard let mapView = mapView else { return }
if let annotations = mapView.annotations {
mapView.removeAnnotations(annotations)
}
var point = mapView.convert(coordinate, toPointTo: mapView)
point.y -= markerImageView.frame.height / 2
let newCoordinate = mapView.convert(point, toCoordinateFrom: mapView)
let annotation = MGLPointAnnotation()
annotation.coordinate = newCoordinate
mapView.addAnnotation(annotation)
}
【问题讨论】: