iosmkmapviewmkpolygon

Create MKPolygon dynamically


I am working with MKPolygon for having overlay on UIMapView. Below is my current code :

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

Now, I am implementing json web service which will result me all the longitudes and latitudes. But I am unable to create the overlay from those points. Can anybody please help me with implementing overlay using dynamic points.

Below is my service response :

 {
  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"
                       }
                      ]
               }
      ]
}

Thanks in advance


Solution

  • Try this... Create Dictionary from json and pass it in this function... it will return you 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;
    }