Change Google Maps' Selected Marker or change marker's color? [iOS]

好久不见. 提交于 2019-11-29 00:37:44

To change icon of marker that selected and for not selected what i did was, First I add all the GMSMarker in an array.After that inside delegate function didTapMarker: I got selected marker and change the icon of that marker

     - (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker
       {
         marker.icon=[UIImage imageNamed:@"selectedicon.png"];//selected marker

           for (int i=0; i<[markerArray count]; i++) 
            {
             GMSMarker *unselectedMarker=markerArray[i];
        //check selected marker and unselected marker position
             if(unselectedMarker.position.latitude!=marker.position.latitude &&    unselectedMarker.position.longitude!=marker.position.longitude)
            {
                unselectedMarker.icon=[UIImage imageNamed:@"unselectedicon.png"];
            } 
          }


         return NO;
       }

This is working for me.

Just incase anyone comes and sees this, I resolved this issues by using my own method and own variables. I used two global variables which are: GMSMarker *selectedMarker and BOOL isMarkerActive. Inside of mapview:markerInfoWindow I check if the marker is active, if it is that means there was one active before this one so go unhighlight that marker. After that I set the current marker is the selected marker, set the bool to true and then highlight that marker, as shown below.

if(self.isMarkerActive == TRUE){
    [self unhighlightMarker:self.selectedMarker];
}
self.selectedMarker = marker;
self.isMarkerActive = TRUE;
[self highlightMarker:marker];

Inside the highlightMarker method I check if the sent marker I sent is equal to the map's selected marker

-(void)highlightMarker:(GMSMarker *)marker{
    if(self.mapView.selectedMarker isEqual:marker]){
        marker.icon = [UIImage imageNamed:@"marker-selected-icon"];
    }
}

Do the same in the unhighlightMarker method

-(void)unhighlightMarker:(GMSMarker* )marker{
    marker.icon = [UIImage imageNamed:@"marker-icon"];
}

Lastly, I check for taps on the map and see if the bool is true and that the map's selected marker does not equal nil

- (void)mapView:(GMSMapView *)amapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate{
    if(self.isMarkerActive == TRUE){
        if(amapView.selectedMarker != nil){
            self.isMarkerActive = FALSE;
            [self unhighlightMarker:self.selectedMarker];
            self.selectedMarker = nil;
            amapView.selectedMarker = nil;
        }
    }
}

Hope this helps anyone else out there.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!