Swift cannot use found values out of reverseGeocodeLocation() - swift

I tried to store the city found by the reverseGeocodeLocation method inside a variable but it seems that it's not possible with the following code to reuse it inside a variable decelerated before. Debugger showed me that a city is found and is stored inside returnCity but only during execution of the reverseGeocodeLocation method.
I found some other posts but none of them helped me with my problem.
Thank you for any help!
// Get City out of coordinates
var returnCity = ""
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: self.selectedLat, longitude: self.selectedLng)
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[0]
// City
if let foundCity = placeMark.locality {
returnCity = foundCity
}
})
NSLog("Sub Locality is: " + returnCity) // console shows "Sub Locality is: "

You need to use property observer to update your UI or use the updated result. I would modify "returnCity" property like below and use the data as soon as it is set and every time it is changed inside a didSet observer.
var returnCity:String? {
didSet {
guard let returnCity = self.returnCity else {return}
NSLog("Sub Locality is: " + returnCity)
}
}

Related

Get the location type (e.g. Hotel or Restaurant)

I want to get the type of place that the user is at, like if they are at a restaurant or at a hotel. I have tried to look into that and no one seems to have an answer. Even when printing the full description of the location it doesn't show that.
I finally found a solution to this answer. I just used the Yelp API and it gave me more than enough information. This is what I used...
https://medium.com/#khansaryan/yelp-fusion-api-integration-af50dd186a6e
Apple is providing this:
func getLocationAddress(completion: #escaping ([String]) -> Void) {
self.getLocation { locations in
let place = CLLocation(latitude: locations[0].latitude, longitude: locations[0].longitude)
CLGeocoder().reverseGeocodeLocation(place) { placemarks, error in
guard let placemark = placemarks?.first else {
let errorString = error?.localizedDescription ?? "Unexpected Error"
print("Unable to reverse geocode the given location. Error: \(errorString)")
return
}
var output: [String] = []
let reversedGeoLocation = ReversedGeoLocation(with: placemark)
output.append(reversedGeoLocation.formattedAddress)
completion(output)
}
}
}
Also, you should include MapKit
Here's a link!

Trying to assign a value to a global variable inside a method with Guards & IFs

I'm still learning to code so please don't shoot me for asking a question. I have tried to find an answer however I haven't found anything on stack overflow that can help, nor within a couple of books I have...
I am using Xcode 11.3. I am trying to copy an address variable from inside a reverse geolocation function to a global variable. However, because the function uses guard statements and nested if's, Xcode wants me to place 'self' infront of my syntax when I try and assign the address. This is fine in that it works, however when the method has finished the global variable is empty and it seems that the address value is only held whilst that method/procedure is executing.
Is there anyway to get the data into a 'global variable' and not to some instance running inside the method? I have added comments at the base of the code where I am trying to give my global variable the contents of the address location (street number, street name and suburb).
My function (method):
// Uses MapKit Delegate
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
//let center = getCenterLocation(for: theMapView)
let center = getCenterLocation(for: theMapView)
let geoCoder = CLGeocoder()
guard let previousLocation = self.previousLocation else { return }
guard center.distance(from: previousLocation) > 100 else { return }
self.previousLocation = center
// The next little bit is mainly error checking stuff
geoCoder.reverseGeocodeLocation(center) { [weak self] (placemarks, error) in
guard let self = self else { return }
if let _ = error {
//TODO: Show alert informing the user
self.myAlertToolbar()
return
}
guard let placemark = placemarks?.first else {
//TODO: Show an alert to the user
self.myAlertToolbar()
return
}
let streetNumber = placemark.subThoroughfare ?? ""
let streetName = placemark.thoroughfare ?? ""
let suburb = placemark.locality ?? ""
if streetName == "" {
DispatchQueue.main.async {
self.addressLabelOutlet.text = "\(suburb)"
}
} else {
DispatchQueue.main.async {
// I want to be able to assign the streetNumber streetName and suburb over to a
// global variable without using self.blahblahblah
self.addressLabelOutlet.text = "\(streetNumber) \(streetName), \(suburb)"
self.myGlobalAddressVariable = String(streetNumber) + streetName + ", " + suburb
print("Address is \(self.myGlobalAddressVariable)")
}
}
}
}

Retrieve first 'mapItem' element from MKLocalSearch object in swift

