Search code examples
iosobjective-cgoogle-maps-markersgoogle-maps-sdk-ios

Unable to display 2 markers on google maps in objective C ios


I'm unable to display two markers on the google maps in objective C. I'm trying to pass two values to it. I realise it might just be getting overwritten but I haven't been able to fix it yet. Please help. Here's my code from .m file:

GMSMarker *marker = [[GMSMarker alloc] init];

marker.position=CLLocationCoordinate2DMake(toLatitudeDouble, toLongitudeDouble);
marker.title=toLocationFromResultVC;
marker.snippet=@"Destination";



marker.position=CLLocationCoordinate2DMake(fromLatitudeDouble, fromLongitudeDouble);
marker.title=fromLocationFromResultVC;
marker.snippet=@"Source";

marker.map = mapView_;

Do I need to provide any more details? I'd be glad for any help I can get. Thanks a lot!


Solution

  • Yes, you're overriding the value. You should create another marker instead.

    GMSMarker *marker = [[GMSMarker alloc] init];
    
    marker.position=CLLocationCoordinate2DMake(toLatitudeDouble, toLongitudeDouble);
    marker.title=toLocationFromResultVC; marker.snippet=@"Destination";
    
    // Your're overriding the value here!!
    // use GMSMarker *marker2 = [[GMSMarker alloc] init]; and reference it instead in the code below.
    
    marker.position=CLLocationCoordinate2DMake(fromLatitudeDouble, fromLongitudeDouble); 
    marker.title=fromLocationFromResultVC; marker.snippet=@"Source";
    
    marker.map = mapView_;
    

    So, the correct code would be

    GMSMarker *marker = [[GMSMarker alloc] init];
    
    marker.position=CLLocationCoordinate2DMake(toLatitudeDouble, toLongitudeDouble);
    marker.title=toLocationFromResultVC; marker.snippet=@"Destination";
    marker.map = mapView_;
    
    GMSMarker *marker2 = [[GMSMarker alloc] init];
    marker2.position=CLLocationCoordinate2DMake(fromLatitudeDouble, fromLongitudeDouble); 
    marker2.title=fromLocationFromResultVC; marker.snippet=@"Source";
    marker2.map = mapView_;