swift - Adding geojson layer to google map in iOS -


i writing first ios native app. trying load geojson layer onto google map in app (map comes google maps sdk) can't find way it. i'm proficient in google maps javascript api i'm sensing things in swift different.

how can load geojson layer onto map in native ios app?

as of today, have not see api can parse geojson google map shapes on ios. therefor have parse on own, parse array, loop through array each feature, each geometry, properties, , create shape based on feature type(point, line, polygon)

here sample mapbox draw line, have extend point , polygon.

// perform geojson parsing on background thread dispatch_queue_t backgroundqueue = dispatch_get_global_queue(dispatch_queue_priority_default, 0); dispatch_async(backgroundqueue, ^(void) {     // path example.geojson in app's bundle     nsstring *jsonpath = [[nsbundle mainbundle] pathforresource:@"example" oftype:@"geojson"];      // load , serialize geojson dictionary filled properly-typed objects     nsdictionary *jsondict = [nsjsonserialization jsonobjectwithdata:[[nsdata alloc] initwithcontentsoffile:jsonpath] options:0 error:nil];      // load `features` dictionary iteration     (nsdictionary *feature in jsondict[@"features"])     {         // our geojson has 1 feature: line string         if ([feature[@"geometry"][@"type"] isequaltostring:@"linestring"])         {             // raw array of coordinates our line             nsarray *rawcoordinates = feature[@"geometry"][@"coordinates"];             nsuinteger coordinatescount = rawcoordinates.count;              // create coordinates array, sized fit of coordinates in line.             // array hold formatted coordinates our mglpolyline.             cllocationcoordinate2d coordinates[coordinatescount];              // iterate on `rawcoordinates` once each coordinate on line             (nsuinteger index = 0; index < coordinatescount; index++)             {                 // individual coordinate index                 nsarray *point = [rawcoordinates objectatindex:index];                  // geojson "longitude, latitude" order, need opposite                 cllocationdegrees lat = [[point objectatindex:1] doublevalue];                 cllocationdegrees lng = [[point objectatindex:0] doublevalue];                 cllocationcoordinate2d coordinate = cllocationcoordinate2dmake(lat, lng);                  // add formatted coordinate final coordinates array @ same index                 coordinates[index] = coordinate;             }              // create our polyline formatted coordinates array             mglpolyline *polyline = [mglpolyline polylinewithcoordinates:coordinates count:coordinatescount];              // optionally set title of polyline, can used for:             //  - callout view             //  - object identification             // in case, set name included in geojson             polyline.title = feature[@"properties"][@"name"]; // "crema council crest"              // add polyline map, on main thread             // use weak reference self prevent retain cycle             __weak typeof(self) weakself = self;             dispatch_async(dispatch_get_main_queue(), ^(void)             {                 [weakself.mapview addannotation:polyline];             });         }     }  }); 

here swift code line, have expend point , polygon

 // parsing geojson can cpu intensive, on background thread     dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), {         // path example.geojson in app's bundle         let jsonpath = nsbundle.mainbundle().pathforresource("example", oftype: "geojson")         let jsondata = nsdata(contentsoffile: jsonpath!)          {             // load , serialize geojson dictionary filled properly-typed objects             if let jsondict = try nsjsonserialization.jsonobjectwithdata(jsondata!, options: []) as? nsdictionary {                  // load `features` array iteration                 if let features = jsondict["features"] as? nsarray {                     feature in features {                         if let feature = feature as? nsdictionary {                             if let geometry = feature["geometry"] as? nsdictionary {                                 if geometry["type"] as? string == "linestring" {                                     // create array hold formatted coordinates our line                                     var coordinates: [cllocationcoordinate2d] = []                                      if let locations = geometry["coordinates"] as? nsarray {                                         // iterate on line coordinates, stored in geojson many lng, lat arrays                                         location in locations {                                             // make cllocationcoordinate2d lat, lng                                             let coordinate = cllocationcoordinate2dmake(location[1].doublevalue, location[0].doublevalue)                                              // add coordinate coordinates array                                             coordinates.append(coordinate)                                         }                                     }                                      let line = mglpolyline(coordinates: &coordinates, count: uint(coordinates.count))                                      // optionally set title of polyline, can used for:                                     //  - callout view                                     //  - object identification                                     line.title = "crema council crest"                                      // add annotation on main thread                                     dispatch_async(dispatch_get_main_queue(), {                                         // unowned reference self prevent retain cycle                                         [unowned self] in                                         self.mapview.addannotation(line)                                     })                                 }                             }                         }                     }                 }             }         }         catch         {             print("geojson parsing failed")         }     }) 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -