2014-01-18 60 views
7

我正在整合谷歌地圖sdk。它的一切正常。但如何去除特定的標記(針點)時,第二次將出現(我不使用Mapkit)使用谷歌地圖sdk在ios中從GMSMapview中刪除特定的GMSMarker

我想以下幾點:

如果我在地圖上點擊,然後一個標記針在該位置現在產生如果我點擊地圖上的另一個位置,然後顯示兩個引腳,但我想刪除舊的標記引腳。

我也用,

[self.mapView clear]; 

但是很明顯,從GMSMapView中所有其他的標誌點。

以下是在地圖上添加PIN代碼:

  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; 

請幫我解決了這個東西.. !!

在此先感謝.. :)

+0

Ple ase添加你的代碼片段,如何在地圖上添加Marker,比它能理解的要好。 – Harin

+0

我也使用它,但它不能工作.. – jigs

+0

@ jigs你找到了方法嗎?我也想移動我的特定標記。我怎樣才能做到這一點? –

回答

0

是的,我得到的解決方案。 添加PIN像下面這樣:

- (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]; 
+0

你好,我有一個非常類似的問題,但我在一個循環中生成標記,並且想要刪除這些標記,而不僅僅是一個。我試過你的實現,但它似乎沒有工作。你可以看看我的帖子嗎?謝謝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

環地圖中所有的標記,並且可以使用標題或摘錄,以決定哪些標記刪除

爲map.markers不再是谷歌地圖IOS SDK使用,你需要有一個NSMutableArray中存儲所有標誌物循環的目的

,您可以使用標記的userData,marker.userData,我傾向於在標記中存儲nsdictionary信息以防止重複標題名稱。

歡呼聲。

20

要刪除一個特定的銷從GMSMapView中保持銷的參考(如果有多個然後使用陣列),然後使用這個代碼

currLocMarker.map = nil; 

要除去所有事情,包括從GMSMapView中銷聚行使用此代碼

[ _mapView clear]; 
1

我做出這樣的:

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

這爲我工作 -

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

其中

var markers = [GMSMarker]() 

標記包含了所有的標記覆蓋了的MapView

+0

嗨,如果你不使用它,爲什麼你會收到一個GMSMapView? –

0

當您在特定的標記挖掘,這將刪除標記

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

    marker.map = nil; 
    return YES; 
}