我的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 !!!