2

嗨,我正在为 ios 开发 Google Maps SDK。我想在 NSArray 的 Google 地图中绘制一些标记,其中包含位置名称、纬度和经度。我尝试使用似乎已经有点蹩脚的 For 循环,但是,

 for(int i=0;i<=[myArray count];i++){
    self.view = mapView_;
    NSString *lat = [[myArray objectAtIndex:i] objectForKey:@"latitude"];
    NSString *lon = [[myArray objectAtIndex:i] objectForKey:@"longitude"];
    double lt=[lat doubleValue];
    double ln=[lon doubleValue];        
    NSString *name = [[myArray objectAtIndex:i] objectForKey:@"name"];
    NSLog(@"%@ and %@ and %f and %f of %@",lat,lon, lt,ln,name);
    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.animated=YES;
    marker.position = CLLocationCoordinate2DMake(lt,ln);
    marker.title = name;
    marker.snippet = @"Kathmandu";
    marker.map = mapView_;

}

这里myarray是具有位置名称的数组,字符串格式的纬度经度,我将其转换为双精度。当我运行此代码时,Xcode 显示 NSRangeException: index beyond bounds,这可能是因为我试图使用相同的对象在同一个地图中显示不同的索引。但与此同时,我想不出任何方法可以将GMSMarker用作数组。但是,如果我使用不同的 GMSMarker 对象,我可以绘制多个标记,但这并不能解决我的问题。我制作了另一个像这样的对象,使用两个 GMSMarker 对象在同一张地图上显示两个标记。

 GMSMarker *marker1 = [[GMSMarker alloc] init];
 marker1.animated=YES;
 marker1.position = CLLocationCoordinate2DMake(lt,ln);
 marker1.title = name;
 marker1.snippet = @"Kathmandu";
 marker1.map = mapView_;

有什么帮助吗?

4

1 回答 1

2

但与此同时,我想不出任何方法可以将 GMSMarker 用作数组。

尝试这个:

NSMutableArray *markersArray = [[NSMutableArray alloc] init];
for(int i=0;i<[myArray count];i++){

    // ... initialise marker here
    marker.map = mapView_;

    [markersArray addObject:marker];
    [marker release];
}
于 2013-07-13T10:37:40.790 回答