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];

}

問題は、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のyourViewControllerNSLog は 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];

これを行う方法については、この簡単な例を参照してください。


必要なものは次の 2 つだけです。

  1. MKAnnotationプロトコルに準拠:@interface MyAnnotationObject :NSObject <MKAnnotation>
  2. 作成@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
于 2014-01-13T13:13:00.337 に答える
3

MKMapAnnotationViewで描画されるすべてのものには、プロトコルを実装するMKMapViewというプロパティが必要です。annotationMKAnnotation

カスタム アノテーションを実装する場合は、必要な数のパラメーターを追加できます。

#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 に答える