iOS CoreLocation CLGeocoder giving incomplete UK postcode - iphone

I've got CoreLocation finding me, and then I'm trying to run reverseGeocodeLocation to figure out the postcode. However, I'm getting an incomplete postcode (SO31 4). Normally you'd expect another two characters after the 4. Here's the code I'm using:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
[self.geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
self.postcode.text = placemark.postalCode;
if (self.postcode.text.length > 0)
[self.locationManager stopUpdatingLocation];
}];
}
Notably, I also get similar results trying the lat/long on Google Maps API, you can see here:
http://maps.googleapis.com/maps/api/geocode/json?latlng=50.87138339,-1.30983213&sensor=true
Ideally I want a full postcode. The only alternative that comes to mind is only using the first four characters but I thought I'd ask here first just incase somebody has a better idea.

They don't have the full UK postcode database so you will only ever get this resolution with the Google API's.
You need to use a full postcode database and companies like http://www.postcodeanywhere.co.uk/ sell them.

Late reply - but for what it's worth, the postcode is still a valid postcode without the last 2 letters - it just covers a wider area. It looks odd but it's still more accurate than if you drop the last number.

Just had the same problem with a zip code in Brazil.
Here we have the format 00000-000 and the property postalCode from CLPlacemark was returning only the first five digits.
It's possible to get the full postal code number accessing the addressDictionary property from CLPlacemark. In this NSDictionary we can get the information we need in the values from the keys: ZIP and PostCodeExtension.

Related

Iphone GPS is not giving accurate latitude and longitude

