使用Google Map SDK在iOS中从GMSMapView中删除特定的GMSMarker

13

我正在集成Google地图SDK。一切都运行良好。但是当第二个标记(Pin Point)出现时,如何删除特定的标记?(我没有使用Mapkit)

我想要以下内容:

如果我在地图上点击,则会在该位置生成一个标记。现在,如果我在地图上另一个位置上再次点击,则会显示两个标记,但我想要删除旧的标记。

我也使用了以下代码:

[self.mapView clear];

但是很明显从GMSMapview中获取了其他所有标记点。

以下是在地图上添加标记的代码:

            GMSMapView *mapView;
            GMSMarker *currLocMarker = [[GMSMarker alloc] init];
            currLocMarker.map  = nil;
            [currLocMarker setTitle:NSLocalizedString(@"current_location_title", nil)];
            currLocMarker.icon = [UIImage imageNamed:@"pin_fetch_location.png"];
            currLocMarker.position = CLLocationCoordinate2DMake(pCoordinate.latitude, pCoordinate.longitude);
            currLocMarker.map = self.mapView;

请帮我解决这个问题..!!

提前致谢..:)


请添加您的代码片段,演示如何在地图上添加标记,这样就可以理解了。 - Harin
我也使用了那个,但它不能工作。 - jigs
@jigs,你找到方法了吗?我也想移动我的特定标记。我该怎么做? - Jaimin Modi
9个回答

28

如果要从GMSMapView中删除特定的标记,需要先引用该标记(如果有多个,则使用数组),然后使用以下代码

currLocMarker.map  = nil;

使用以下代码从GMSMapView中删除所有东西,包括点和折线

[ _mapView clear];

3
这对我有效 -
func removeMarkers(mapView: GMSMapView){
    for (index, _) in markers.enumerate() {
        //print("Item \(index): \(element)")
                    self.markers[index].map = nil
    }
}

where

    var markers = [GMSMarker]()

markers包含地图视图中所有标记覆盖层。


1
你好,如果你不会使用它,为什么会收到一个GMSMapView的呢? - Cristian Mora

2
我这样做了:
GMSMarker *myMarker;

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        if (myMarker) {
            myMarker.map = nil;
            myMarker = nil;
        }
        myMarker = [[GMSMarker alloc] init];
        myMarker.position = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude);
        myMarker.title = @"title";
        myMarker.map = mapView_;
    }];
}

并且对我很有帮助!

1

1
循环地图中的所有标记,您可以使用标题或片段来决定要删除哪个标记。
由于在Google Maps iOS SDK中不再使用map.markers,因此您需要一个NSMutableArray来存储所有标记以进行循环。
您可以利用标记的userData,marker.userData,我更喜欢在标记中存储NSDictionary信息,以防止重复标题名称。
干杯。

0

当您点击特定的标记时,将会删除该标记

- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {

    marker.map = nil;
    return YES;
}

0

是的,我明白了那个解决方案。 添加像下面这样的引脚:

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinates {

pCoordinate.latitude =coordinates.latitude;
pCoordinate.longitude =coordinates.longitude;

[[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake(coordinates.latitude, coordinates.longitude) completionHandler:^(GMSReverseGeocodeResponse *resp, NSError *error)
                {
     [currLocMarker setTitle:NSLocalizedString(@"current_location_title", nil)];
     currLocMarker.icon = [UIImage imageNamed:@"pin.png"];
     currLocMarker.position = CLLocationCoordinate2DMake(coordinates.latitude,       coordinates.longitude);
     currLocMarker.map = self.mapView;} ] ;}

请在上述使用了此行的情况下删除以下行:

GMSMarker *currLocMarker = [[GMSMarker alloc] init];

你好,我与你有非常相似的问题,不过我是在循环中生成标记并希望删除这些标记,而不仅仅是一个。我尝试了你的实现,但好像并没有起作用。你能否看一下我的帖子?谢谢。 http://stackoverflow.com/questions/26811920/is-there-a-way-to-detect-if-a-marker-is-present-on-the-google-map-to-delete-it - Pangu

0
如果您有不同的标记,并且想要从地图中仅删除特定的标记,则必须保留该标记(s)对象。
say if you have 
var removableMarkers: [GMSMarker]?

you have to append those markers in the above array when adding markers to map

Now, when you want to remove those markers:
  _ = self.removableMarkers.map({
            $0.map = nil
        })
self.RemovableMarkers = []

就是这样!


0

Swift 5

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool
{
    let alertcontrolserver = UIAlertController.init(title : nil, message : "Are you sure you want to Remove ! ", preferredStyle: .alert)
        let okbtn = UIAlertAction(title: "Yes", style: .default, handler: { UIAlertAction in  marker.map = nil
             })
    let cancelbtn = UIAlertAction(title: "No", style: .default, handler: nil)
        alertcontrolserver.addAction(okbtn)
       alertcontrolserver.addAction(cancelbtn)
        self.present(alertcontrolserver, animated: true, completion: nil)
    return true
}

1
欢迎来到Stack Overflow!请不要仅仅回答源代码,尽量提供一个关于你的解决方案如何工作的好描述。参见:如何撰写一个好的答案? 谢谢。 - Selim Yildiz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接