1

How can I display multiple markers on the Google Map in iOS? I used the following approach, but it didn't work.

for (int i = 0; i < [array count]; i++)
{
     pointsToUse[i] = CLLocationCoordinate2DMake([[[[array objectAtIndex:0]  componentsSeparatedByString:@","] objectAtIndex:0] floatValue],[[[[array objectAtIndex:0]  componentsSeparatedByString:@","] objectAtIndex:1] floatValue]);
     [_map animateToLocation:pointsToUse[i]];
      GMSMarkerOptions *options = [[GMSMarkerOptions alloc] init];
      options.position = pointsToUse[i];
    [_map animateToLocation:pointsToUse[i]];
    [_map addMarkerWithOptions:options];
}
icebat
  • 4,696
  • 4
  • 22
  • 36
Bryanyan
  • 677
  • 3
  • 13
  • 30

4 Answers4

1

You're using [array objectAtIndex:0] (in two places), when I think you should probably be using [array objectAtIndex:i]?

Also, you probably don't need the calls to animateToLocation?

Saxon Druce
  • 17,406
  • 5
  • 50
  • 71
1

I tried your code. This seems to work fine. Just delete the object at index 0 after passing it values to pointsToUse.

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];
}
Manoj
  • 1,482
  • 2
  • 20
  • 53
1

Yes both of you are correct. According to your suggestions I changed the code and it works. But the problem is, I set the zoom and the zoom is fixed. If the two locations are far, I can't see both locations on one screen ( i need to pinch to see both). How can I see both locations at the same time? My code is shown below.

-(void) displayMapwithPositionfortheArray:(NSMutableArray*) array
{
    CLLocationCoordinate2D firstPoint = CLLocationCoordinate2DMake([[[[array objectAtIndex:0]  componentsSeparatedByString:@","] objectAtIndex:0] floatValue],[[[[array objectAtIndex:0]  componentsSeparatedByString:@","] objectAtIndex:1] floatValue]);
    GMSCameraPosition *currloc = [GMSCameraPosition cameraWithLatitude:firstPoint.latitude
                                                         longitude:firstPoint.longitude
                                                              zoom:8
                                                           bearing:0
                                                      viewingAngle:45];


    _map = [GMSMapView mapWithFrame:CGRectZero camera:currloc];
    _map.myLocationEnabled = YES;
    _map.frame = CGRectMake(0, heightOffset, self.view.frame.size.width, self.view.frame.size.height - heightOffset);
    [self.view addSubview:_map];

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

          GMSMarkerOptions *options = [[GMSMarkerOptions alloc] init];
          options.position = pointsToUse[i];
          [_map addMarkerWithOptions:options];
    }
}
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Bryanyan
  • 677
  • 3
  • 13
  • 30
  • Hi @Bryanyan, I only just saw this post. You can set up your camera to cover a bounding region (ie all your markers) using this code here: http://stackoverflow.com/questions/15040409/how-to-setregion-with-google-maps-sdk-for-ios – Saxon Druce Mar 22 '13 at 01:36
1

Try this:

-(void)plotMutliplePinsonMap
{
    mapView_ = [[GMSMapView alloc]initWithFrame:CGRectMake(0, 96, 320, 450)];
    for(int i=0;i<[arrTobeShown count];i++)
    {
        double_lat = [[[arrTobeShown objectAtIndex:i]valueForKey:@"latitude"] doubleValue];
        double_long = [[[arrTobeShown objectAtIndex:i]valueForKey:@"longitude"] doubleValue];
        GMSMarker *mkr = [[GMSMarker alloc] init];
        if (double_lat !=0 && double_long!=0) 
        {        
            [mkr setPosition:CLLocationCoordinate2DMake(double_lat, double_long)];
            [mkr setTitle:[[arrTobeShown objectAtIndex:i] valueForKey:@"name"]];
            [mkr setSnippet:[[arrTobeShown objectAtIndex:i] valueForKey:@"address"]];
            [mkr setMap:mapView_];

            GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:double_lat longitude:double_long zoom:5];
        mapView_.camera=camera;
        }
    }
    [self.view addSubview:mapView_];
    [mapView_ setHidden:YES];
    [self.view layoutIfNeeded];
}
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Hemant Singh Rathore
  • 2,153
  • 1
  • 24
  • 38