Display multiple markers on the Google Map in Objective C

和自甴很熟 提交于 2019-12-12 09:22:28

问题


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

回答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?




回答2:


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



回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/15071209/display-multiple-markers-on-the-google-map-in-objective-c

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