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

时间:2014-01-18 06:30:27

标签: ios iphone google-maps gmsmapview

我正在整合谷歌地图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;

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

提前致谢.. :))

9 个答案:

答案 0 :(得分:23)

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

currLocMarker.map  = nil;

要从GMSMapView中删除包括多边形线的所有内容,请使用此代码

[ _mapView clear];

答案 1 :(得分: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_;
    }];
}

对我来说效果很好!

答案 2 :(得分:1)

选中此项并在您的代码中尝试

Remove a marker in Google Maps sdk

答案 3 :(得分:1)

这对我有用 -

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

,其中

    var markers = [GMSMarker]()

标记包含mapView

的所有标记叠加层

答案 4 :(得分: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];

答案 5 :(得分:0)

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

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

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

欢呼声。

答案 6 :(得分:0)

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

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

    marker.map = nil;
    return YES;
}

答案 7 :(得分:0)

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

答案 8 :(得分:0)

如果您有不同的标记,并且只想从地图中删除特定的标记,则必须保留该标记对象。

{Android.OS.NetworkOnMainThreadException: Exception of type 'Android.OS.NetworkOnMainThreadException' was thrown.
  at Java.Interop.JniEnvironment+InstanceMethods.CallObjectMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0006e] in <42748fcc36b74733af2d9940a8f3cc8e>:0 
  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualObjectMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in <42748fcc36b74733af2d9940a8f3cc8e>:0 
  at WindowsAzure.Messaging.NotificationHub.Register (System.String pnsHandle, System.String[] tags) [0x00043] in <7ef3a358b177460dacd73e56198bd8f2>:0 
  at PantAppAndroid.Utils.NotificationUtil.SendRegistrationToServer (Android.Content.Context context) [0x00041] in C:\Users\weiha\source\repos\App2\PantAppAndroid\Utils\NotificationUtil.cs:36 
  --- End of managed Android.OS.NetworkOnMainThreadException stack trace ---
android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513)
    at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:117)
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105)
    at java.net.InetAddress.getAllByName(InetAddress.java:1154)
    at com.android.okhttp.Dns$1.lookup(Dns.java:39)
    at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175)
    at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141)
    at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83)
    at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
    at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
    at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
    at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
    at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:26)
    at com.microsoft.windowsazure.messaging.Connection.executeRequest(Connection.java:213)
    at com.microsoft.windowsazure.messaging.Connection.executeRequest(Connection.java:170)
    at com.microsoft.windowsazure.messaging.Connection.executeRequest(Connection.java:130)
    at com.microsoft.windowsazure.messaging.NotificationHub.refreshRegistrationInformation(NotificationHub.java:301)
    at com.microsoft.windowsazure.messaging.NotificationHub.registerInternal(NotificationHub.java:399)
    at com.microsoft.windowsazure.messaging.NotificationHub.register(NotificationHub.java:148)
    at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
    at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:30)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
}

就是这样!