通过经纬度定位城市:
CLGeocoder * geoCoder = [[[CLGeocoder alloc] init] autorelease];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks,NSError *error) {
for (CLPlacemark * placemark in placemarks) {
NSString *myCity = placemark.locality;
if ([myCity isEqualToString:@"长安"]) {
myCity = @"西安";
}
self.myLabel.text = [NSString stringWithFormat:@"(%@,%@)||%@ %@",currentLatitude,currentLongitude,placemark.country,myCity];
if (self.myLabel.text.length > 0) {
[self.locationManager stopUpdatingLocation];
}
}
}];
通过城市名定位经纬度:
CLGeocoder * geocoder = [[CLGeocoder alloc] init]; |
066 | [geocoder geocodeAddressString:_searchTextField.text completionHandler:^(NSArray *placemarks, NSError *error) { |
071 | NSLog(@ "Geocoder error : %@" ,error); |
075 | if ([placemarks count] == 0) |
077 | NSLog(@ "Could found the address." ); |
081 | CLPlacemark * placeMark = [placemarks objectAtIndex:0]; |
083 | _mapView.centerCoordinate = placeMark.location.coordinate; |
085 | mAnnotationView * annotationView = [[mAnnotationView alloc] initWithCLLocationCoordinate2d:placeMark.location.coordinate title:placeMark.locality subTitle:[NSString stringWithFormat:@ "%@-%@" ,placeMark.country,placeMark.administrativeArea]]; |
087 | [_mapView addAnnotation:annotationView]; |
089 | [_mapView selectAnnotation:annotationView animated:YES]; |
092 | NSLog(@ "country:%@,locality:%@,%@,%@" ,placeMark.country,placeMark.locality,placeMark.administrativeArea,placeMark.region); |
094 | [indicatorView stopAnimating]; |