-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeocoder.swift
54 lines (51 loc) · 2.25 KB
/
Geocoder.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
// GeoCoder.swift
// AuthDemo
//
// Created by N A Shashank on 12/5/17.
// Copyright © 2017 N A Shashank. All rights reserved.
//
import UIKit
import CoreLocation
class GeoCoder
{
//APPLE PROVIDED GEOCODER
class func locationFromString(str:String,callBack:@escaping (CLLocationCoordinate2D) -> Void,failureCallBack:@escaping ()->Void)
{
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(str) { (arrPlacemarks, error) in
guard let unwrappedPlacemarks = arrPlacemarks ,let placemark = unwrappedPlacemarks.first, error == nil else{
failureCallBack()
return
}
guard let location = placemark.location else{
failureCallBack()
return
}
callBack(location.coordinate)
}
}
//GOOGLE PROVIDED GEOCODER
class func geocodeAddress(address:String,successCallBack:@escaping (CLLocationCoordinate2D) -> Void,failureCallBack:@escaping ()->Void)
{
guard let encodedAddress = address.addingPercentEncoding(withAllowedCharacters: CharacterSet.alphanumerics) else{
return
}
let strUrl = "https://maps.googleapis.com/maps/api/geocode/json?address=\(encodedAddress)&key=ADD_UR_API_KEY_HERE"
guard let url = URL(string:strUrl) else{
return
}
URLSession.shared.dataTask(with: url) { (data, url, error) in
guard let unwrappedData = data ,let jsonReceived = try? JSONSerialization.jsonObject(with: unwrappedData, options: JSONSerialization.ReadingOptions.mutableContainers) ,let dictReceived = jsonReceived as? [String:Any] else{
failureCallBack()
return
}
guard let arrResults = dictReceived["results"] as? [[String:Any]],arrResults.count > 0, let dictGeometry = arrResults[0]["geometry"] as? [String:Any] , let dictLocation = dictGeometry["location"] as? [String:Any] , let lat = dictLocation["lat"] as? Double , let long = dictLocation["lng"] as? Double else{
failureCallBack()
return
}
let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
successCallBack(coordinate)
}.resume()
}
}