I am trying to draw a polygon on google map from a geo json file. i know how can i draw path from few coordinate but i am unable to draw this from google map
Here is my JSON file
{
"type": "Polygon",
"coordinates": [
[
[
90.35087585449219,
23.87767555995429
],
[
90.35293579101562,
23.877832529038052
],
[
90.45867919921875,
23.662449542924175
],
[
90.45455932617188,
23.66103447347277
],
[
90.35053253173828,
23.872966398925893
],
[
90.35018920898438,
23.87516402872517
],
[
90.35087585449219,
23.87767555995429
]
]
]
}
How can i draw polygon form this json ? i want it dynamic way not static way also for google map
Thanks
Try this way it may help :)
struct ResponseData: Decodable {
var coordinates: [coordinates]
var type : String
}
struct coordinates : Decodable {
var lng: String
var lat: String
}
Create 2 struct
@IBOutlet weak var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
let path = GMSMutablePath()
if let url = Bundle.main.url(forResource: "document", withExtension: "json") {
do {
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
let jsonData = try decoder.decode(ResponseData.self, from: data)
for te in jsonData.coordinates {
path.add(CLLocationCoordinate2D(latitude: te.lat.toDouble(), longitude: te.lng.toDouble()))
}
}
catch {
//Handle error
print(error.localizedDescription)
}
}
let rectangle = GMSPolyline(path: path)
rectangle.strokeColor = .red
rectangle.strokeWidth = 5.0
rectangle.map = mapView
}