在 ios 中使用 Google Map sdk 从 GMSMapview 中删除特定的 GMSMarker

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21200914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:10:48  来源:igfitidea点击:

Remove particular GMSMarker from GMSMapview using Google Map sdk in ios

iosiphonegoogle-mapsgmsmapview

提问by jigs

I am integrating google maps sdk. Its all work fine. But how to remove particular Marker(Pin Point) when second will appear.(I am not using Mapkit)

我正在集成谷歌地图 sdk。它一切正常。但是如何在第二个出现时删除特定的标记(针点)。(我没有使用 Mapkit)

I want the following:

我想要以下内容:

If i tap on map then one marker pin is generate at that location now if i tap on another location on map then two pins are displayed but i want to remove the old marker pin.

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

I also use,

我也用,

[self.mapView clear];

But it was clear all other marker points from GMSMapview.

但是从 GMSMapview 中可以清楚地看到所有其他标记点。

Following is the code to add pin on Map:

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

            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;

Please help me to solve out this stuff..!!

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

Thanks in advance..:)

提前致谢..:)

采纳答案by jigs

Yes, I got that solution. Add pin like the following:

是的,我得到了那个解决方案。添加如下所示的引脚:

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

Please remove the following line if you used in the above:

如果您在上面使用,请删除以下行:

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

回答by Spydy

To remove a particular pin from GMSMapView keep reference of pin (if there are multiple then use array) then use this code

要从 GMSMapView 中删除特定的引脚,请保留引脚的引用(如果有多个,则使用数组)然后使用此代码

currLocMarker.map  = nil;

To remove all things including pins poly lines from GMSMapView use this code

要从 GMSMapView 中删除包括 pin 折线在内的所有内容,请使用此代码

[ _mapView clear];

回答by 3ddy

I made like this:

我是这样制作的:

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

and worked well for me !

对我来说效果很好!

回答by Atul Kaushik

This worked for me -

这对我有用-

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

where

在哪里

    var markers = [GMSMarker]()

markers contains all the marker overlays for the mapView

标记包含 mapView 的所有标记覆盖

回答by Harin

Check This one and try it in your code

检查这个并在您的代码中尝试

Remove a marker in Google Maps sdk

删除 Google Maps sdk 中的标记

回答by chings228

loop all marker in the map , and you can use title or snippet to decide which marker you remove

循环地图中的所有标记,您可以使用标题或片段来决定删除哪个标记

as map.markers is no longer to use in google map ios sdk , you need to have a nsmutablearray to store all marker for looping purpose

由于 map.markers 不再在谷歌地图 ios sdk 中使用,您需要有一个 nsmutablearray 来存储所有标记以用于循环目的

and you can make use of userData of the marker , marker.userData , which i prefer to store a nsdictionary information in the marker in order to prevent from duplicate name of title .

并且您可以使用标记的 userData,marker.userData,我更喜欢在标记中存储 nsdictionary 信息,以防止 title 的重复名称。

cheers.

干杯。

回答by iOS

When you tap on specific marker this will remove that marker

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

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

    marker.map = nil;
    return YES;
}

回答by venkatesan ns

swift 5

迅捷 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
}