ios - json 注释未显示在 mapview 上

标签 ios json mkmapview mapkit mkpointannotation

我试图在 map 上显示来自 json 的信息,但没有任何反应我认为缺少某些东西,因此信息可以显示在 map 上 设法让 json 工作并获取日志信息,现在我想知道谁创建了 nsdictionary 和数组,以便我可以在 mapview 上显示信息。 这是到目前为止我与阵列的 war :

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

设法添加此代码,但信息仍未显示在 map View 上:

- (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;}

最佳答案

我们没有看到您的 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

  • 一个更微妙的问题是,如果注解 View 成功出队,您并没有重置它的注解。因此,我建议在 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 的相等方法。话虽如此,检查类成员在这里就足够了。

关于ios - json 注释未显示在 mapview 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16329212/

相关文章:

ios - 从另一个应用程序 iOS 打开 Venmo

java - 如何正确生成JsonArray?

c# - 有没有办法在事先不知道对象结构的情况下从 JSON 组合匿名类型对象?

jquery - 我有这段代码,但我不知道出了什么问题

iphone - 在 MKMapView 中移动 MKCircle

ios - 如何在 Swift 5 中的 UIScrollView 内以编程方式生成的 UIView 上实现点击手势

ios - 在 NSUserDefaults iOS 中保存 int 的问题

iOS:检查 coredata 对象是否仍然存在?

iphone - 打开 MKMapView 时是否应该检查互联网连接?

ios - MKMapView cornerRadius 仅适用于左上角