如何插入数组值以根据long / lat值绘制多边形 - Google Maps iOS?

时间:2017-03-15 12:05:51

标签: ios objective-c xcode google-maps

我正在尝试GMSPolygon * polygon = [GMSPolygon polygonWithPath:rect];用户绘制抽头坐标的多边形的方法。这是我存储点击坐标的方式:

// array made of clicked coordinates
NSMutableArray *latitudeTappedCoordinates = [NSMutableArray array];
NSMutableArray *longitudeTappedCoordinates = [NSMutableArray array];
NSUInteger numberOfLongitudeCoordinates = [longitudeTappedCoordinates count];
NSUInteger numberOfLatitudeCoordinates = [latitudeTappedCoordinates count];
for (int i = 2; i < numberOfLatitudeCoordinates; i++) {
    [latitudeTappedCoordinates addObject:[NSNumber numberWithInt:coordinate.latitude]];
}
for (int i = 2; i < numberOfLongitudeCoordinates; i++) {
    [longitudeTappedCoordinates addObject:[NSNumber numberWithInt:coordinate.longitude]];
}

在此之后,我有以下内容:

// polygon
GMSMutablePath *rect = [GMSMutablePath path];
[rect addCoordinate:CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude)];
GMSPolygon *polygon = [GMSPolygon polygonWithPath:rect];

如您所见,行

[rect addCoordinate:CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude)];

只接受一个属性。我希望它接受上面的数组init中的所有值,因此它可以绘制多边形。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

第一个存储浮点值不是数组中的int值。 然后

添加界面

 NSMutableArray *latitudeTappedCoordinates;
 NSMutableArray *longitudeTappedCoordinates;

你可以这样做:

// Create a rectangular path
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];
}

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;