如何在iOS谷歌地图应用程序上定义闭合多边形?
我的应用程序目前允许用户点击任何地方并在那里绘制标记。我想一个封闭的多边形自动绘制时,有两个以上的标记。我如何做到这一点?第一个点和最后一个点应相互连接以创建闭合形状 到目前为止,我的代码绘制了多段线,但没有将其作为多边形闭合:如何在iOS谷歌地图应用程序上定义闭合多边形?,ios,objective-c,xcode,google-maps,Ios,Objective C,Xcode,Google Maps,我的应用程序目前允许用户点击任何地方并在那里绘制标记。我想一个封闭的多边形自动绘制时,有两个以上的标记。我如何做到这一点?第一个点和最后一个点应相互连接以创建闭合形状 到目前为止,我的代码绘制了多段线,但没有将其作为多边形闭合: // polyline setup -> array made of clicked coordinates [latitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.lat
// polyline setup -> array made of clicked coordinates
[latitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.latitude]];
[longitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.longitude]];
GMSMutablePath *rect = [GMSMutablePath path];
CLLocationCoordinate2D event;
for (int i = 0; i <= [longitudeTappedCoordinates count]-1; i++) {
event.latitude = [[latitudeTappedCoordinates objectAtIndex:i] floatValue];
event.longitude = [[longitudeTappedCoordinates objectAtIndex:i] floatValue];
[rect addCoordinate:event];
}
// draw polyline
GMSPolyline *polygon = [GMSPolyline polylineWithPath:rect];
//多段线设置->由单击的坐标组成的数组
[latitudeTappedCoordinates添加对象:[NSNumber numberWithFloat:坐标.纬度]];
[longitudeTappedCoordinates添加对象:[NSNumber numberWithFloat:坐标.经度]];
GMSMutablePath*rect=[GMSMutablePath];
CLLocationCoordinated2D事件;
对于(int i=0;i您是否尝试将第一个坐标添加为最后一个点
// polyline setup -> array made of clicked coordinates
[latitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.latitude]];
[longitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.longitude]];
GMSMutablePath *rect = [GMSMutablePath path];
CLLocationCoordinate2D event;
for (int i = 0; i <= [longitudeTappedCoordinates count]-1; i++) {
event.latitude = [[latitudeTappedCoordinates objectAtIndex:i] floatValue];
event.longitude = [[longitudeTappedCoordinates objectAtIndex:i] floatValue];
[rect addCoordinate:event];
}
// add the first point as the last point, to "close" the polygon
event.latitude = [[latitudeTappedCoordinates objectAtIndex:0] floatValue];
event.longitude = [[longitudeTappedCoordinates objectAtIndex:0] floatValue];
[rect addCoordinate:event];
// draw polyline
GMSPolyline *polygon = [GMSPolyline polylineWithPath:rect];
//多段线设置->由单击的坐标组成的数组
[latitudeTappedCoordinates添加对象:[NSNumber numberWithFloat:坐标.纬度]];
[longitudeTappedCoordinates添加对象:[NSNumber numberWithFloat:坐标.经度]];
GMSMutablePath*rect=[GMSMutablePath];
CLLocationCoordinated2D事件;
对于(int i=0;i
然后,您可以将多边形的填充颜色设置为清晰
- (void)mapView:(GMSMapView *)mapView
didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
if (latitudeTappedCoordinates == nil){
longitudeTappedCoordinates = [[NSMutableArray alloc] init];
latitudeTappedCoordinates = [[NSMutableArray alloc] init];
}
[latitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.latitude]];
[longitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.longitude]];
}
调用以下方法在地图上显示多边形:
-(void)drawPolygon {
if ([longitudeTappedCoordinates count]>2) {
GMSMutablePath *rect = [GMSMutablePath path];
CLLocationCoordinate2D event;
for (int i = 0; i <= [longitudeTappedCoordinates count]-1; i++) {
event.latitude = [[latitudeTappedCoordinates objectAtIndex:i] floatValue];
event.longitude = [[longitudeTappedCoordinates objectAtIndex:i] floatValue];
[rect addCoordinate:event];
}
event.latitude = [[latitudeTappedCoordinates objectAtIndex:0] floatValue];
event.longitude = [[longitudeTappedCoordinates objectAtIndex:0] floatValue];
[rect addCoordinate:event];
GMSPolygon *polygon = [[GMSPolygon alloc] init];
polygon.path = rect;
polygon.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.2f];
polygon.strokeColor = [UIColor blackColor];
polygon.strokeWidth = 2;
polygon.map = _mapView;
}
else {
NSLog(@"Tap again on Map");
}
}
-(空)绘图多边形{
如果([longitudeTappedCoordinates count]>2){
GMSMutablePath*rect=[GMSMutablePath];
CLLocationCoordinated2D事件;
对于(inti=0;i,在GMS多边形中,第一个点和最后一个点始终是连接的。您只需要以适当的顺序传递所有坐标
- (void)mapView:(GMSMapView *)mapView
didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
if (latitudeTappedCoordinates == nil){
longitudeTappedCoordinates = [[NSMutableArray alloc] init];
latitudeTappedCoordinates = [[NSMutableArray alloc] init];
}
[latitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.latitude]];
[longitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.longitude]];
if([longitudeTappedCoordinates count]>2)
{
[self drawPolygonOnMap]
}
}
然后更改polygon.fillColor以检查多边形是否闭合
-(void)drawPolygon {
GMSMutablePath *rect = [GMSMutablePath path];
CLLocationCoordinate2D event;
for (int i = 0; i <= [longitudeTappedCoordinates count]-1; i++) {
event.latitude = [[latitudeTappedCoordinates objectAtIndex:i] floatValue];
event.longitude = [[longitudeTappedCoordinates objectAtIndex:i] floatValue];
[rect addCoordinate:event];
}
event.latitude = [[latitudeTappedCoordinates objectAtIndex:0] floatValue];
event.longitude = [[longitudeTappedCoordinates objectAtIndex:0] floatValue];
[rect addCoordinate:event];
GMSPolygon *polygon = [[GMSPolygon alloc] init];
polygon.path = rect;
polygon.fillColor = [UIColor grayColor];
polygon.strokeColor = [UIColor blackColor];
polygon.strokeWidth = 2;
polygon.map = _mapView;
}
-(空)绘图多边形{
GMSMutablePath*rect=[GMSMutablePath];
CLLocationCoordinated2D事件;
对于(int i=0;i)这很酷,但是当它达到3个以上的坐标时:。我怎么能始终保持“关闭”?对不起,我想你已经在将坐标排序为非交叉模式。这是一个完全不同的问题。让我们来讨论一下。答案是“AND AND”在这个问题上,看起来是一个好的、明确的解决方案……你有解决方案吗?
-(void)drawPolygon {
GMSMutablePath *rect = [GMSMutablePath path];
CLLocationCoordinate2D event;
for (int i = 0; i <= [longitudeTappedCoordinates count]-1; i++) {
event.latitude = [[latitudeTappedCoordinates objectAtIndex:i] floatValue];
event.longitude = [[longitudeTappedCoordinates objectAtIndex:i] floatValue];
[rect addCoordinate:event];
}
event.latitude = [[latitudeTappedCoordinates objectAtIndex:0] floatValue];
event.longitude = [[longitudeTappedCoordinates objectAtIndex:0] floatValue];
[rect addCoordinate:event];
GMSPolygon *polygon = [[GMSPolygon alloc] init];
polygon.path = rect;
polygon.fillColor = [UIColor grayColor];
polygon.strokeColor = [UIColor blackColor];
polygon.strokeWidth = 2;
polygon.map = _mapView;
}