geocodeAddressString

16 results found

Post not yet marked as solved

Post not yet marked as solved
0 Replies
447 Views
I run this code in viewDidLoad, but it doesn't even run the print(!!!) statement. What should I check? Instead of <address>, I have a real address. geocoder.geocodeAddressString(<address>) { [weak self] placemarks, error in print(!!!) guard !(error != nil) else { print(error) return } if let placemarks = placemarks, let placemark = placemarks.first { self!.church = placemark print(self!.church) } else { print(Church cannot be found.) } }
Posted
by
Post not yet marked as solved
1 Replies
970 Views
CLGeocoder's geocodeAddressString function always return an array with only one CLPlacemark object, independently of the string passed as a parameter. I'm using it like this: let placesArray = try await CLGeocoder().geocodeAddressString(addressString) CLGeocoder's documentation is clear: In the case of forward-geocoding requests, multiple placemark objects may be returned if the provided information yielded multiple possible locations. But that never happens. Even when passing something like San which should obvioulsy return an array with many CLPlacemark possibilities. I've seen this same question asked in multiple places (like here, here, here, and here) but I can't find an answer. Is this a bug? Am I doing something wrong? How can I obtain multiple possible locations from CLGeocoder? EDIT: MKLocalSearch I tried the same thing using MKLocalSearch: let searchRequest = MKLocalSearch.Request() searchRequest.naturalLanguageQuery = addressString let search = MKLocalSearch(request: searchRequest
Posted
by
Post not yet marked as solved
7 Replies
It's still not working in Beta 5. I've tried geocodeAddressString and geocodeAddressDictionary. If their completionHandlers return at all, the placemarks array will be nil.
Post marked as solved
4 Replies
Is it really how yoiu use it ? how i use it : getLat(withAddress: address1) { (result) in lat1 = result } Or is it getLatFromAddress(withAddress: address1) { (result) in lat1 = result } geocodeAddressString is async by nature, AFAIK you cannot change it. https://mhorga.org/2015/09/23/completion-handlers-in-ios.html So the ways to solve it: do as you have written: do the print (or any other code) in the completion handler. or use async wait pattern (declaring your func async)
Post not yet marked as solved
1 Replies
Replied In weatherKit
The WeatherKit API only needs you to provide a CLLocation object, nothing about the name of a city/country. You will have to use your own list of city/country names and get the locations of these to pass to WeatherKit. Helpfully, CLGeocoder has that functionality already. Here is an example of how to use it: func location(for address: String) async throws -> CLLocation? { let placemarks = try await CLGeocoder().geocodeAddressString(address) return placemarks.first?.location } if let londonLocation = try? await location(for: London) { // pass londonLocation to WeatherKit let coordinates = londonLocation.coordinate print(coordinates) // CLLocationCoordinate2D(latitude: 51.5033466, longitude: -0.0793965) }
Post not yet marked as solved
2 Replies
As already explained, `geocodeAddressString` and its closure will be called asynchronously.This sort of thing may happen even in C, when you pass callback to some asynchronous call.If you want to execute the callback closure strictly in order, you may need to start the next aync call after the current is finished.That can be a little bit tricky (for person who are not accustomed to it) and take more time to finish all, so please consider if you really need it.If you just want to do something after all async calls are finished, you can use DispatchGroup. let group = DispatchGroup() //<- for loc in arrayOfStrings ?? [] { print(Geocoder call:, loc) let geocoder = CLGeocoder() group.enter() //Call `enter()` before invoking each async call geocoder.geocodeAddressString(loc) { placemarks, error in //All UI manipulation should be done in the main thread. DispatchQueue.main.async { if let placemark = placemarks?.first, let location = placemark.location { let mark = MKPlacemark(placemark: placemark) if !fi
Post not yet marked as solved
1 Replies
The code is not executing serially because it involves completion handlers, which are inherently asynchronous . Your getLocation function only starts an asynchronous operation, but does not wait for it to finish. Your queue.sync calls here don't achieve anything here, because the calls to getLocation are already synchronous (only the completion handlers are executed asynchronously). So, you've effectively written: self.getLocation(from: self.sourceString) { … } self.getLocation(from: self.destinationString) { … } // I think you meant destination, not source here! if(/*some test*/) { /* some stuff */ } These things run in order, but the parts represented by { … } are completion handlers that run later — which is to say, after your if statement. If you want to get the destination location only after you have the source location, and test the coordinates after you have both locations, you'd need something like this: self.getLocation(from: self.sourceString) { … self.getLocation(from: self.destinationString) { …
Post not yet marked as solved
0 Replies
559 Views
In my application I am trying to use Geocoder, but only one value is always returned when requested. This cannot be, since many addresses must be displayed by the name of the street. I would like to know if there is a solution to this problem or should I use a different tool? Code CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressString:@Хользунова completionHandler:^(NSArray* placemarks, NSError* error) { if (error) { return; // TODO: handle error } NSLog(@Count placemarks: %@, @(placemarks.count)); for (CLPlacemark *placemark in placemarks) { NSLog(@Placemark: %@, placemark); } }];
Posted
by
Post not yet marked as solved
0 Replies
759 Views
I have written a program in SwiftUI to display information from a database that includes the street address, but not the latitude and longitude. I've added a button that passes the street address to geocodeAddressString to get the lat & long and updates the map, but every time I click the button, even though the map appears to be displayed properly, I get a message in Xcode complaining Modifying state during view update... My view has a @State variable that holds a MKCoordinateRegion() for the map to display. The closure on completion of the geocode lookup updates that @State. @State private var mapRegion = MKCoordinateRegion() private func updateAddress() { let index = data.zipCode.index(data.zipCode.startIndex, offsetBy: 5) geocoder.geocodeAddressString((data.streetAddress) (data.zipCode[..
Posted
by
Post not yet marked as solved
0 Replies
326 Views
Hello,I'm having a bug with the convertion of an NSString (which contains a place from a textfiel input) to a CLLocation.Here is the method supposed to process the convertion:- (void)geoCodeForAddress:(NSString *)address { CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressString:address completionHandler:^(NSArray* placemarks, NSError* error){ dispatch_async(dispatch_get_main_queue(), ^{ CLPlacemark *placeMark = [placemarks lastObject]; if (placeMark.location != nil) { self.actionLocation = placeMark.location; } else { self.actionLocation = nil; } if(self.event) [self.tableView reloadData]; }); }];}My problem is, this method returns the wrong location for some specific strings. For instance, whenever I try to use la patache paris as an input, it returns a location in the wrong city though when I try the same exact string in Maps, it's finding the right place.The method geocodeAddressString: completionHandler: is a CoreLocation methods from the CLGeocoder class so there is no w
Posted
by
Post not yet marked as solved
5 Replies
Hi thereHere is the complete View Controller page - which compilles in Swift 2import UIKit import MapKit import CoreLocation class ViewController: UIViewController { @IBOutlet weak var sourceField: UITextField! @IBOutlet weak var destinationField1: UITextField! @IBOutlet weak var destinationField2: UITextField! @IBOutlet weak var topMarginConstraint: NSLayoutConstraint! @IBOutlet var enterButtonArray: [UIButton]! var originalTopMargin: CGFloat! let locationManager = CLLocationManager() var locationTuples: [(textField: UITextField!, mapItem: MKMapItem?)]! var locationsArray: [(textField: UITextField!, mapItem: MKMapItem?)] { var filtered = locationTuples.filter({ $0.mapItem != nil }) filtered += [filtered.first!] return filtered } override func viewDidLoad() { super.viewDidLoad() originalTopMargin = topMarginConstraint.constant locationTuples = [(sourceField, nil), (destinationField1, nil), (destinationField2, nil)] locationManager.delegate = self locationManager.requestWhenInUseAuthorization() if CLLocationMa
Post not yet marked as solved
1 Replies
750 Views
I have been using CLGeocoder’s “geocodePostalAddress” API for many years, and it’s been working just fine. Since yesterday, a lot of users have started complaining that they are getting “no results” for their geocoding requests, and I have also confirmed this case. It seems like the “geocodePostalAddress” returns “No Result” in many cases where it used to work correctly before, even though the “geocodeAddressString” API still works correctly. Has anyone else noticed this problem? Has there been some server change that is causing this? Here's a unit test I wrote to test this out: import CoreLocation final class CJTestCLGeocoderRequests: XCTestCase { func testGeocoderWithCNPostalAddressBerkeley() async throws { let geocoder = CLGeocoder() do { let postalAddress = CNMutablePostalAddress() postalAddress.street = 2300 College Ave postalAddress.city = Berkeley postalAddress.state = CA postalAddress.postalCode = 94108 postalAddress.country = United States let placemarks3 = try await geocoder.geocodePostalAd
Posted
by
Post not yet marked as solved
1 Replies
897 Views
I want to read almost a million addresses in foor loop using CLGeocoder and save their coordinates. I have also put a 2 second pause in each loop, but every time I want to run the program in Xcode, after 200 or 300 iterations, the program closes suddenly. Does anyone know the reason? Is there a problem with the code? here is my code : func createCSV(from recArray:[Dictionary]) { var csvString = (Adresse);(latitute );(longitude)nn for dct in recArray { csvString = csvString.appending((String(describing:dct[Adresse]!));(String(describing: dct[latitute]!));(String(describing: dct[longitude]!))n) } let fileManager = FileManager.default do { let path = try fileManager.url(for: .documentDirectory, in: .allDomainsMask, appropriateFor: nil, create: false) let fileURL = path.appendingPathComponent(CSVRec.csv) try csvString.write(to: fileURL, atomically: true, encoding: .utf8) } catch { print(error creating file) } } func getLocation(for address: String) async throws -> CLLocationCoordinate2D { guard let coordinate
Posted
by