在mapView中识别MKPointAnnotation [英] Identify MKPointAnnotation in mapView

查看:91
本文介绍了在mapView中识别MKPointAnnotation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我至少有100个不同点...如何将每个点与"listOfPoints"中的位置相关联,从而在viewForAnnotation中关联的位置中分配标签.

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

如何将此标签与我的"listOfPoints"上的位置相关联

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

pin.tag = tagPosition; 

还是还有另一种方法?

推荐答案

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

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

我从MKAnnotation定制了一个用于注释的类:

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

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

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

这篇关于在mapView中识别MKPointAnnotation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