MKErrorDomain error 4 iPhone

时间秒杀一切 提交于 2019-11-30 12:43:52

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.

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'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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!