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];
}
問題は、JSON の id を対応するピンにバインドする方法が見つからないため、ユーザーがピンに触れるたびに、id=1 のピンが触れられていることがわかり、他の操作を行うことです。たとえば、 DB から取得したデータを含む詳細ビューを開きます。
私がやろうとしていることを理解していただければ幸いです。
ありがとうございました!
編集: ほとんどあります。
//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 から) のピンの場合、sendAlertID
(変数 fromのyourViewController
NSLog は 245513072 です !!!