1

我在使用适用于 iOS 的 Google Maps SDK(版本 1.5.0)绘制多个标记时遇到问题。我是 Objective c(使用 Xcode 4.6.3 版)和 Google Maps SDK 的新手,所以我可能会遗漏一些明显的东西。我也在使用 iOS 6.1 模拟器。我正在尝试边做边学。

我花了几天时间搜索并找到了几个处理这个主题的线程,但没有一个解决方案适合我。我遇到的问题是我的标记相互覆盖。我创建了一个 NSArray,locations,它将有 4 列和未知的行。列是纬度、经度、名称、地址。

for(int i=0;i<[locations count];i++){
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40.0823
                                                        longitude:-74.2234
                                                             zoom:7];
    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    self.view = mapView_;
    mapView_.myLocationEnabled = YES;
    mapView_.mapType = kGMSTypeHybrid;
    mapView_.settings.myLocationButton = YES;
    mapView_.settings.zoomGestures = YES;
    mapView_.settings.tiltGestures = NO;
    mapView_.settings.rotateGestures = NO;
    NSString *lat = [[locations objectAtIndex:i] objectAtIndex:0];
    NSString *lon = [[locations objectAtIndex:i] objectAtIndex:1];
    double lt=[lat doubleValue];
    double ln=[lon doubleValue];
    NSString *name = [[locations objectAtIndex:i] objectAtIndex:2];
    NSMutableArray *markersArray = [[NSMutableArray alloc] init];
        GMSMarker *marker = [[GMSMarker alloc] init];
        marker.appearAnimation=YES;
        marker.position = CLLocationCoordinate2DMake(lt,ln);
        marker.title = name;
        marker.snippet = [[locations objectAtIndex:i] objectAtIndex:3];
        marker.map = mapView_;

       [markersArray addObject:marker];

}
4

1 回答 1

5

我看到了一些可能相关的错误。每次遍历 for 循环中的位置数组时,都会覆盖markersArray。在for循环之外实例化markersArray。

您可以尝试 NSLog 您尝试绘制的每个标记的坐标吗?

如果坐标相同,则标记应绘制在彼此的正上方,使标记看起来被覆盖,但它们只是在彼此之上。

完成后记录位置和标记数组的计数,以确保它们彼此相等,以便快速检查。

*编辑:我看到你的问题。每次遍历 for 循环时,您都会覆盖 MapView。

尝试这样的事情:

// Create a markersArray property
@property (nonatomic, strong) NSMutableArray *markersArray;
// Create a GMSMapView property
@property (nonatomic, strong) GMSMapView *mapView_;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupMapView];
    [self plotMarkers];
}

// Lazy load the getter method
- (NSMutableArray *)markersArray
{
    if (!_markersArray) {
        _markersArray = [NSMutableArray array];
    }
    return _markersArray;
}

- (void)setupMapView
{
    self.mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    self.view = self.mapView_;
    self.mapView_.myLocationEnabled = YES;
    self.mapView_.mapType = kGMSTypeHybrid;
    self.mapView_.settings.myLocationButton = YES;
    self.mapView_.settings.zoomGestures = YES;
    self.mapView_.settings.tiltGestures = NO;
    self.mapView_.settings.rotateGestures = NO;

    // You also instantiate a GMSCameraPosition class, but you don't add it to your mapview
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40.0823
                                                            longitude:-74.2234
                                                                 zoom:7];

}

- (void)plotMarkers
{
    // I don't know how you're creating your locations array, so I'm just pretending
    // an array will be returned from this fake method
    NSArray *locations = [self loadLocations];
    for (int i=0; i<[locations count]; i++){

        NSString *lat = [[locations objectAtIndex:i] objectAtIndex:0];
        NSString *lon = [[locations objectAtIndex:i] objectAtIndex:1];
        double lt=[lat doubleValue];
        double ln=[lon doubleValue];
        NSString *name = [[locations objectAtIndex:i] objectAtIndex:2];

        // Instantiate and set the GMSMarker properties
        GMSMarker *marker = [[GMSMarker alloc] init];
        marker.appearAnimation=YES;
        marker.position = CLLocationCoordinate2DMake(lt,ln);
        marker.title = name;
        marker.snippet = [[locations objectAtIndex:i] objectAtIndex:3];
        marker.map = self.mapView_;

        [self.markersArray addObject:marker];

    }
}
于 2013-10-03T18:23:40.253 回答