Identify MKPointAnnotation in mapView

北城余情 提交于 2019-12-04 10:57:35

I think that is easy and im doing this in an app my.

I customize a class for Annotation from MKAnnotation:

import MapKit
import AddressBook

class MyAnnotation: NSObject, MKAnnotation
{
    let identifier : String
    let title: String
    let subtitle: String
    let coordinate: CLLocationCoordinate2D
    let color: MKPinAnnotationColor


    init(identifier: String, title: String, subtitle: String, coordinate: CLLocationCoordinate2D, color: MKPinAnnotationColor)
    {
        self.identifier = identifier
        self.title = title
        self.subtitle = subtitle
        self.coordinate = coordinate
        self.color = color

        super.init()
    }

    func mapItem() -> MKMapItem
    {
        let addressDictionary = [String(kABPersonAddressStreetKey): subtitle]
        let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: addressDictionary)

        let mapItem = MKMapItem(placemark: placemark)
        mapItem.name = title

        return mapItem
    }
}

Now you can use the field identifier from class MyAnnotation in your points:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!
{
    if let annotation = annotation as? MyAnnotation
    {
        let identifier = annotation.identifier
        var view: MKPinAnnotationView

        if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) as? MKPinAnnotationView
        {
            view = dequeuedView
            view.annotation = annotation
        }
        else
        {
            view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            view.canShowCallout = true
            view.calloutOffset = CGPoint(x: -5, y: 5)
            view.animatesDrop = true
            view.leftCalloutAccessoryView = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as! UIView
            view.rightCalloutAccessoryView = UIButton.buttonWithType(UIButtonType.ContactAdd) as! UIView
            view.pinColor = annotation.color
            //view.image
            //MKAnnotationView 32 x 29
        }
        return view
    }
    return nil
}

If you don't understand you can red this excelente article:

http://www.raywenderlich.com/90971/introduction-mapkit-swift-tutorial

The problem is that you are using a plain vanilla built-in MKPointAnnotation. It has no tag property. You need to define your own MKAnnotation class (i.e. a class that adopts the MKAnnotation protocol). That way, it can have any properties you like.

My very easy solution on Objective-C

.h file:

#import <MapKit/MapKit.h>

@interface mapMKPointAnnotation : MKPointAnnotation

@property (nonatomic) NSInteger tag;

@end

and create object with tag:

mapMKPointAnnotation *annotation = [[mapMKPointAnnotation alloc] init];
[annotation setTag:1];

next get tag in method:

 -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

static NSString *SFAnnotationIdentifier = @"SFAnnotationIdentifier";
    mapMKPointAnnotation *mMKPAnn = (mapMKPointAnnotation *) annotation;
    NSString *img = [mMKPAnn tag] == 0 ? @"map_icon" : @"map_icon_active";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!