【问题标题】:Google Map GMSPolygon is not updating in iOSGoogle Map GMSPolygon 未在 iOS 中更新
【发布时间】:2016-07-05 15:48:04
【问题描述】:

我在 GMSMapView 中添加了 GMSPolygon,当我单击按钮更新多边形的纬度和经度时,它根本没有更新。我只能看到旧的多边形。

这是我从 NSArray 获取坐标并显示多边形的代码,它运行良好。

-(void)loadAreasInMap{

    for(NSArray *polygon in [[vehicle model] area]){
        self.path = [GMSMutablePath path];

                for(int i = 0; i < [[polygon  valueForKey:@"polygon_coordinates"] count]; i++){

                    [self.path addCoordinate:CLLocationCoordinate2DMake([[[[polygon  valueForKey:@"polygon_coordinates"] objectAtIndex:i] objectAtIndex:1] floatValue],[[[[polygon  valueForKey:@"polygon_coordinates"] objectAtIndex:i] objectAtIndex:0] floatValue])];
                }

                GMSPolygon *rectangle = [GMSPolygon polygonWithPath:self.path];
                rectangle.fillColor = [UIColor colorWithRed:255.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.4];
                rectangle.map = self.mapView;

    }

    [self.mapView bringSubviewToFront:self.locationMarkerView];
    CLLocation *userLocation = [[locManager sharedManager] userLocation];
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:userLocation.coordinate.latitude
                                                            longitude:userLocation.coordinate.longitude
                                                                 zoom:7];
    self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];



}

现在,当我单击按钮时,我将调用此方法来更新我的坐标并重新绘制多边形,但我可以看到所有说明方法正常工作但地图未更新的日志。

 -(void)buttonClicked{

   [self.mapView clear];
   [self.loadAreasInMap];
 }

请告诉我如何更新地图

【问题讨论】:

    标签: ios objective-c google-maps google-maps-api-3 google-maps-markers


    【解决方案1】:

    如果您将多边形定义为实例变量,使用虚拟路径加载它并将 .map 属性设置为 nil 会怎样。然后,当您单击按钮时,使用新路径加载多边形并将地图属性设置为地图视图。

    GMSPolygon *rectangle;
    

    在你的初始化中的某个地方用一个虚拟路径填充它:

        GMSMutablePath *path = [[GMSMutablePath alloc] init];
        [path addCoordinate:CLLocationCoordinate2DMake(0.0,0.0)];
        [path addCoordinate:CLLocationCoordinate2DMake(0.1,0.0)];
        [path addCoordinate:CLLocationCoordinate2DMake(0.0,0.1)];
    
        rectangle = [GMSPolygon  polygonWithPath:path];
        rectangle.map = nil;
    

    然后在你的函数中,加载新的路径并设置rectangle.map = mapView;

    您不必为此清除整个地图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      相关资源
      最近更新 更多