first I want to say I'm new to the swift language.
My question almost mirrors this question: Accessing MKLocalSearchResponse item (swift)
However, when I apply this to my similar looking code I get an error "Value of type 'MKLocalSearch' has no member 'mapItems'"
Like in the link above I want the first mapItems (mapItems[0]) result.
Can anybody help me?
Heres my code:
let latitude = String(currentLocation.coordinate.latitude)
let longitude = String(currentLocation.coordinate.longitude)
var station1Unwrapped: String! = ""
var station2Unwrapped: String! = ""
var coord: CLLocationCoordinate2D!
coord = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
var region: MKCoordinateRegion!
region = MKCoordinateRegion(center: coord, latitudinalMeters: 100, longitudinalMeters: 100);
let request = MKLocalSearch.Request()
request.naturalLanguageQuery = "Train Station"
request.region = region
let search = MKLocalSearch(request: request)
search.start { response, error in
guard let response = response else {
print("There was an error searching for: \(String(describing: request.naturalLanguageQuery)) error: \(String(describing: error))")
return
}
print("Inside function")
let station1 = response.mapItems[0].name
}
var newLocVar = (search.mapItems[0] as! MKMapItem).name
print(newLocVar)
The variable search is MKLocalSearch, so it doesn't has property mapItems. If you want to print the MKMapItem's name, you should access the mapItems in the completion block, where you get access to the response which is MKLocalSearch.Response. The line you write let station1 = response.mapItems[0].name is perfectly correct and it contains the name of the first mapItems found

How To Call a func within a Closure

In a model's class Location, I get the name of the current city:
var currentLatitude: Double!
var currentLongitude: Double!
var currentLocation: String!
var currentCity: String!
func getLocationName() {
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: currentLatitude, longitude: currentLongitude)
geoCoder.reverseGeocodeLocation(location, completionHandler: { placemarks, error in
guard let addressDict = placemarks?[0].addressDictionary else {
return
}
if let city = addressDict["City"] as? String {
self.currentCity = city
print(city)
}
if let zip = addressDict["ZIP"] as? String {
print(zip)
}
if let country = addressDict["Country"] as? String {
print(country)
}
self.nowUpdateUI()
})
}
In view controller I want to update the UI and update my label to show the current city.
However, self.currentCity = city happens inside of a closure. So if I just run a func in view controller:
func updateUI() {
cityLbl.text = Location.sharedInstance.currentCity
}
I'm not getting anywhere because the closure haven't finished running.
I've been advised to add a completion handler to getLocationName() and inside of it, perform the call to a func that will update the UI.
However, from all the tutorials out there on closures, completion handlers, it is not clear to me how to achieve that.
How to construct a completion handler, pass it as an arg to getLocationName() and how to call getLocationName from view controller?
To handle this situation you have multiple option.
Create delegate/protocol with your Location class
Create one protocol and implement that protocol method with your ViewController and declare its instance in your Location class. After then in the completionHandler of reverseGeocodeLocation call this delegate method. Check Apple documentation on Protocol for more details.
You can create completionHandler with your getLocationName method of Location class.
Add completionHandler with getLocationName and called that completionHandler inside the completionHandler of reverseGeocodeLocation like this way.
func getLocationName(completionHandler: #escaping (_ success: Bool) -> Void) {
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: currentLatitude, longitude: currentLongitude)
geoCoder.reverseGeocodeLocation(location, completionHandler: { placemarks, error in
guard let addressDict = placemarks?[0].addressDictionary else {
completionHandler(false)
return
}
if let city = addressDict["City"] as? String {
self.currentCity = city
print(city)
}
if let zip = addressDict["ZIP"] as? String {
print(zip)
}
if let country = addressDict["Country"] as? String {
print(country)
}
completionHandler(true)
//self.nowUpdateUI()
})
}
Now in ViewController where you are calling this function call your updateUI method inside the completion block.
Location.sharedInstance.getLocationName { (success) in
if success {//If successfully got response
self.updateUI()
}
}
You can add observer for (NS)NotificationCenter.
Register the observer with (NS)NotificationCenter and then post the notification inside the completionHandler of reverseGeocodeLocation. You can get more detail on this with this StackOverflow Post.
// I thing issue back ground thread you need to update your UI in main thread
var currentLatitude: Double!
var currentLongitude: Double!
var currentLocation: String!
var currentCity: String!
func getLocationName() {
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: currentLatitude, longitude: currentLongitude)
geoCoder.reverseGeocodeLocation(location, completionHandler: { placemarks, error in
guard let addressDict = placemarks?[0].addressDictionary else {
return
}
if let city = addressDict["City"] as? String {
self.currentCity = city
print(city)
}
if let zip = addressDict["ZIP"] as? String {
print(zip)
}
if let country = addressDict["Country"] as? String {
print(country)
}
DispatchQueue.main.async {
self.nowUpdateUI()
// Update your UI in main thread
}
})
}
This entire piece of your code:
completionHandler: { placemarks, error in
guard let addressDict = placemarks?[0].addressDictionary else {
return
}
if let city = addressDict["City"] as? String {
self.currentCity = city
print(city)
}
if let zip = addressDict["ZIP"] as? String {
print(zip)
}
if let country = addressDict["Country"] as? String {
print(country)
}
self.nowUpdateUI()
}
)
is already happening in the completionHandler (which happens after everything is finished) Just also run your updateUI() inside the completionHandler. So your end code would be :
completionHandler: { placemarks, error in
guard let addressDict = placemarks?[0].addressDictionary else {
return
}
if let city = addressDict["City"] as? String {
self.currentCity = city
DispatchQueue.main.async {
updateUI()
}
}
if let zip = addressDict["ZIP"] as? String {
print(zip)
}
if let country = addressDict["Country"] as? String {
print(country)
}
self.nowUpdateUI()
}
)
The reason you have to use DispatchQueue.main is because your completionHandler is on a backgroundqueue but you MUST always do you UI related stuff from your mainQueue—so users get the fastest changing in their UI without any glitches. Imagine if you were doing on a background thread and it was happening slow

