【问题标题】:annotations - NSArray - How to do this?注释 - NSArray - 如何做到这一点?
【发布时间】:2012-02-06 09:51:28
【问题描述】:

我想在 Mapview 中添加注释,使用通过 URL 获取的 Json 数据。 可以吗?

NSMutableArray *annotations = [[NSMutableArray alloc]init];

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://example.com/LData.php"]];

//Data: Longitud/Latitud/Country;.....

NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(str);

[foo removeAllObjects]; 

NSArray *foo2 =[str componentsSeparatedByString: @";"];
int i=0;
for(i=0;i<[foo2 count]; i++){

    [foo insertObject:[foo2 objectAtIndex:i] atIndex:i];
        NSArray *foo3 =[foo2 componentsSeparatedByString: @"/"];

到目前为止一切顺利 ¿ As I Lay 介绍变量 [i]?

        CLLocationCoordinate2D theCoordinate[i];
        theCoordinate1.latitude[i] = [foo3 objectAtIndex:1]);//Longitud
        theCoordinate1.longitude[i] = [foo3 objectAtIndex:2]);//Latitud

        MyAnnotation* myAnnotation[i]=[[MyAnnotation alloc] init];

    myAnnotation[i].coordinate=theCoordinate[i];
    myAnnotation[i].title=@""+[foo3 objectAtIndex:3];
    myAnnotation[i].subtitle=@"in the city";

        [mapView addAnnotation:myAnnotation[i]];
        [annotations addObject:myAnnotation[i]];




}

我该如何解决这个问题?

【问题讨论】:

  • 请清楚说明您遇到的问题。

标签: objective-c annotations


【解决方案1】:

为什么要使用不同的 CLLocationCoordinate2D 变量?

theCoordinate1.latitude[i] = [foo3 objectAtIndex:1]);//Longitud
theCoordinate1.longitude[i] = [foo3 objectAtIndex:2]);//Latitud

然后

myAnnotation[i].coordinate=theCoordinate[i];

我认为你需要调整你的代码,有些问题可以自己解决。

【讨论】:

  • 为什么要使用 CLLocationCoordinate2D 不同的变量?为它们的坐标添加注释
  • 我想要的是获取经纬度不同方数组的数据,并在地图上添加注释。
  • 我在这里感到困惑的是:theCoordinate[i].latitude = [foo3 objectAtIndex:1]);//经度 theCoordinate[i].longitude = [foo3 objectAtIndex:2]);// Latitud 你能把注释的创建者放在 For 中并创建它们吗?
【解决方案2】:
NSMutableArray *annotations = [[NSMutableArray alloc]init];

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://example.com/LData.php"]];

//Data: Longitud/Latitud/Country;.....

NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(str);

[foo removeAllObjects]; 

NSArray *foo2 =[str componentsSeparatedByString: @";"];
int i=0;
for(i=0;i<[foo2 count]; i++){

    [foo insertObject:[foo2 objectAtIndex:i] atIndex:i];
        NSArray *foo3 =[foo2 componentsSeparatedByString: @"/"];


     float realLatitude = [[foo3 objectAtIndex:1] floatValue];//Longitud
     float realLongitude = [[foo3 objectAtIndex:0] floatValue];//Latitud        

         MyAnnotation* myAnnotation = [[MyAnnotation alloc] init];

     CLLocationCoordinate2D theCoordinate;
            theCoordinate.latitude = realLatitude;
            theCoordinate.longitude =  realLongitude;


    myAnnotation.coordinate = theCoordinate;
     myAnnotation.title = [foo3 objectAtIndex:3];
      //myAnnotation.subtitle = [note objectForKey:@"stationAddressKey"];

      [_mapView setDelegate:self];
      [_mapView addAnnotation:myAnnotation];
      [myAnnotation release];  

}




-(MKAnnotationView *)_mapView:(MKMapView *)aMapView viewForAnnotation:(id<MKAnnotation>)annotation

{  
    if ([annotation isKindOfClass:[MyAnnotation class]])
    {
        static NSString *reuseId = @"customAnn";

        MKAnnotationView *customAnnotationView = [aMapView dequeueReusableAnnotationViewWithIdentifier:reuseId];

        if (customAnnotationView == nil)
        {
            customAnnotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] autorelease];
            UIImage *pinImage = [UIImage imageNamed:@"pin-green.png"];
            [customAnnotationView setImage:pinImage];
            customAnnotationView.canShowCallout = YES;
            UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            customAnnotationView.rightCalloutAccessoryView = rightButton;
        }

       // NSString *iconFilename = @"";
       // MyAnnotation *myAnn = (MyAnnotation *)annotation;
       // if ([myAnn.stationIdKey isEqualToString:@"BP"])
            iconFilename = @"bp-logo.png";
       // else
       //     if ([myAnn.stationIdKey isEqualToString:@"Caltex"])
       //         iconFilename = @"caltex.png";
       //     else
       //         if ([myAnn.stationIdKey isEqualToString:@"Shell"])
       //             iconFilename = @"shell.png";
       // UIImageView *leftIconView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:iconFilename]] autorelease];
        customAnnotationView.leftCalloutAccessoryView = leftIconView;

        customAnnotationView.annotation = annotation;

        return customAnnotationView; 
    }

    return nil; 
}



-(void)_mapView:(MKMapView *)_mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{ if ([view.annotation isKindOfClass:[MyAnnotation class]])
{
    MyAnnotation *myAnn = (MyAnnotation *)view.annotation;
    NSLog(@"callout button tapped for station id %@", myAnn.stationIdKey);
}
else
{
    NSLog(@"callout button tapped for annotation %@", view.annotation);
} }

这样对吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多