iPhone MKMapView - MKPolygon问题

9

我正在尝试在iOS 4.0的MKMapView上绘制一个MKPolygon。我有一个包含自定义对象的NSArray,其中包括纬度/经度属性。我下面有一个代码示例:

- (void)viewDidLoad {
    [super viewDidLoad];
    dataController = [[DataController alloc] initWithMockData];
    coordinateData = [dataController getCordData];

    CLLocationCoordinate2D *coords = NULL;
    NSUInteger coordsLen = 0;

    /* How do we actually define an array of CLLocationCoordinate2d? */

    MKPolygon *polygon = [MKPolygon polygonWithCoordinates:coords count:coordsLen];
    [mapView addOverlay: polygon];

}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKPolygonView *polygonView = [[MKPolygonView alloc] initWithPolygon: routePolygon]; 
    NSLog(@"Attempting to add Overlay View");   
    return polygonView;
}

我理解的方式是:
  1. 我需要创建MKPolygon
  2. 将一个覆盖层添加到MapView中
  3. 这将触发MKPolygonView的创建。
我的问题是,如何将包含在NSArray(coordinateData)中的自定义对象转换为CLLocationCoordinate2d数组,以便多边形可以解释和呈现?我甚至不确定CLLocationCoordinate2d是一个数组?有人能够对此进行澄清吗?
4个回答

17

polygonWithCoordinates方法需要一个CLLocationCoordinate2D结构体的C数组。你可以使用malloc来为数组分配内存(并使用free释放内存)。遍历你的NSArray并将每个元素设置到结构体数组中。

例如:

coordsLen = [coordinateData count];
CLLocationCoordinate2D *coords = malloc(sizeof(CLLocationCoordinate2D) * coordsLen);
for (int i=0; i < coordsLen; i++)
{
    YourCustomObj *coordObj = (YourCustomObj *)[coordinateData objectAtIndex:i];
    coords[i] = CLLocationCoordinate2DMake(coordObj.latitude, coordObj.longitude);
}
MKPolygon *polygon = [MKPolygon polygonWithCoordinates:coords count:coordsLen];
free(coords);
[mapView addOverlay:polygon];

viewForOverlay方法应该像这样:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKPolygonView *polygonView = [[[MKPolygonView alloc] initWithPolygon:overlay] autorelease]; 
    polygonView.lineWidth = 1.0;
    polygonView.strokeColor = [UIColor redColor];
    polygonView.fillColor = [UIColor greenColor];
    return polygonView;
}

2

在iOS 7.0及以上版本中,我们应该使用MKPolygonRenderer代替MKPolygonView

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
   MKPolygonRenderer * polygonView = [[MKPolygonRenderer alloc] initWithPolygon:overlay];
   polygonView.fillColor   = [UIColor greenColor];
   polygonView.strokeColor = [UIColor redColor] ;
   polygonView.lineWidth   = 1.0;
   return polygonView;
}

1

Swift 4中的代码

用于处理JSON中的坐标:

{
"coordinates": [
    [-73.947676,40.660297],
    [-73.947264,40.656437],
    [-73.947159,40.655594],
    [-73.946479,40.6491],
    [-73.947467,40.649039]
}

读取坐标:

let coordinates = json["coordinates"] as! [[Double]] 

创建点数组:

var locationCoordinates = [CLLocationCoordinate2D]()    
for coordinate in coordinates{
   locationCoordinates.append(CLLocationCoordinate2DMake(coordinate.last!, coordinate.first!))
}

创建一个多边形并将其添加到地图中:
map.addOverlay(MKPolyline(coordinates: locationCoordinates, 
                                count: locationCoordinates.count))

请确保您的VC符合 MKMapViewDelegate
class ViewController: UIViewController, MKMapViewDelegate { ... }

并添加这个方法:

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if overlay is MKPolygon {
        let polygonView = MKPolygonRenderer(overlay: overlay)
        polygonView.fillColor = .black
        polygonView.strokeColor = .red
        polygonView.lineWidth = 2.0

        return polygonView

    return MKOverlayRenderer()
}

0

coordinatesArray; //您的包含坐标的数组

for (int i=0; i <[coordinatesArray count]; i++) 
{
   float latitude  = [coordinatesArray[i][@"latitude"] floatValue];
   float longitude  = [coordinatesArray[i][@"longitude"] floatValue];
   MKPolygon *polygon;
   CLLocationCoordinate2D coordinates[[coordinatesArray count]];
   coordinates[i] = CLLocationCoordinate2DMake(latitude , longitude);
   polygon = [MKPolygon polygonWithCoordinates:coordinates count:[coordinatesArray count]];
   [self.mapView addOverlay:polygon];
}

//你的“coordinatesArray”是一个包含多个纬度和经度键值对字典的数组。 //希望这能帮到你。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接