我在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];
}
请告诉我如何更新地图
答案 0 :(得分:0)
如果将多边形定义为实例变量,使用虚拟路径加载多边形并将.map属性设置为nil,该怎么办?然后单击该按钮时,使用新路径加载多边形,并将map属性设置为地图视图。
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;
您无需清除整个地图。