将参数从引脚传递到其他视图

The*_*s80 1 mapkit ios mkpointannotation

我的问题MKPointAnnotation在地图上丢失了.我会解释一下.

场景:

从服务器读取JSON - >在地图上删除引脚 - >单击某个引脚时,打开一个带有传递给视图的参数的新视图.该参数必须绑定到引脚.

我的代码到目前为止:

JSON(仅限示例用途)

{"places":[{"lat":"30.03","lng":"40.31","id":"1"}]}
Run Code Online (Sandbox Code Playgroud)

读取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];
    }
Run Code Online (Sandbox Code Playgroud)

单击注释时,使用与引脚对应的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];

}
Run Code Online (Sandbox Code Playgroud)

问题是我无法找到将id从JSON绑定到相应引脚的方法,因此每次用户触摸引脚时,我都会知道触发了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];

}
Run Code Online (Sandbox Code Playgroud)

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

Rui*_*res 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];

}
Run Code Online (Sandbox Code Playgroud)

所以:

只需创建符合MKAnnotation协议的对象即可.然后你可以:

MyAnnotationObject *object = (MyAnnotationObject *)view.annotation;
NSString *jsonId = object.id;
Run Code Online (Sandbox Code Playgroud)

代替:

 MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
 point.coordinate = myCoordinate;
 point.title=[jsonObj objectForKey:@"title"];
 point.subtitle=[jsonObj objectForKey:@"description"];   
Run Code Online (Sandbox Code Playgroud)

你会使用自己的类:

MyAnnotationObject *annotation = [[MyAnnotationObject alloc] initWithCoordinates:coordinates title:title subtitle:subtitle];
Run Code Online (Sandbox Code Playgroud)

你可以看到这个简单的例子来说明如何做到这一点.


你只需要两件事:

  1. 遵守MKAnnotation协议:@interface MyAnnotationObject :NSObject <MKAnnotation>
  2. 创建 @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;