2017-02-24 34 views
0

我想在地圖中添加新的GMSMarker並刪除舊的。以前我使用的是[map clear];方法並添加新標記。它工作正常。但我不想那樣。我想添加新標記並刪除舊標記,而不必每次都清除地圖。iOS GMaps從地圖中刪除舊的GMSMarker

我的代碼:

if(markerMYLocation == nil) 
{ 
    markerMYLocation = [[GMSMarker alloc] init]; 
} 
markerMYLocation.map = nil; 
markerMYLocation.position = CLLocationCoordinate2DMake(latitude_Point, longitude_Point); 
markerMYLocation.title = @"You"; 
markerMYLocation.groundAnchor = CGPointMake(0.5, 0.5); 
markerMYLocation.icon = [UIImage imageNamed:@"white.png"]; 
markerMYLocation.map = mapViewGoog; 

問題: 1)什麼是刪除和添加標記的正確方法是什麼?

2)我只在我的viewDidLoad中初始化標記。是這樣做的正確方法,或者我應該每次添加它時初始化?

回答

0

標記在地圖上查看添加

GMSMarker *marker = [GMSMarker markerWithPosition:[marker.getcoordinateObject MKCoordinateValue]]; 
    marker.icon =[UIImage imageNamed:@「」]; 

    marker.title = @「Driver Pin」    
    marker.map = mapView_; 
    [allMarkeronMap addObject:googleMapsDriverPin]; // Add maker on Array 
    [_mapView addSubview:mapView_]; 

//使MutableArray上的viewDidLoad

NSMutableArray * allMarkeronMap =[NSMutableArray alloc] init]; 

particualr標記或舊標誌上的MapView

刪除
for (GMSMarker *pin in allMarkeronMap) { 
    if (pin.userData == @"Driver Pin"){ 
     pin.map = nil; 
    } 
} 
+0

有沒有這樣的屬性爲'_mapView .markers' –

+0

請檢查編輯答案 –