the latest Xcode/SDK iOS download is no longer providing the 'country' string.
- (void)GEOLocator
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *myLocation = [[CLLocation alloc] initWithLatitude:currentCentre.latitude longitude:currentCentre.longitude];
[geocoder reverseGeocodeLocation:myLocation completionHandler:^(NSArray *placemarks, NSError *error)
{
// the returned error code is 0
NSLog(#"%ld",(long)error.code);
// there’s only one entry in the placemarks NSArray
NSLog(#"placemarks count(%lu)",(unsigned long)[placemarks count]);
CLPlacemark *placemark = [placemarks firstObject];
// and the country property is null
NSLog(#"placemark.country(%#)",placemark.country);
}
];
}
currentCentre.latitude and currentCentre.longitude is hardcoded to downtown San Francisco: these have been proven to work.
the NSLog output’s are:
2015-08-29 15:47:48.299 MyApp[10128:548448] 0
2015-08-29 15:47:48.299 MyApp[10128:548448] placemarks count(1)
2015-08-29 15:47:48.300 MyApp[10128:548448] placemark.country((null))
this code sequence is about as simple as it gets, yet the latest Xcode/iOS download no longer can tell me what country I’m in?!
if this isn't correct can someone please post the correct way to retrieve 'county' from CLLocation?
the core problem is that in Xcode/Simulator you can set lat/long values in multiple places begging the question: what happens when these settings conflict?
the simulator had me out in the middle of the North Atlantic! which explains why the 'country' (as well as several others) were null. the difficulty lie in setting a test lat/long value and the fact one can do that in multiple places: 1) Edit Scheme, 2) the Simulator Loction menu item, and 3) directly in source.
it would be nice to set test lat/long's in one place and everything then just works.
Related
I am trying to get user's region code in swift 3 by using:
Locale.current.regionCode
Unfortunately regionCode is nil, do you have an idea why?
And what should I do to obtain it?
Thanks a lot.
For those looking for solutions at SIMULATOR, go to "Settings > General > Language & Region" and change the region.
It worked for me and other people.
For a weird unknown reason, some simulators doesn't return the region until it changes at least once.
I don't know if it works at real device as well, because I did not had this problem on real device.
I ran into the same issue and as it turns out it was because I set my Scheme to a specific language. If you set both, Application Language & Application Region to "System Language / Region" it should work.
Many of the properties on Locale can return nil for various reasons on iOS (and Android).
You may also use objectForKey:NSLocaleCountryCode to query the country code.
// code may be nil
NSString *code = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode];
It's a good idea to have a fallback logic to find countryCode with CoreTelephony.
CTCarrier *carrier = [[CTTelephonyNetworkInfo new] subscriberCellularProvider];
NSString *countryCode = carrier.isoCountryCode;
Or/And with reverse geocode:
// CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
__block CLLocation *location = [locations firstObject];
[[[CLGeocoder alloc] init] reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (!error && placemarks.count > 0) {
CLPlacemark *placemark = [placemarks firstObject];
// Use placemark.country;
// Use placemark.ISOCountryCode;
}
}
}
For instance, on Android version of corresponding region query for Locale, it's nil for many of the rooted devices.
The documentation for regionCode states
The region code of the locale, or nil if it has none.
Some locales simply do not have a region (aka country) code. However I don't know which ones do not.
I have a MKMapView and want to grab the cities name based on the center of the MKMapView. I do not want to drop any pins or annotations at all. Just automatically grab the data based on center of MKMapView location. I know there is this method '[placemarkName locality]'. But is there a way without using a placemark?
You could just do a reverseGeocodeLocation (effective iOS 5+) from the centerCoordinate of the map view. For example, I could do something like:
CLLocationCoordinate2D center = self.mapView.centerCoordinate;
CLGeocoder *coder = [[CLGeocoder alloc] init];
[coder reverseGeocodeLocation:[[CLLocation alloc] initWithLatitude:center.latitude longitude:center.longitude] completionHandler:^(NSArray *placemarks, NSError *error) {
self.cityLabel.text = [[placemarks firstObject] locality];
}];
This admittedly, returns an array of placemarks, but you don't have to do anything with them after you extract the city's name.
At the moment I'm working on an application for the iPad, which at a certain point should show a list of cities with next to these cities the distance from the users current location.
I'm using a loop which uses a database I built earlier. This is the code:
NSString *address = [NSString stringWithFormat:#"%#, Nederland", [info2 objectForKey:#"stadsnaam"]];
[self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *location = placemark.location;
CLLocationCoordinate2D firstLocation = location.coordinate;
NSLog(#"%f,%f", firstLocation.latitude, firstLocation.longitude);
}];
This piece of code is inside a for loop. But the first time it is run, it just returns 0.0000
The second time the loop is activated (and I'm sure it runs more than one time) it returns only the distance between the current location and the FIRST city.
The app gets the city names from this piece of code:[NSString stringWithFormat:#"%#, Nederland", [info2 objectForKey:#"stadsnaam"]]
Which I verified and works. I would like to avoid using the Google API because I will be making more than the maximum request a day probably XD
Any help is appreciated, please inform me if anything isn't completely clear and thanks in advance!
You need to read the documentation for -geocodeAddressString:completionHandler::
After initiating a forward-geocoding request, do not attempt to initiate another forward- or reverse-geocoding request.
You can use CLGeocoder's geocoding property to determine whether a geocoding operation is in progress.
I want to get the location by the CLLocationCoordinate2D and I used the following code
//newLocation is a CLLocationCoordinate2D object
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:newLocation completionHandler:
^(NSArray *placemarks, NSError *error){
CLPlacemark *placemark = [placemarks objectAtIndex:0];
self.locationInput.text =placemark.subLocality;
self.locationInput.text =placemark.ISOcountryCode;
}];
but the returned placemarks is nil and the error description is kCLErrorDomain error =8
The return values are documented here.
https://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CoreLocationConstantsRef/Reference/reference.html#//apple_ref/doc/uid/TP40010237-CH2-SW2
Number 8 is "kCLErrorGeocodeFoundNoResult", so I suspect you're searching for something with no results. I've read elsewhere that CLGeocoder only runs on a device (not the simulator) but I haven't verified it myself. If you are sure your search string should get results, try your code on a device.
My issue was that I was using a VPN connected to a Thai server, trying to do a reverseGeocodeLocation on a UK postcode.
When I switched the VPN to a UK server it worked fine.
I'm developing an iOS app with reverse geocoding features according to this article: geocoding tutorial
But when I test like this on simulator, I get 'kCLErrorDomain error 9'. I've searched a lot and there are only error 0 or 1 not 9.
Here is my code in viewDidLoad:
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = 80.0;
[self.locationManager startUpdatingLocation];
CLGeocoder *geocoder = [[[CLGeocoder alloc] init] autorelease];
[geocoder reverseGeocodeLocation:self.locationManager.location
completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(#"reverseGeocodeLocation:completionHandler: Completion Handler called!");
if (error){
NSLog(#"Geocode failed with error: %#", error);
return;
}
if(placemarks && placemarks.count > 0)
{
//do something
CLPlacemark *topResult = [placemarks objectAtIndex:0];
NSString *addressTxt = [NSString stringWithFormat:#"%# %#,%# %#",
[topResult subThoroughfare],[topResult thoroughfare],
[topResult locality], [topResult administrativeArea]];
NSLog(#"%#",addressTxt);
}
}];
Thank you very much.
The Core Location error codes are documented here.
Code values 8, 9, and 10 are:
kCLErrorGeocodeFoundNoResult,
kCLErrorGeocodeFoundPartialResult,
kCLErrorGeocodeCanceled
Based on the code shown, the most likely reason you'd get error 8 is that it's trying to use location immediately after calling startUpdatingLocation at which time it might still be nil.
It usually takes a few seconds to obtain the current location and it will most likely be nil until then (resulting in geocode error 8 or kCLErrorGeocodeFoundNoResult). I'm not sure what error code 9 means by "FoundPartialResult" but Apple's GeocoderDemo sample app treats both the same way (as "No Result").
Try moving the geocoding code (all the code after the startUpdatingLocation call) to the delegate method locationManager:didUpdateToLocation:fromLocation:. The location manager will call that delegate method when it actually has a location and only then is it safe to use location.
There, after the geocoding is successful (or not), you may want to call stopUpdatingLocation otherwise it will try geocoding every time the user location is updated.
You may also want to check the accuracy (newLocation.horizontalAccuracy) and age (newLocation.timestamp) of the received location before trying to geocode it.
It turns out I mixed up the longitute and latitude when creating the location. Thought I'd add this as something for people to check.