2014-03-14 54 views
1

我有一個項目,我需要根據我們得到的位置數組在地圖上顯示一些標記。我們使用自定義標記引腳而不是默認值。一個要求是,如果我們放大超過某個縮放閾值,我們需要顯示更大的標記圖標。所以我們有一個小針圖像和一個大針圖像。基於縮放級別,我們需要顯示更大或更小。我明白一種方法是清除所有標記並重新添加它們。但是,由於其他一些挑戰,該解決方案對我們無效。我需要簡單地替換標記圖標圖像。以下是我的代碼:iOS谷歌地圖SDK替換標記圖標

-(void)updateMarkerImagePinSize { 
NSLog(@"Update Marker Image called"); 
//[self.mapView clear]; 

UIImage *pinImage; 
AtmLocations *atmLocations = [ATMLocatorManager sharedATMLocatorManager].atmLocations; 
CLLocationCoordinate2D searchedAddressCoordinate = CLLocationCoordinate2DMake(atmLocations.locations.startLatitude, atmLocations.locations.startLongitude); 
[self addSearchLocationMarkerToMap:searchedAddressCoordinate]; 

NSArray *locationArray = atmLocations.locations.location; 

if (locationArray == nil || [locationArray count] == 0){ 
    return; 
} 

for (Location *location in locationArray) { 

    CLLocationCoordinate2D markerCoordinate = CLLocationCoordinate2DMake([[[location attributes] latitude] doubleValue], [[[location attributes] longitude] doubleValue]); 

    GMSMarker *marker = [GMSMarker markerWithPosition:markerCoordinate]; 

    if ([location.attributes.locationType isEqualToString:@"ATM"]) { 


     if (self.mapView.camera.zoom < kZoomThresoldForMarkerImageSizeChange) { 
      pinImage = [UIImage imageNamed:@"atm_pin_small"]; 
     } else { 
      pinImage = [UIImage imageNamed:@"atm_pin"]; 
     } 
    } else { 

     if (self.mapView.camera.zoom < kZoomThresoldForMarkerImageSizeChange) { 
      pinImage = [UIImage imageNamed:@"office_pin_small"]; 
     } else { 
      pinImage = [UIImage imageNamed:@"office_pin"]; 
     } 

    } 


    marker.map = nil; 
    marker.icon = pinImage; 
    marker.map = self.mapView; 

} 

}

不過,我所面臨的問題是,無論是標記圖標圖像依然存在。我該如何更換標記圖像而不顯示兩者?

回答

1

其實,我想通了,什麼問題是:

GMSMarker *marker = [GMSMarker markerWithPosition:markerCoordinate]; 

我錯過了markerWithPosition是一個構造函數和在該位置不能簡單地返回標記,如果它的存在。因此,它每次都會創建新的標記。通過創建標記的NSMutableArray並管理該數組來解決此問題