首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态创建MKPolygon

动态创建MKPolygon
EN

Stack Overflow用户
提问于 2014-01-30 12:14:28
回答 1查看 2K关注 0票数 1

我正在与MKPolygon一起工作,因为它在UIMapView上有覆盖。下面是我的当前代码:

代码语言:javascript
复制
    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服务,这将给我带来所有的经度和纬度。但我无法从这些点创建覆盖层。谁能帮我实现叠加使用动态点。

以下是我的服务回应:

代码语言:javascript
复制
 {
  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"
                       }
                      ]
               }
      ]
}

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-30 12:33:41

试试这个..。从json创建字典并在这个函数中传递它..。它会把MKPolygon还给你

代码语言:javascript
复制
-(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;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21456259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档