Google 不允许单个设备在 60 秒内多次检索其位置,因此在 (MKReverseGeocoder *)geocoder didFailWithError 时使用另一种方法(而不是 http 请求,需要 JSON)。
它适用于我的 4.3.3 (3GS) 并测试了 30-40 次连续检索用户的位置而没有崩溃,希望它有所帮助!
- (void) retrieveCurrentLoc {
self.geoCoder =
[[[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate] autorelease];
geoCoder.delegate = self;
[geoCoder start];
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{
NSString *fetchURL = [NSString stringWithFormat:@"http://maps.google.com/maps/geo? q=%@,%@&output=json&sensor=true", [NSString stringWithFormat:@"%f",mapView.userLocation.location.coordinate.latitude], [NSString stringWithFormat:@"%f",mapView.userLocation.location.coordinate.longitude]];
NSURL *url = [NSURL URLWithString:fetchURL];
NSString *htmlData = [NSString stringWithContentsOfURL:url];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *json = [parser objectWithString:htmlData error:nil];
NSArray *placemark = [json objectForKey:@"Placemark"];
if ([[[[[placemark objectAtIndex:0] objectForKey:@"AddressDetails"]objectForKey:@"Country"]objectForKey:@"Thoroughfare"]objectFor Key:@"ThoroughfareName"] != NULL){
currentAddress = [[[[[placemark objectAtIndex:0] objectForKey:@"AddressDetails"]objectForKey:@"Country"]objectForKey:@"Thoroughfare"]objectFor Key:@"ThoroughfareName"];}
else {
currentAddress = [[placemark objectAtIndex:0] objectForKey:@"address"];
}
}