【问题标题】:Google Maps SDK for IOS markers are being overwritten用于 IOS 标记的 Google Maps SDK 正在被覆盖
【发布时间】:2013-10-03 17:56:58
【问题描述】:

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

我花了几天时间搜索并找到了处理此主题的several threads,但没有一个解决方案适合我。我遇到的问题是我的标记相互覆盖。我创建了一个 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];

}

【问题讨论】:

    标签: ios objective-c google-maps-sdk-ios


    【解决方案1】:

    我看到了一些可能相关的错误。每次遍历 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];
    
        }
    }
    

    【讨论】:

    • 感谢您的意见。我得花点时间看看你的答案。不过,我确实让我的工作正常了,因为就像你提到的那样,我每次都在循环加载我的 mapView。
    • 酷,如果您需要其他任何东西,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2016-02-08
    • 2014-05-15
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 2013-02-23
    相关资源
    最近更新 更多