GMSPolygon在iOS Google地图应用中不起作用

时间:2017-03-15 07:00:07

标签: ios objective-c xcode google-maps

我有以下代码用于我的新iOS Google地图应用。当用户点击屏幕时,我可以在地图上创建动态标记,但不会绘制标记之间的多边形形状。任何想法为什么会发生这种情况?

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//Controls whether the My Location dot and accuracy circle is enabled.
CGFloat currentZoom = 14.0f;
self.mapView.myLocationEnabled = YES;

//adds type of map: kGMSTypeSatellite, kGMSTypeTerrain, kGMSTypeHybrid, kGMSTypeNormal
self.mapView.mapType = kGMSTypeHybrid;

//Shows the compass button on the map
self.mapView.settings.compassButton = YES;

//Shows the my location button on the map
self.mapView.settings.myLocationButton = YES;

//Sets the view controller to be the GMSMapView delegate
self.mapView.delegate = self;
GMSCameraPosition *manhattan = [GMSCameraPosition cameraWithLatitude:40.790278
                                                        longitude:-73.959722
                                                             zoom:14];
self.mapView.camera = manhattan;
}
 //setting up zoom
-(void)ZoominOutMap:(CGFloat)level
{
self.mapView.delegate = self;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40.790218 longitude:-73.959722
                                                             zoom:level];
self.mapView.camera = camera;
}

-(void)zoomInMapView:(id)sender
{
CGFloat currentZoom;
currentZoom = currentZoom + 1;

[self ZoominOutMap:currentZoom];
}

-(void) zoomOutMapView:(id)sender
{
CGFloat currentZoom;
currentZoom = currentZoom - 1;

[self ZoominOutMap:currentZoom];
}

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
NSLog(@"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
GMSMarker *marker = [[GMSMarker alloc] init];
//adds animation for adding marker
marker.appearAnimation = kGMSMarkerAnimationPop;
//draw marker on tapped position
marker.position = CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude);
marker.map = _mapView;
//set color of marker and make them draggable
marker.icon = [GMSMarker markerImageWithColor:[UIColor blueColor]];
[marker setDraggable: YES];

// Create a rectangular path
GMSMutablePath *rect = [GMSMutablePath path];
[rect addCoordinate:CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude)];

// Create the polygon, and assign it to the map.
GMSPolygon *polygon = [GMSPolygon polygonWithPath:rect];
polygon.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05];
polygon.strokeColor = [UIColor blackColor];
polygon.strokeWidth = 2;
polygon.map = _mapView;
}

2 个答案:

答案 0 :(得分:0)

在您的代码中,您不提供矩形路径。添加适当的多边形坐标以绘制GMSPolygon

// Create a rectangular path
GMSMutablePath *rect = [GMSMutablePath path];
[rect addCoordinate:CLLocationCoordinate2DMake(37.36, -122.0)];
[rect addCoordinate:CLLocationCoordinate2DMake(37.45, -122.0)];
[rect addCoordinate:CLLocationCoordinate2DMake(37.45, -122.2)];
[rect addCoordinate:CLLocationCoordinate2DMake(37.36, -122.2)];

// Create the polygon, and assign it to the map.
GMSPolygon *polygon = [GMSPolygon polygonWithPath:rect];
polygon.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05];
polygon.strokeColor = [UIColor blackColor];
polygon.strokeWidth = 2;
polygon.map = mapView;

更新: 回答How to insert array values to draw polygon based on long/lat values - Google Maps iOS?

答案 1 :(得分:0)

你必须从' didTapAtCoordinate'代码并将其粘贴到viewDidLoad

#GMSMutablePath *rect = [GMSMutablePath path];#

然后它会起作用