Forward Geocoding with Swift
I have made a simple LocationManager helper on github in which you can get the thing what you want.
There is a function getReverseGeoCodedLocation
in the library. Just input your address as a String and get the complete placemark and lat, longs.
Add the LocationManager.swift
file in your project
How to use it:-
LocationManager.sharedInstance.getReverseGeoCodedLocation(address: yourAddress, completionHandler: { (location:CLLocation?, placemark:CLPlacemark?, error:NSError?) in
if error != nil {
print((error?.localizedDescription)!)
return
}
if placemark == nil {
print("Location can't be fetched")
return
}
print(placemark?.addressDictionary?.description ?? "")
print((placemark?.location?.coordinate.latitude)!)
print((placemark?.location?.coordinate.longitude)!)
})
Internally it uses the geocodeAddressString
of CLGeocoder
Output
Note:- It is for Swift 3.0 and above
Google Maps Reverse Geocoding only returning APPROXIMATE location
Figured this out pretty quickly. It was because I was including the bounds
parameter in the request. Removing bounds
gives me highly accurate results. I am now only using bounds
in forward geocoding.
How to get world coordinate of a place by name or address
What you are looking for is called Forward Geocoding:
Forward geocoding converts location text into geographic coordinates, turning 2 Lincoln Memorial Circle NW into -77.050,38.889.
https://www.mapbox.com/api-documentation/#geocoding
Get Coordinates for given Location
There is a built in API for that.
For iOS 5 and newer you can use CLGeocoder and for older version you will need to use some extern libraries.
Just call
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler
where you want to get the location. It runs async and in the completionHandler
you will get an array of all possible placemarks for your address where the first one is the most accurate. As apple says:
For most geocoding requests, this array should contain only one
entry. However, forward-geocoding requests may return multiple
placemark objects in situations where the specified address could not
be resolved to a single location.
Off topic:
Am I the only one whos first thoughts about Oxford
was "What does he mean with this weird Hex value?"
Related Topics
Why Constant Constraints the Property from a Structure Instance But Not the Class Instance
Make a Swift Dictionary Where the Key Is "Type"
Swift - Extra Argument in Call
Constant Unassigned Optional Will Not Be Nil by Default
Swift Variable Name With ' (Backtick)
Deletable Table With Textfield on Swiftui
Delete Folder With Contents from Firebase Storage
How to Detect a Tap Gesture Location in Swiftui
Calculate Age from Birth Date Using Nsdatecomponents in Swift
Class Conforming to Protocol as Function Parameter in Swift
How to Create a Multi-Line Text Inside a Scrollview in Swiftui
Why Is an Observedobject Array Not Updated in My Swiftui Application
How Does String Substring Work in Swift