Identify MKPointAnnotation in mapView
Asked Answered
P

3

6

I have at least 100 diferents Points ... how can associate each point with the position in my 'listOfPoints' assigning a tag in the position associated in viewForAnnotation .

Here i add my Points, some events will have the same title.

var listOfPoints : Array<Events> = [] // List Of all events

//add points to map
    for (index, mPoints) in enumerate(listOfPoints) {

        var point: MKPointAnnotation! = MKPointAnnotation()
        var location = CLLocationCoordinate2D(latitude: mPoints.latitude, longitude: mPoints.longitude)
        point.coordinate = location
        point.title = mPoints.name
        point.subtitle = mPoints.address
        self.mapView.addAnnotation(point)
    }

//Draw custom pin in the map

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    print("ffff");

    var identifier = "CustomAnnotation"


    if annotation.isKindOfClass(MKPointAnnotation) {
        var pin = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

        if pin == nil {
            pin = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            pin.tag = tagPosition; // here
            pin.image = UIImage(named: "mapa_pin")
            pin.centerOffset = CGPointMake(0, -10)
            pin.canShowCallout = true

            var pointTitle = pin!.annotation.title! as String


            // Callout
            var button = UIButton.buttonWithType(.DetailDisclosure) as UIButton
            pin!.leftCalloutAccessoryView = button

            var image = UIImageView(image: UIImage(named: "mapa_pin"))
            pin!.rightCalloutAccessoryView = image


        } else {
            pin!.annotation = annotation
        }

        return pin
    }

    return nil

}

// Print the position 
    func  mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
        println(view.tag);
    }

How can associated this tag with the position on my 'listOfPoints'

pin.tag = tagPosition; 

or is there another way?

Popish answered 27/3, 2015 at 17:55 Comment(4)
If you just need tag, then in your "viewForAnnotation" API, find the index of "annotation" in "listOfPoints" and set it.Grumous
@gagarwal, That would not work because the code creates annotations of type MKPointAnnotation from the objects in listOfPoints which are of type Events (they are completely separate objects). The best solution is to use a custom class like matt's answer says.Tacet
I agree with matt's answer though you might consider making your Events object itself implement the MKAnnotation protocol. That way, your annotation objects will already be Events so you will not need any linking properties or lookups afterwards.Tacet
@GilbertoIbarra: Do not confuse the annotation model objects (like MKPointAnnotation) with their views (like MKAnnotationView). They are completely different classes and completely different things. The answer is talking about the model objects -- not the views.Tacet
M
7

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

Matriculate answered 6/9, 2015 at 22:39 Comment(1)
This needs an update for Swift 3. If I get it to work I'll post it.Loath
U
4

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.

Unrequited answered 27/3, 2015 at 18:22 Comment(5)
can you give me an example? . I need get the position tag of the pin.Popish
And here is my example of a basic custom annotation class written in Swift: github.com/mattneub/Programming-iOS-Book-Examples/blob/master/… Adding a tag property to that is obvious and trivial.Unrequited
This example does not work for me, the click are based on the title of the pin, I have many pins with the same titlePopish
Ok, if i use custom MKAnnotation, I could get the tags I need. now I understand.Popish
How can i get the new var in the viewForAnnotation ?Popish
M
2

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";
Monochasium answered 15/6, 2017 at 5:55 Comment(1)
This is the best answer!Lyophilic

© 2022 - 2024 — McMap. All rights reserved.