Swift Reverse Geocoding using the Data

I am successfully getting the current address details based on my location. It printlns perfectly. What is throwing me is how I extract the data from this call. I have tried passing, say the ZIP/Postcode, as local and even global variables but with no joy. The data only seems to exist within this call. How can I use it elsewhere?
// Get Address Information
let geoCoder = CLGeocoder()
let newLocation = CLLocation(latitude: valueLatitude, longitude: valueLongitude)
geoCoder.reverseGeocodeLocation(newLocation, completionHandler: {(placemarks: [AnyObject]!, error: NSError!) in
if error != nil {
println("Geocode failed with error: \(error.localizedDescription)")
}
if placemarks.count > 0 {
let placemark = placemarks[0] as! CLPlacemark
let addressDictionary = placemark.addressDictionary
let address = addressDictionary[kABPersonAddressStreetKey] as! NSString
let city = addressDictionary[kABPersonAddressCityKey] as! NSString
let state = addressDictionary[kABPersonAddressStateKey] as! NSString
let postcode = addressDictionary[kABPersonAddressZIPKey] as! NSString
let country = addressDictionary[kABPersonAddressCountryKey] as! NSString
println("\(address) \(city) \(state) \(postcode) \(country)") }
})
Your problem is most likely due to the fact that reverseGeocodeLocation is an asynchronous request made to Apple servers.
What needs to happen is:
You call reverseGeocodeLocation
reverseGeocodeLocation finishes, starts its completion which calls a method passing the placemark you just recovered.
In order to do that:
#IBAction func btnInsertClicked(sender: AnyObject) {
var locationRecord: LocationRecord = LocationRecord()
// Get Address Information
let geoCoder = CLGeocoder()
let newLocation = CLLocation(latitude: valueLatitude, longitude: valueLongitude)
geoCoder.reverseGeocodeLocation(newLocation, completionHandler:
{(placemarks: [AnyObject]!, error: NSError!) in
if error != nil {
println("Geocode failed with error: (error.localizedDescription)")
}
if placemarks.count > 0 {
let myPlacemark = placemarks[0] as! CLPlacemark
// Here call the method that uses myPlacemark
self.myAwesomeMethod(placemarks[0] as! CLPlacemark)
} else {
println("No placemark")
}
})
}
Where you need to use it in your code:
func myAwesomeMethod(placemark: CLPlacemark) {
// Do stuff with placemark
}
I won't have my mac until tonight but if that doesn't work, leave a comment and we will work this out