Change selected map marker or map marker color? [Ios]

I am wondering if there is a way to change the color or image of the selected marker, and then change it when it is no longer selected. I see that Yelp, which uses Apple Maps, changes the color / image of the selected marker, and then returns to the original as soon as it is no longer selected, and wondered if the Google Map iOS SDK had something similar or someone encountered this problem and found a solution.

What I tried:

I looked at the Google Marker Documentation ( found here ) and see what they have marker.opacitythat changes the opacity and marker.icon = [GMSMarker markerImageWithColor:[UIColor blackColor]];which changes the color of the marker.

I tried to manually change it in -(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker;by adding this line marker.icon = [GMSMarker markerImageWithColor: [UIColor differentColor]];or this line marker.icon = [UIImage imageNamed:@"differentColorImage"];, but when you exit the marker / info window, the image / color remains the same.

Anyone have any thoughts? Everything helps. Thanks in advance!

+4
source share
2 answers

, - , , . : GMSMarker *selectedMarker BOOL isMarkerActive. mapview:markerInfoWindow , , , , . , bool true, , .

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

highlightMarker ,

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

unhighlightMarker

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

, , bool 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;
        }
    }
}

, - .

+4

, , , ,   GMSMarker . didTapMarker:  

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

.

+6

Source: https://habr.com/ru/post/1528000/


All Articles