繁体   English   中英

在Objective C中的Google Map上显示多个标记

[英]Display multiple markers on the Google Map in Objective C

如何在iOS中的Google地图上显示多个标记? 我使用了以下方法,但它没有用。

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

你正在使用[array objectAtIndex:0] (在两个地方),当我认为你应该使用[array objectAtIndex:i]

此外,您可能不需要调用animateToLocation

我试过你的代码。 这似乎工作正常。 只需在将值传递给pointsToUse后删除索引0处的对象。

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

是的,你们两个都是正确的。 根据你的建议,我改变了代码,它的工作原理。 但问题是,我设置了缩放并且缩放是固定的。 如果这两个位置很远,我在一个屏幕上看不到两个位置(我需要捏两个看)。 我怎样才能同时看到这两个位置? 我的代码如下所示。

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

尝试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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