adding multiple pins on google map in ios

2019-02-19 12:03发布

问题:

I want to add multiple pins on a google map, while it is being loaded.

I have a list of Latitude and Longitude values of nearby locations. How can I show all these locations on the map with a pin. I am using Google SDK for iOS.

I am using the following code, but it didn't work for me.

NSMutableArray *array = [NSMutableArray arrayWithObjects:@"12.981902,80.266333",@"12.982902,80.266363", nil];

CLLocationCoordinate2D pointsToUse[5];

for (int i = 0; i < [array Size]; i++)
{
    pointsToUse[i] = CLLocationCoordinate2DMake([[[[array objectAtIndex:0]  componentsSeparatedByString:@","] objectAtIndex:0] floatValue],[[[[array objectAtIndex:0]  componentsSeparatedByString:@","] objectAtIndex:1] floatValue]);

    [array removeObjectAtIndex:0];

    GMSMarkerOptions *options = [[GMSMarkerOptions alloc] init];
    options.position = pointsToUse[i];
    [mapView_ animateToLocation:pointsToUse[i]];
    [mapView_ addMarkerWithOptions:options];
}

I have tried enough to search for it but there isn't enough documentation to answer my question.

Thanks in advance for the help.

回答1:

This works for me:

for(GMSMarker*marker in array)
    {
        GMSMarker *mkr= [[GMSMarker alloc]init];
        [mkr setPosition:CLLocationCoordinate2DMake(<coord>)];

        [mkr setAnimated:YES];
        [mkr setTitle:<Title>];
        [mkr setSnippet:<Snippet>];
        [mkr setMap:mapView_];
    }

Using latest Google Maps SDK.

Hope it helps



回答2:

self.view = mapView_;     
for(int i=0;i<[array count];i++)
{       
   GMSMarker *marker = [[GMSMarker alloc] init];
   marker.animated=YES;
   marker.position = CLLocationCoordinate2DMake(latitude,longitude);
   marker.title = @"name";
   marker.snippet = @"snippet";
   marker.map = mapView_;
}

This worked for me!



回答3:

For swift we can use

for i in 0..<array.count() {
    var marker = GMSMarker()
    marker.animated = true
    marker.position = CLLocationCoordinate2DMake(latitude, longitude)
    marker.title = "name"
    marker.snippet = "snippet"
    marker.map = mapView_
}