vkinra
vkinra

Reputation: 943

iOS Google Maps SDK replace marker icon

I have a project where I need to display some markers on the map based on a location array we get. We are using custom marker pins and not the default. One requirement is that if we zoom in beyond a certain zoom threshold, we need to display bigger icons for the markers. So we have a small pin image and a big pin image. Based on zoom level, we need to display either the bigger or the smaller. I understand one way is to clear all markers and re-add them. However, due to some other challenge, that solution doesn't work for us. I need to simply replace the marker icon image. Following is my code:

-(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;

}

}

However, the issue I'm facing is that both the marker icon images remain. How can I replace the marker image and not show both?

Upvotes: 1

Views: 2716

Answers (1)

vkinra
vkinra

Reputation: 943

Actually, I figured out what the issue was:

GMSMarker *marker = [GMSMarker markerWithPosition:markerCoordinate];

I missed that markerWithPosition is a constructor and not simply returning the marker at that position if it exists. As a result it was creating new markers each time. Solved it by creating a NSMutableArray of markers and managing that array

Upvotes: 1

Related Questions