2

我的MKPointAnnotation分数在地图上掉了有问题。我会解释的。

设想:

从服务器读取 JSON -> 在地图上放置图钉 -> 单击图钉时,打开一个新视图,并将参数传递给视图。参数必须绑定到引脚。

到目前为止我的代码:

JSON(仅用于示例目的)

{"places":[{"lat":"30.03","lng":"40.31","id":"1"}]}

读取 JSON 并添加点到地图:

NSString *markersJSON=@"http://test.com/json.php";
NSURLRequest *requestUsername = [NSURLRequest requestWithURL:[NSURL URLWithString:markersJSON]];
NSData *response = [NSURLConnection sendSynchronousRequest:requestUsername
                                             returningResponse:nil error:nil];
NSError *jsonParsingError = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response
                                                         options:0 error:&jsonParsingError];
markerArray = [json objectForKey:@"places"];

    for (NSDictionary *jsonObj in markerArray ){
        latJSON=[jsonObj objectForKey:@"lat"];
        lngJSON=[jsonObj objectForKey:@"lng"];
        alertID=[jsonObj objectForKey:@"id"];
        CLLocationCoordinate2D myCoordinate = {[latJSON doubleValue], [lngJSON doubleValue]};
        MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
        point.coordinate = myCoordinate;
        point.title=[jsonObj objectForKey:@"title"];
        point.subtitle=[jsonObj objectForKey:@"description"];        
        [self.mapView addAnnotation:point];
    }

单击注释时,使用与引脚对应的 JSON 中的变量“id”打开新视图。

- (void)mapView:(MKMapView *)map annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
{   
ViewController *yourViewController = (ViewController *)[navStoryBoard instantiateViewControllerWithIdentifier:@"details_alert"];

**//STUCKED HERE**

[self.navigationController pushViewController:yourViewController animated:YES];

}

问题是我找不到将 id 从 JSON 绑定到相应 pin 的方法,所以每次用户触摸 pin 时,我都会知道 id=1 的 pin 被触摸,然后执行其他操作。例如,使用从数据库中获取的数据打开详细视图。

我希望你能理解我想要做什么。

谢谢!

编辑: 几乎就在那里。

//Annotation.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface Annotation : NSObject<MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
    NSString *placeId;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic,readonly,copy) NSString *title;
@property (nonatomic,readonly,copy) NSString *subtitle;
@property (nonatomic) NSString *placeId;

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description placeId:(NSString *)placeId;

@end

//Annotation.m

#import "Annotation.h"

@implementation Annotation

@synthesize coordinate;
@synthesize title;
@synthesize subtitle;
@synthesize placeId;

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:placeName description:description placeId:(NSString *)placeIda;
{
    self = [super init];
    if (self != nil) {
        coordinate = location;
        title = placeName;
        subtitle = description;
        placeId=placeIda;

    }
    return self;
}

- (void)dealloc {

}
@end


Add pins from JSON loop:

for (NSDictionary *jsonObj in markerArray ){
        latJSON=[jsonObj objectForKey:@"lat"];
        lngJSON=[jsonObj objectForKey:@"lng"];
        alertID=[jsonObj objectForKey:@"id"];
        CLLocationCoordinate2D myCoordinate = {[latJSON doubleValue], [lngJSON doubleValue]};

       Annotation *pin = [[Annotation alloc] initWithCoordinates:myCoordinate placeName:[jsonObj objectForKey:@"title"] description:[jsonObj objectForKey:@"description"] placeId:alertID];
       [self.mapView addAnnotation:pin];
    }

Touch pin and pass pin ID to other view

- (void)mapView:(MKMapView *)map annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
{
    id <MKAnnotation> annotation = [view annotation];
    CLLocationCoordinate2D centerCoordinate =[annotation coordinate ];

    ViewController *yourViewController = (ViewController *)[navStoryBoard instantiateViewControllerWithIdentifier:@"details_alert"];

    //////
    Annotation *mysc=view.annotation;
    [yourViewController setValue:[NSString stringWithFormat:@"%lu",(unsigned long)mysc.placeId] forKey:@"sendAlertID"];
    //////

    [self.navigationController pushViewController:yourViewController animated:YES];

}

以上所有工作。但是mysc.placeId传递给 viewController 是完全错误的。例如,对于 id=1(来自 JSON)的 pin,sendAlertID(变量 from的 NSLogyourViewController是 245513072 !!!

4

2 回答 2

5
- (void)mapView:(MKMapView *)map annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
{   
ViewController *yourViewController = (ViewController *)[navStoryBoard instantiateViewControllerWithIdentifier:@"details_alert"];

**//STUCKED HERE**

[self.navigationController pushViewController:yourViewController animated:YES];

}

所以:

只需创建符合MKAnnotation协议的您自己的对象。然后您可以:

MyAnnotationObject *object = (MyAnnotationObject *)view.annotation;
NSString *jsonId = object.id;

代替:

 MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
 point.coordinate = myCoordinate;
 point.title=[jsonObj objectForKey:@"title"];
 point.subtitle=[jsonObj objectForKey:@"description"];   

您将使用自己的类:

MyAnnotationObject *annotation = [[MyAnnotationObject alloc] initWithCoordinates:coordinates title:title subtitle:subtitle];

你可以看到这个简单的例子来了解如何做到这一点


你只需要两件事:

  1. 遵守MKAnnotation协议:@interface MyAnnotationObject :NSObject <MKAnnotation>
  2. 创造@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
于 2014-01-13T13:13:00.337 回答
3

MKMapAnnotationView在 a 中绘制的每一个MKMapView都应该有一个称为annotation实现MKAnnotation协议的属性。

如果您实现自定义注解,您可以添加任意数量的参数。

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface STMapAnnotation : NSObject <MKAnnotation>
@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic,readonly,copy) NSString *title;
@property (nonatomic,readonly,copy) NSString *subtitle;
@property (nonatomic) NSUInteger placeId;

- (id)initWithTitle:(NSString *)title
           subtitle:(NSString *)subtitle
        coordinates:(CLLocationCoordinate2D)coordinates
            placeId:(NSUInteger)placeId;
@end

然后,您将创建此自定义并将其添加annotation到方法中,MKMapView以便delegate您可以检索您添加的任何自定义信息。

- (void)mapView:(MKMapView *)map annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
{   
         ViewController *yourViewController = (ViewController *)[navStoryBoard instantiateViewControllerWithIdentifier:@"details_alert"];
         STMapAnnotation *myCustomAnnotation = view.annotation;
         yourViewController.placeId = myCustomAnnotation.placeId;
         [self.navigationController pushViewController:yourViewController animated:YES];

}
于 2014-01-13T13:18:42.407 回答