0

我正在运行一个线程以每 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];
         }
}

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

4

5 回答 5

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"];
    }
}

更新:

[self.googleMapsView clear];
于 2013-05-16T14:25:04.170 回答
2

基于 pin id,您还可以删除 pin。这里 deletePinId 整数用于选定的引脚 ID。

for(GMSMarker *pin in self.mapView_.markers) {
NSLog(@"pin.userData : %@",pin.userData);
int pinId1 = [[pin.userData valueForKey:@"pin_id"] integerValue];
if(deltePinId == pinId1 ){
    pin.map = nil;
 }

}

于 2014-01-01T06:17:28.223 回答
1

你目前只存储一个标记,但你想添加 N 个标记——所以(正如撒克逊人所说)你需要一个数组来保存所有的引脚 :)

@interface YouClass

...

@property(nonatomic, retain) NSMutableArray *googleMapsDriverPins;
@end

@implementation YourClass

...

-(void)APiResponse:(id)returnJson
{    
    for(GMSMarker *pin in self.googleMapsDriverPins) {
        pin.map = nil;
    }    
    self.googleMapsDriverPins = 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];
    }

    self.googleMapsDriverPins = [NSMutableArray arrayWithCapacity:driverPins.count];
    for (CLLocation *newLocation in driverPins) {
        GMSMarker *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];
        [self.googleMapsDriverPins addObject:googleMapsDriverPin];
     }
 }
 @end
于 2013-05-16T10:21:02.093 回答
0

看起来您有一个添加多个驱动程序的循环,每个驱动程序都分配给成员变量googleMapsDriverPin。然后下次它会删除googleMapsDriverPin- 但这只是您添加的最后一个引脚,而不是全部。

为此,您需要将循环内的每个标记添加到一个数组中,然后在下次更新时从地图中删除所有标记。

于 2013-05-15T08:36:16.420 回答
0

在斯威夫特 2 中:

为您的地图创建一个出口:

@IBOutlet weak var mapView: GMSMapView!

创建一个数组来存储所有标记

var markers = [GMSMarker]()

像这样创建标记:

    func funcName() {    
        let position = CLLocationCoordinate2DMake(lat, lon)
        let marker = GMSMarker(position: position)

        for pin: GMSMarker in self.markers {
             if pin.userData as! String == "from" {
                   pin.map = nil
             }
        }

        marker.icon = UIImage(named: "navigation-red")
        marker.userData = "from"
        marker.map = self.mapView
        self.markers.append(marker)
    }

您可以将 userData 属性设置为您想要的任何内容,然后使用该字符串删除该标记。执行 funcName 函数时,从地图中删除所有 userData 为“来自”的标记。如果您有任何疑问,请告诉我.

于 2016-08-15T09:27:08.307 回答