ios 如何将 MKMapView 的用户位置蓝点更改为选择的图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6293432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change MKMapView's user-location blue dot to an image of choice?
提问by LouwHopley
回答by EmptyStack
In the viewForAnnotation:method of MKMapViewDelegateprobably you would be having the code like this.
在MKMapViewDelegate的viewForAnnotation:方法中,您可能会有这样的代码。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if (annotation == mapView.userLocation) return nil;
...
We return nilif the annotationis userLocationto let the mapViewdisplay the blue dot & circle animation. In order to show our custom annotation for userLocationjust remove the line return nil;
and do your customization there.
如果注解为userLocation,我们返回nil以让mapView显示蓝点和圆圈动画。为了显示我们对userLocation的自定义注释,只需删除该行并在那里进行自定义。return nil;
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString* AnnotationIdentifier = @"Annotation";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinView) {
MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
if (annotation == mapView.userLocation){
customPinView.image = [UIImage imageNamed:@"myCarImage.png"];
}
else{
customPinView.image = [UIImage imageNamed:@"mySomeOtherImage.png"];
}
customPinView.animatesDrop = NO;
customPinView.canShowCallout = YES;
return customPinView;
} else {
pinView.annotation = annotation;
}
return pinView;
}
回答by Aggressor
Here is Swift 2.0 version in which you might have multiple pins.
这是 Swift 2.0 版本,其中您可能有多个引脚。
In this code CustomAnnotation is just an MKAnnotation subclass. Basically if the annotation is not the kind of one of your custom classes, then its the user location pin.
在这段代码中 CustomAnnotation 只是一个 MKAnnotation 子类。基本上,如果注释不是您的自定义类之一,则它是用户位置引脚。
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
{
// This is false if its a user pin
if(annotation.isKindOfClass(CustomAnnotation) == false)
{
let userPin = "userLocation"
if let dequeuedView = _view.mapView().dequeueReusableAnnotationViewWithIdentifier(userPin)
{
return dequeuedView
} else
{
let mkAnnotationView:MKAnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: userPin)
mkAnnotationView.image = UIImage(named: C_GPS.ROUTE_WALK_ICON_NAME)
let offset:CGPoint = CGPoint(x: 0, y: -mkAnnotationView.image!.size.height / 2)
mkAnnotationView.centerOffset = offset
return mkAnnotationView
}
}
let annotation = annotation as? CustomAnnotation
if(annotation == nil)
{
return nil
}
let endPointsIdentifier = "endPoint"
if let dequeuedView = _view.mapView().dequeueReusableAnnotationViewWithIdentifier(endPointsIdentifier)
{
dequeuedView.image = annotation!.uiimage
return dequeuedView
} else
{
let mkAnnotationView:MKAnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: endPointsIdentifier)
mkAnnotationView.image = annotation!.uiimage
let offset:CGPoint = CGPoint(x: 0, y: -mkAnnotationView.image!.size.height / 2)
mkAnnotationView.centerOffset = offset
let gesture = UITapGestureRecognizer(target: self, action: "routeTouched:")
mkAnnotationView.addGestureRecognizer(gesture)
return mkAnnotationView
}
}
回答by mechdon
Ok, here is the Swift version:
好的,这是 Swift 版本:
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
let identifier = "User"
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil{
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView.canShowCallout = true
} else {
annotationView.annotation = annotation
}
annotationView.image = UIImage(named: "image")
return annotationView
}
回答by anthony baradhy
is this to change the current location blue dot???
这是要更改当前位置的蓝点吗???
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) ->
MKAnnotationView! {
let identifier = "User"
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil{
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView.canShowCallout = true
} else {
annotationView.annotation = annotation
}
annotationView.image = UIImage(named: "image")
return annotationView
}
回答by Museer Ahamad Ansari
Please try this something like this. its working for me in Xcode 7 and swift 2.
请尝试这样的事情。它在 Xcode 7 和 swift 2 中为我工作。
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
// want to show a custom image if the annotation is the user's location.
guard !annotation.isKindOfClass(MKUserLocation) else {
let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "userLocation")
annotationView.image = UIImage(named: "icon_coordinates_self")
return annotationView
//return nil
}
// for other annotation except current location
let annotationIdentifier = "AnnotationIdentifier"
var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
else {
let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
annotationView = av
}
if let annotationView = annotationView {
// Configure your annotation view here
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "Annotation_map")
}
return annotationView
}