Custom marker image on swift (MapKit)
我在 MapKit 上使用自定义标记
如何更改自定义标记上的图像宽度和高度?
我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { if (annotation is MKUserLocation) { return nil } let reuseId ="test" var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) if anView == nil { anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) anView.image = UIImage(named:"x2.png") anView.canShowCallout = true } else { anView.annotation = annotation } return anView } |
尝试使用
1 | anView.frame.size = CGSize(width: 30.0, height: 30.0) |
1 2 3 | var currentLocationAppleMarker : MKAnnotationView? self.currentLocationAppleMarker?.image = //Image for Current Location Pin |
MapKit ViewForAnnotation 委托应该像,
1 2 3 4 5 6 7 8 9 10 11 | func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { let pin = mapView.view(for: annotation) ?? MKAnnotationView(annotation: annotation, reuseIdentifier: nil) if annotation is MKUserLocation { pin.image = //Image for Current Location Pin return pin } else { pin.image = //Image for Other Pin return pin } } |