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

时间:2014-01-13 13:06:17

标签: ios mapkit mkpointannotation

我在地图上删除了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];
    }

单击注释时,使用变量" id"打开新视图。来自JSON对应的引脚。

- (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绑定到相应的引脚的方法,所以每次用户触摸一个引脚时,我都知道该引脚的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];

}

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

2 个答案:

答案 0 :(得分: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];

您可以看到此simple example on how to do it


你只需要两件事:

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

答案 1 :(得分:2)

MKMapAnnotationView中绘制的每个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];

}