开发者

Mapkit - Change annotation image on tapping

开发者 https://www.devze.com 2023-03-31 05:24 出处:网络
I am new to iphone development. I have added annotations on the map. I am able to catch the tapping event on the annotation in didSelectAnnotationView(). But i want to chang开发者_高级运维e the image

I am new to iphone development. I have added annotations on the map. I am able to catch the tapping event on the annotation in didSelectAnnotationView(). But i want to chang开发者_高级运维e the image of the annotation when the user taps on it.


Set the image property, as such.

annView.image = [UIImage imageNamed:@"AnnotationIcon.png"];

EDIT

So, you seems to be using MKPinAnnotationView which has a pinColor property.

Therefore, you can change it as such

pin.pinColor = MKPinAnnotationColorRed; // green and purple are 2 other colors.


Swift 3: I'll assume you are using this function already for custom MKAnnotation views

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {...}

and I'll update @djibouti33 answer from Objective-c to Swift 3:

    func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
        view.image = UIImage(named: "unselected_marker")
        view.frame = CGRect(x: 0, y: 0, width: 40, height: 40)//keep the new icon size resobable.
     }

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        view.image = UIImage(named: "selected_marker")
        view.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
    }


If you're using an MKAnnotationView and not MKPinAnnotationView you can just rely on MKMapViewDelegate methods:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    view.image = [UIImage imageNamed:@"selected_image"];
}

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
    view.image = [UIImage imageNamed:@"deselected_image"];
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消