谷歌地图标记不删除iOS

emo*_*ity 0 iphone objective-c ios google-maps-sdk-ios

我正在运行一个线程来每10秒获取一次驱动程序位置,并希望从地图中删除添加的标记,但它不起作用..

我的代码:

-(void)APiResponse:(id)returnJson
{        
        [googleMapsDriverPin setMap:nil];
        googleMapsDriverPin = nil;

        NSMutableArray *driverPins = [[NSMutableArray alloc]init];
        for (int x = 0; x < [[returnJson valueForKey:@"drivers"] count]; x++) {
            CLLocation *driverLocations = [[CLLocation alloc]initWithLatitude:[[[[returnJson valueForKey:@"drivers"] objectAtIndex:x] valueForKey:@"driver_latitude"] doubleValue] longitude:[[[[detail valueForKey:@"drivers"] objectAtIndex:x] valueForKey:@"driver_longitude"] doubleValue]];
            [driverPins addObject:driverLocations];
        }

        for (CLLocation *newLocation in driverPins) {
            googleMapsDriverPin = [[GMSMarker alloc] init];
            [googleMapsDriverPin setPosition:newLocation.coordinate];
            [googleMapsDriverPin setAnimated:YES];
            [googleMapsDriverPin setTitle:@"title"];
            [googleMapsDriverPin setSnippet:@"snippet"];
            [googleMapsDriverPin setIcon:[GMSMarker markerImageWithColor:[UIColor blackColor]]];
            [googleMapsDriverPin setMap:googleMaps];
         }
}
Run Code Online (Sandbox Code Playgroud)

它只是每隔10秒添加和添加而不是删除,请帮忙!谢谢!

Tom*_*idd 5

它是一种快速而又脏的选项,但是如果你想这样,GMSMarker有一个userData属性,你可以使用它来标记驱动程序引脚

- (void)apiResponse:(id)returnJson
{        
    for (GMSMarker *pin in self.googleMaps.markers) {
        if (pin.userData == @"Driver Pin"){ 
            pin.map = nil; 
        }
    }

    ...

    for (CLLocation *newLocation in driverPins) {
        googleMapsDriverPin = [[GMSMarker alloc] init];
        ...
        [googleMapsDriverPin setUserData:@"Driver Pin"];
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:

[self.googleMapsView clear];
Run Code Online (Sandbox Code Playgroud)