2013-05-02 72 views
1

我试图从json的地图上显示信息,但没有任何反应我认为有些东西丢失,因此信息可以显示在地图上 管理让json工作并获取有关信息日志,现在我想知道谁来创建nsdictionary和数组,以便我可以显示mapview上的信息。 这里是战争我迄今为止与阵列:json注解不显示在地图上

NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]; 

NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 

NSLog(@"Datos Obtenidos:\n%@",dict); 
NSDictionary *cortes; 
NSError* error; 
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data 
                options:kNilOptions 
                 error:&error]; 
NSMutableArray *jsonCheckins = [NSMutableArray array]; 
for(NSDictionary *jsonElement in jsonArray){ 
    InitViewController *checkin = [InitViewController checkinFromDictionary:jsonElement]; 
    [jsonCheckins addObject:checkin]; 
} 

管理添加此代码,但仍然是信息不showning上的MapView:

- (CLLocationCoordinate2D) checkinCoordinate { 
    CLLocationCoordinate2D coordinate; 
    coordinate.latitude = self.checkin.latitud; 
    coordinate.longitude = self.checkin.longitud; 
    return coordinate;} 

- (void)showCheckinAnnotation { 
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init]; 
annotationPoint.coordinate = [self checkinCoordinate]; 
annotationPoint.title = self.checkin.type; 
annotationPoint.subtitle = self.checkin.description; 
[self.mapView addAnnotation:annotationPoint];} 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
MKAnnotationView *annotationView= nil; 
if(annotation != mapView.userLocation) { 
static NSString *annotationViewId = @"annotationViewId"; 
annotationView = (MKAnnotationView *)[mapView   
dequeueReusableAnnotationViewWithIdentifier:annotationViewId]; 
if (annotationView == nil) { 
    annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewId]; 
     } 
    } 
return annotationView;} 
+0

您确定addAnnotation行正在执行并且坐标正确吗?另外请注意,您正在创建一个普通的MKAnnotationView,默认情况下它是空的,除非您添加内容或设置图像。尝试创建MKPinAnnotationView,或者完全删除viewForAnnotation方法以获取默认的视图视图。 – Anna 2013-05-02 03:14:22

+0

同意,'MKPinAnnotationView'是关键问题,但是在'viewForAnnotation'中存在另一个小问题,在'dequeueReusableAnnotationViewWithIdentifier'成功的情况下,你永远不会设置注释。此外,你不共享'checkinFromDictionary',但你的其他方法使用'self.checkin',但你从来没有设置('checkin'是你的第一个方法的局部变量,所以希望这不是你的意思,因为赢得了没有工作)。 – Rob 2013-05-02 04:01:26

回答

2

我们看不到你的checkinFromDictionary,所以我们很难评论,因为我们从来没有看到你设置这个self.checkin属性,但它是令人担忧的。但是我们需要看到更多的代码才能对此进一步发表评论。

但是,只是为了演示,下面是查找并添加所有注释无事的方法:

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.buenosairescortes.com.ar/capitalfederalcortes/getCortes.php"]]; 

NSError *error; 
NSArray *array = [NSJSONSerialization JSONObjectWithData:data 
               options:0 
                error:&error]; 

if (error) 
{ 
    NSLog(@"%s: error=%@", __FUNCTION__, error); 
    return; 
} 

for (NSDictionary *dictionary in array) 
{ 
    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init]; 
    annotation.coordinate = CLLocationCoordinate2DMake([dictionary[@"latitude"] doubleValue], 
                 [dictionary[@"longitude"] doubleValue]); 
    annotation.title = dictionary[@"type"]; 
    annotation.subtitle = dictionary[@"description"]; 
    [self.mapView addAnnotation:annotation]; 
} 

(顺便说一句,我用的是CLLocationCoordinate2dMake方法,这是CoreLocation.framework的一部分,但您也可以使用您的方法。)

根据您的viewForAnnotation,似乎没有必要,因为您似乎没有做任何默认情况下不会执行的操作。但让我们假设你想要它(因为也许你正在计划进一步定制该方法)。

有两个问题与您提供的程序:

  • 您正在创建一个MKAnnotationView,除非你设置它的image它不会做任何事情(或者在设置其image一个类继承它)。如果您只需要一个标准引脚,请改为使用MKPinAnnotationView

  • 一个更微妙的问题是,如果一个注解视图被成功地出队,你不会重置它的注解。所以,我建议在if (annotation == nil)声明中包含一个else子句。

因此,产率:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKAnnotationView *annotationView= nil; 
    if (![annotation isKindOfClass:[MKUserLocation class]]) 
    { 
     static NSString *annotationViewId = @"annotationViewId"; 
     annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewId]; 
     if (annotationView == nil) 
     { 
      annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewId]; 
     } 
     else 
     { 
      annotationView.annotation = annotation; 
     } 
    } 
    return annotationView; 
} 

该方法还检查用于注解类是否是不经由

if (![annotation isKindOfClass:[MKUserLocation class]]) 
{ 
    // do annotation stuff here 
} 

代替

if (annotation != mapView.userLocation) 
{ 
    // do annotation stuff here 
} 

这一个构件是Apple描述的方法在他们的Location Awareness Programming Guide,所以它可能更安全。总的来说,检查两个对象之间的平等并不是完全可靠的(也许你在这里很幸运,但通常不是谨慎的)。支持比较操作的对象通常具有前缀为isEqual的等式方法。话虽如此,在这里检查班级成员是足够的。

+0

感谢Rob完美地工作了最后一件事,一旦您按下注释后该如何显示说明,以及如何将该图钉更改为自定义图像?在此先感谢 – darkjuso 2013-05-02 21:33:54

+0

@ user2259545要显示标注,请在'viewForAnnotation'中添加一行,说'annotationView.canShowCallout = YES;'。要将您的图钉改为自定义图片,您可以使用'MKAnnotationView'而不是'MKPinAnnotationView',但只要确保将'annotationView.image'设置为您的图片。 'annotationView.image = [UIImage imageNamed:@“myimage.png”];'。 – Rob 2013-05-02 21:40:51

+1

感谢一切正常 – darkjuso 2013-05-02 21:58:36