Search code examples
iosobjective-cgoogle-mapsgoogle-maps-api-3gmsmapview

How plot new marker (with already plotted markers) on GMS map and set selected?


I'm using Google Maps iOS SDK and placed multiple markers with the marker info window opens when marker click. Now i want to add new marker which is not already plotted on map. I have list view with different places when user clicks on any place out side his local location, he will be jump to the selected location. Till this all working right but marker of place not displaying with error message:

Marker set as selectedMarker while not belonging to this map. Ignoring.

I'm re-plotting all the marker agin on selected location but its not working for me. Is there any way to plot single or multiple markers and set set selected marker default.

for (int i =0; i < [getdata count]; i++)
{
    LocationData *getlocation = [getdata objectAtIndex:i];
    CLLocationCoordinate2D position = CLLocationCoordinate2DMake(getlocation.lat, getlocation.longt);
    GMSMarker *marker = [GMSMarker markerWithPosition:position];
    marker.map = _mapView;
    marker.title = getlocation.name;
    marker.snippet = getlocation.disciption;
    marker.flat = YES;
    marker.icon = [UIImage imageNamed:img];
} 

Solution

  • You can trigger method on location selection to plat single new marker with default selected on map:

    For Obj c

    GMSMarker *myMarkerAutomaticSnippet = [[GMSMarker alloc] init];
    marker.position = <Your cordinates>;
    marker.title = @“Title";
    marker.snippet = @"Snippet";
    marker.map = _mapView; 
    [_mapView setSelectedMarker:marker];
    

    For Swift 3.0

    let myMarker = GMSMarker()
    myMarker.position = <Your cordinates>
    myMarker.title = "title"
    myMarker.snippet = "snippet"
    myMarker.map = _mapView    
    _mapView.customMapView.selectedMarker = myMarker