【问题标题】:iPhone MKMapView - MKPolygon IssuesiPhone MKMapView - MKPolygon 问题
【发布时间】:2011-07-25 08:42:13
【问题描述】:

我正在尝试在 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 数组,以便 Polygon 可以解释和渲染?我不确定 CLLocationCoordinate2d 甚至是一个数组吗?有人可以澄清一下吗?

【问题讨论】:

    标签: ios objective-c iphone mkmapview mkpolygon


    【解决方案1】:

    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;
      }
      

      【讨论】:

        【解决方案3】:

        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()
        }
        

        【讨论】:

          【解决方案4】:

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

          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”是一个数组,其中包含具有多个纬度和经度键值的字典。 //希望对你有帮助。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-29
            相关资源
            最近更新 更多