Hello Friend i have seen many post regarding accuracy problem with gps but its not working all the time
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSString *latstr = [NSString stringWithFormat:#"%f", newLocation.coordinate.latitude];
NSString *longstring=[NSStringstringWithFormat:#"%f",newLocation.coordinate.longitude];
if (abs(howRecent)>5.0)
{
[self.locationManager startUpdatingLocation];
return;
}
if(abs(newLocation.horizontalAccuracy)<0.0f)
{
[self.locationManager startUpdatingLocation];
return;
}
if(newLocation.horizontalAccuracy>65.0f)
{
[self.locationManager startUpdatingLocation];
return;
}
self.latstring = [latstr copy];
self.longstr = [longstring copy];
if((updateLocationFirst||loadFirstView))
{
[[NSUserDefaults standardUserDefaults]setObject:latstring forKey:#"Latitude"];
[[NSUserDefaults standardUserDefaults]setObject:longstr forKey:#"Longitude"];
[self displayParticularDaySpecial];
loadFirstView=FALSE;
updateLocationFirst=FALSE;
[self.locationManager stopUpdatingLocation];
}
}
Here the problem is I am sending the latitude and longitude to the google api with respect to some addresses if i am decreasing the accuracy value its taking lot of time to load and this value is having problem when you reach at destination with respect to destination with 0.6 miles difference.
You should refer to the Location Awareness Programming Guide which provides excellent programming examples for determining your location.
As you'll see in the demo in that document, when you start the standard location manager, you generally tell it what accuracy you require. And once you start the location manager, you don't have to start it again (like you appear to be doing in your code sample). The didUpdateLocations method will be called again and again, with increasing accuracy, all on its own. You don't need to start the location manager again ... it keeps going until you turn it off. By definition, if you're in didUpdateLocations, it means it's already on.
I also notice that you're using the old didUpdateToLocation. If you're programming for iOS 6, you really want to use didUpdateLocations, which is the current version of that CLLocationManagerDelegate method.
Finally, you mention it takes a long time to get your location. That's very possible. In fact, in certain areas, you will never get very close (or any location at all). Your app should assume that accurate locations will take a while, and you should gracefully handle if you never get a really accurate location, which is very possible.
Have you set desiredLocationAccuracy in your CLLocationManager? By default the range is somewhat wide.
It will take some time to acquire a very exact value.

Get place names using CLGeocoder (not street names)

I want to develop my own maps app. I have been successful in getting a street address at the point of touch on my iPhone using CLGeocoder. Now my question is the following:
I know that you can get information about a place by using a URL like https://maps.google.com/?q=37.324599,-122.031844
If you click on the above URL, it will take you near a church in Cupertino. Now CLGeocoder will only give me its street address i.e. 10110 N De Anza Blvd Cupertino, CA 95014 (I get this). But how to get the actual name i.e. St Joseph of Cupertino Church and Parish?
We can see it on Google Maps that means they must have it stored somewhere right?
Is there any way to access those place names (Not just by CLGeoCoder, any way is fine).
The GeoCoding API in general is for converting between street addresses and coordinates. If you want place names you can probably use the Places API.
(I know this is more of a comment than an answer, but I think I don't have enough reputation points to comment on questions yet)
YOu need t odo like this way after getting lat long use this code by CLLocation manager class
{
CLGeocoder *reverseGeo = [[CLGeocoder alloc] init];
[reverseGeo reverseGeocodeLocation: loc completionHandler:
^(NSArray *placemarks, NSError *error) {
NSLog(#"%#",[placemarks objectAtIndex:0]);
for (CLPlacemark *placemark in placemarks) {
NSLog(#"~~~~~~~~%#",placemark.locality);
}
}];
}

CLGeocoder Only Returning One Placemark

I have a problem with CLGeocoder where when I call geocodeAddressString:withCompletionHandler I only ever get one result back, despite knowing that the inputted string should return more than one value. The class reference even states:
In the case of forward-geocoding requests, multiple placemark objects may be returned if the provided information yielded multiple possible locations.
However, my placemarks array only ever has one item in it:
[geocoderDestination geocodeAddressString:destination completionHandler:^(NSArray *placemarks, NSError *error){
NSLog(#"array count:%i", [placemarks count];}
Thank you for any help.
I have used strings such as "Piccadilly, UK", "Union Street, UK" which have only returned one result. Now that I think about it, putting UK on the end might be the contributing factor.
I dont know about CLGeocoder but if your requirement is a location search another way is to use google location search url http://maps.google.com/maps/geo?q=london which returns a json containing the matched location information.
Instead of UK use United Kingdom it will give you some related result.
Try to add some more info in your address.
One more thing UK is not a valid country code it's GB but it seems that putting GB instead of UK didn't solve the problem.
Moreover CLGeocoder is not as smart as Google Maps API right now because apple uses its own server to decode addresses so you can use Google services.

MKErrorDomain error 4 iPhone

I keep getting this randomly when I run my gps app I'm building. It doesn't happen everytime, and the coordinates passed in are always valid (i nslog them). Is there documentation for these somewhere?
EDIT:
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(locManager.location.coordinate.latitude, locManager.location.coordinate.longitude);
geocoder1 = [[MKReverseGeocoder alloc] initWithCoordinate:coord];
geocoder1.delegate = self;
[geocoder1 start];
and then about half the time it returns an error. I tried releasing and re-assigning the geocoder if there was an error, but that didn't help. Only thing that did was restarting the app.
In "MKTypes.h" in the MapKit framework, the following is defined:
Error constants for the Map Kit framework.
enum MKErrorCode {
MKErrorUnknown = 1,
MKErrorServerFailure,
MKErrorLoadingThrottled,
MKErrorPlacemarkNotFound,
};
...
MKErrorPlacemarkNotFound
The specified placemark could not be found.
This sounds like you are referencing some unknown placemark in your code? Or it could be that Google doesn't have a name for the position you are passing - however valid the coordinates may be.
I've met and solved this issue recently. In my case, when Apple Map cannot find any result for a query, it sometimes will just throw this this "MKErrorDomain = 4" error. So I ended up just treat this as "result not found".
It was painstaking to find this out, MapKit needs a better Error handling system.
I've been hitting this error repeatedly, and was unable to figure out how to make it stop; but I finally found an end-run around the whole issue that works quite well, and only takes a little more work: Don't use Apple's MKReverseGeocoder at all -- instead, directly call Google's reverse-geocoding API (this is apparently the same service that MKReverseGeocoder does behind the scenes). You can get back either JSON or XML (your preference), which you will then have to parse, but that isn't too hard.
For example, since my app is using ASIHTTPRequest, this is what it looks like (although this would also be easy to do with do with Apple's native APIs such as NSURLConnection):
#pragma mark -
#pragma mark CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// Be careful: My code is passing sensor=true, because I got the lat/long
// from the iPhone's location services, but if you are passing in a lat/long
// that was obtained by some other means, you must pass sensor=false.
NSString* urlStr = [NSString stringWithFormat:
#"http://maps.googleapis.com/maps/api/geocode/xml?latlng=%f,%f&sensor=true",
newLocation.coordinate.latitude, newLocation.coordinate.longitude];
NSURL* url = [NSURL URLWithString:urlStr];
self.reverseGeocoderRequest = [ASIHTTPRequest requestWithURL:url];
self.reverseGeocoderRequest.delegate = self;
[self.reverseGeocoderRequest startAsynchronous];
}
By the way, Google's API has rules, just like Apple's does. Make sure you read the docs, especially regarding quotas.
I'm running into the same thing (the exact same code randomly fails sometimes) and I think I've found the answer. From Apple's developer docs: "Each Map Kit application has a limited amount of reverse geocoding capacity, so it is to your advantage to use reverse geocode requests sparingly."
So my theory is, we're getting rate-limited... since no other variables are changing (i.e. my code isn't changing, I'm running it on the simulator so the location of the device isn't changing, etc.) I think this must be the only remaining reason.
I just got done with a lot of research on this problem and it seems to be outside of our hands. I checked the developer forums as well as all around Stack and elsewhere and no one has a solution other than using a different service. There is a pretty good thread at https://devforums.apple.com/message/154126 on the subject.
Some people find the error after a certain time, I just find it to be out for a while and then comes back. I looked at the "Current Address" sample code and I couldn't see how I might have messed up. I ran the sample code and sure enough, it was NSLogging errors instead of returning a location.
This link has some code using Google's reverse geocoder: http://www.iphonedevsdk.com/forum/iphone-sdk-development/31883-pbrequestererrordomain-errors-reverse-geocoding.html#post155793
Actually, I am running into this problem as well. Code is extremely compact
//1. ask map for current coords
CLLocationCoordinate2D userLocation;
userLocation.latitude = [[_theMapView.userLocation location] coordinate].latitude;
userLocation.longitude = [[_theMapView.userLocation location] coordinate].longitude;
NSLog(#"%f, %f",userLocation.latitude,userLocation.longitude);
//2. reverse geocode coords
MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc]
initWithCoordinate:userLocation];
[reverseGeocoder setDelegate:self];
[reverseGeocoder start];
and later simply NSLog the error in the fail delegate method. It works the first time or two, then stops working

MKReverseGeocoder server returned error: 503

Currently i am developing an iPhone application where i have to show user's current city and location for which i am using Mapkit Framework.When i build the application it works fine and show me the exact city details .But right now when i try to build the application again application shows following error message in my log
/SourceCache/ProtocolBuffer_Sim/ProtocolBuffer-26/Runtime/PBRequester.m:523 server returned error: 503
reverseGeocoder: didFailWithError:Error Domain=PBRequesterErrorDomain Code=6001 "Operation could not be completed. (PBRequesterErrorDomain error 6001.)"
Is any body facing the same issue and how can i resolve it?
I am pretty sure this is happening because you are in testing, and using the reverse geocoder too much and Google's servers know that. So, it has basically blocked you for a while. Try again later and see if it works.
You need to make sure that you are not calling the geocoder more than once every 60 seconds.
I find out!
Well, now we'll have to use CLGeocoder, but if you want still to use MKReverseGeocoder, you MUST not call [geocoder start] twice, even if the geocode object is a fresh new one!
For example, DON'T do this:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
MKReverseGeocoder *geoCoder = [[[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate] autorelease];
geoCoder.delegate = self;
[geoCoder start];
}
Or you'll get an arror: the location manager update position function is being called as soon as a new position is detected. As the accuracy is getting better and better, a new fresh new posistion is being sent to "didUpdateToLocation".
But creating a new object MKReverseGeocoder each time a position is found is a bad idea!
So, you've got a few solutions:
- Put a boolean that is true when geocoder is started. Then in didFindPlacemark (geocoder's), you should stop the geocoder with [geocoder cancel] and set your boolean to false.
- Make geocoder variable not local and manange a [geocoder cancel] + realease and create a new one and[geocoder start] each time didUpdateToLocation is beeing called.
- Any other way to avoid [geocoder start] to be called twice
I used the first solution and never get the 503 error anymore.
For me, MKReverseGeocoder was failing with 503 far too many times. It would also fail the first time it was requested, well after 60 seconds had passed. As a result, I decided to give up using it and instead decided to access Google's API directly using NSURLRequest via a service I created that also parses the result as a JSON String using the SBJSON Framework (very useful).
From my service, I call the following URL as follows:
NSString *urlStr = [NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true", coordinate.latitude, coordinate.longitude];
where coordinate is a CLLocationCoordinate2D.
I realise this is a long way around the problem, but since converting to this system I have had no issues!
It can happen at random, depending on the server's status.