Remove particular GMSMarker from GMSMapview using Google Map sdk in ios

后端 未结 9 3153
-上瘾入骨i
-上瘾入骨i 2021-02-19 23:18

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)

I want the following:

相关标签:
9条回答
  • 2021-02-20 00:00

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

    currLocMarker.map  = nil;
    

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

    [ _mapView clear];
    
    0 讨论(0)
  • 2021-02-20 00:01

    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];
    
    0 讨论(0)
  • 2021-02-20 00:08

    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

    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 .

    cheers.

    0 讨论(0)
  • 2021-02-20 00:09

    Check This one and try it in your code

    Remove a marker in Google Maps sdk

    0 讨论(0)
  • 2021-02-20 00:10

    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

    0 讨论(0)
  • 2021-02-20 00:11

    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
    }
    
    0 讨论(0)
提交回复
热议问题