动态创建MKPolygon

时间:2014-01-30 12:14:29

标签: ios mkmapview mkpolygon

我正在使用MKPolygon在UIMapView上进行叠加。以下是我目前的代码:

    CLLocationCoordinate2D commuterLotCoords[5]={
    CLLocationCoordinate2DMake(39.048019,-76.850535),
    CLLocationCoordinate2DMake(39.048027,-76.850234),
    CLLocationCoordinate2DMake(39.047407,-76.850181),
    CLLocationCoordinate2DMake(39.047407,-76.8505),
    CLLocationCoordinate2DMake(39.048019,-76.850535)
};

MKPolygon *commuterPoly1 = [MKPolygon polygonWithCoordinates:commuterLotCoords count:5];
[commuterPoly1 setTitle:@"first"];
[self.overlayMap addOverlay:commuterPoly1];

现在,我正在实施json web服务,这将导致我所有的经度和纬度。但我无法从这些点创建叠加层。任何人都可以帮助我使用动态点实现叠加。

以下是我的服务回复:

 {
  response_code: 1,
  response_message: "successful",
  districts: [
              {
              district_id: "1",
              district_name: "Austin",
              Points: [
                      {
                         latitude: "39.048019",
                         longitude: "-76.850535"
                       },
                       {
                         latitude: "39.048027",
                         longitude: "-76.850234"
                       }
                       ]
             },
             {
              district_id: "2",
              district_name: "Tulsa",
              Points: [
                       {
                         latitude: "39.048019",
                         longitude: "-76.850535"
                       },
                       {
                         latitude: "39.048027",
                         longitude: "-76.850234"
                       },
                       {
                         latitude: "39.047407",
                         longitude: "-76.850181"
                       },
                       {
                         latitude: "39.047407",
                         longitude: "-76.8505"
                       },
                       {
                         latitude: "39.048019",
                         longitude: "-76.850535"
                       }
                      ]
               }
      ]
}

提前致谢

1 个答案:

答案 0 :(得分:2)

试试这个...从json创建字典并在此函数中传递它...它将返回MKPolygon

-(MKPolygon *)getPointsForDic:(NSMutableDictionary *)dic
{
    NSMutableSet *set = [[NSMutableSet alloc] init];
    NSMutableArray *arr = [dic valueForKey:@"districts"];

    for (int i = 0; i < arr.count; i++)
    {
        NSMutableDictionary *dicPoints = [arr objectAtIndex:i];
        [set addObjectsFromArray:[dicPoints valueForKey:@"Points"]];
    }

    int count = set.count;

    CLLocationCoordinate2D *coords = calloc(count, sizeof(CLLocationCoordinate2D));

    int i = 0;

    for (NSMutableDictionary *dicT in set)
    {
        CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([[dicT valueForKey:@"latitude"] floatValue], [[dicT valueForKey:@"longitude"] floatValue]);
        coords[coordIdx++] = coord;
        i++;
    }

    MKPolygon *commuterPoly1 = [MKPolygon polygonWithCoordinates:coords count:count];
    [commuterPoly1 setTitle:@"first"];

    return commuterPoly1;
}