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?

4

3 回答 3

7

我认为这很容易,我在我的应用程序中执行此操作。

我从 MKAnnotation 为 Annotation 定制了一个类:

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
    }
}

现在您可以在点中使用 MyAnnotation 类中的字段标识符:

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
}

不明白的可以点红这篇精彩文章:

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

于 2015-09-06T22:39:13.353 回答
4

问题是您使用的是普通的内置 MKPointAnnotation。它没有tag财产。您需要定义自己的MKAnnotation 类(即采用MKAnnotation 协议的类)。这样,它可以具有您喜欢的任何属性。

于 2015-03-27T18:22:00.897 回答
2

我在 Objective-C 上的简单解决方案

.h 文件:

#import <MapKit/MapKit.h>

@interface mapMKPointAnnotation : MKPointAnnotation

@property (nonatomic) NSInteger tag;

@end

并使用标签创建对象:

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

方法中的下一个获取标签:

 -(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";
于 2017-06-15T05:55:37.537 回答