adding multiple pins on google map in ios

社会主义新天地 提交于 2020-01-29 23:37:45

问题


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_
}


来源:https://stackoverflow.com/questions/16191436/adding-multiple-pins-on-google-map-in-ios

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