Reputation: 39
Can someone please help me in displaying GeoJSON
data received from an api endpoint on google maps iOS sdk ?.
I have been trying for almost a week now and have gone nowhere! I have used the GMUGeoJSONParser
function but the parameters it accepts are url
, inputstream
and data
. I am using Alamofire
to receive the data and then passing it in the GMUGeoJSONParser(data: jsonData)
but it is not displaying anything.
I have imported GoogleMapsUtils so that's not an issue.
Alamofire.request(url, method: .post, parameters: params,encoding: JSONEncoding.default,headers: headers).responseJSON{response in
if let result = response.data
{
let geoJSONParser = GMUGeoJSONParser(data: result)
geoJSONParser.parse()
let renderer = GMUGeometryRenderer(map:self.mapView, geometries: geoJSONParser.features)
renderer.render()
}
}
The geojson is a featureCollection and has been validated. It also works perfectly fine with the Google Maps JS Api on the web. I have tried converting the response into JSON via SwiftyJSON first
result = response.result.value
json = JSON(result)
but when i pass this json result into the parameter of GMUGeoJSONParser(data: json)
it gives an error "Cannot convert type json to argument type Data ". I have also tried converting the json result into type data like this
let jsonData : Data = try! JSONSerialization.data(withJSONObject:json, options: JSONSerialization.WritingOptions.fragmentsAllowed)
When doing this, the application crashes.
EDIT:
Here is a snippet of the GeoJSON response. The full response has over 1200 features.
"{\"type\": \"FeatureCollection\", \"features\": [{\"id\": 159, \"type\": \"Feature\", \"geometry\": {\"type\": \"MultiPolygon\", \"coordinates\": [[[[-88.548423248149, 39.7640682556336], [-88.5484140112235, 39.7634955634145], [-88.5577472392719, 39.7635270396728], [-88.5577514852793, 39.7635270624558], [-88.5577514785708, 39.7635283408508], [-88.5577534828958, 39.7649876367628], [-88.557753529907, 39.764989619059], [-88.5577431041359, 39.7649895725788], [-88.5484181305347, 39.7649561806335], [-88.548423248149, 39.7640682556336]]]]}, \"properties\": {\"name\": \"W&Vogel 1 (2100) 9\"}},
UPDATE:
let json = SwiftyJSON.JSON(result)
let fieldData: AnyObject? = json.string?.parseJSONString
let checker = JSONSerialization.isValidJSONObject(fieldData)
print(checker)
let jsonData : Data = try!
JSONSerialization.data(withJSONObject:fieldData, options:[])
let geoJSONParser = GMUGeoJSONParser(data: jsonData)
geoJSONParser.parse()
When i do this, the code throws an exception at geoJSONParser.parse(). Thread 1: Exception: "-[NSNull objectForKey:]: unrecognized selector sent to instance 0x7fff8062d9d0"
@Larme I added a break in the code for the exception and it lead me here
Upvotes: 0
Views: 1047
Reputation: 39
I did it ! After a lot of time wasting digging through the code it was actually an empty geometry array in the GeoJSON that was causing the issue. The table in which these fields were stored apparently allows empty geometry values and there was one field that was saved without the geometry. So although the json was valid, the GMUGeoJSONParser was throwing an error. Thanks to those who responded. Here is the final code in case it helps someone in the future.
Alamofire.request(url, method: .post, parameters: params, headers: headers).responseJSON{response in
if let result = response.result.value
{
let json = SwiftyJSON.JSON(result)
let fieldData: AnyObject? = json.string?.parseJSONString as! AnyObject
let jsonData : Data = try! JSONSerialization.data(withJSONObject:fieldData, options:[])
let geoJSONParser = GMUGeoJSONParser(data: jsonData)
geoJSONParser.parse()
let style = GMUStyle(styleID: "styleId", stroke: UIColor.black, fill: UIColor.yellow, width: 2, scale: 2, heading: 0, anchor: CGPoint(x: 0, y: 0), iconUrl: nil, title: nil, hasFill: true, hasStroke: true)
for feature in geoJSONParser.features{
feature.style = style
}
let renderer = GMUGeometryRenderer(map: self.mapView, geometries: geoJSONParser.features, styles: [style])
renderer.render()
}
}
Note: parseJSONString is an string extension that i found here Simple and clean way to convert JSON string to Object in Swift
Upvotes: 0