我至少有100个不同点...如何将每个点与我的列表中的位置关联起来'在viewForAnnotation中关联的位置分配标记。
在这里我添加了我的积分,有些活动会有相同的标题。
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);
}
如何将此标记与我的列表中的位置相关联&#39;
pin.tag = tagPosition;
还是有另一种方式吗?
答案 0 :(得分:6)
我觉得这很容易,我在app中做这个。
我从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
答案 1 :(得分:3)
问题是您使用的是普通的内置MKPointAnnotation。它没有tag
属性。您需要定义自己的MKAnnotation类(即采用MKAnnotation协议的类)。这样,它可以有你喜欢的任何属性。
答案 2 :(得分:0)
我对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";