简体   繁体   中英

Display multiple markers on the Google Map in Objective C

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];
}

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 ?

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];
}

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];
    }
}

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM