【问题标题】:Display multiple markers on the Google Map in Objective C在 Objective C 中的谷歌地图上显示多个标记
【发布时间】:2016-03-10 19:35:28
【问题描述】:

如何在 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];
}

【问题讨论】:

    标签: google-maps google-maps-mobile google-maps-sdk-ios


    【解决方案1】:

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

    另外,您可能不需要拨打animateToLocation

    【讨论】:

      【解决方案2】:

      我试过你的代码。这似乎工作正常。将值传递给 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];
      }
      

      【讨论】:

        【解决方案3】:

        试试这个:

        -(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】:

          是的,你们两个都是对的。根据您的建议,我更改了代码并且可以正常工作。 但问题是,我设置了缩放并且缩放是固定的。如果这两个位置很远,我无法在一个屏幕上看到这两个位置(我需要捏一下才能看到这两个位置)。如何同时查看两个位置? 我的代码如下所示。

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

          【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-11-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-01
          • 2016-02-04
          相关资源
          最近更新 更多