萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> ios >> ios如何根據經緯度坐標取詳細地址

ios如何根據經緯度坐標取詳細地址

   以當前手機為例:

  獲取當前手機地址,再從經緯度轉換成詳細地址(包括:國家名-國家代碼,省,市,街道名,郵政代代碼等)。

  1:獲取當前手機經緯度

  Reverse geoder Class

  1:- (void)startedReverseGeoderWithLatitude:(double)latitude longitude:(double)longitude{ CLLocationCoordinate2D coordinate2D; coordinate2D.longitude = longitude; coordinate2D.latitude = latitude; // if (self.locationInfo == nil) { self.locationInfo = [[[Location alloc] init] autorelease]; } self.locationInfo.latitude = latitude; self.locationInfo.longitude = longitude; // MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinate2D]; geoCoder.delegate = self; [geoCoder start];}#pragma mark -#pragma mark MKReverseGeocoderDelegate methods- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark { NSLog(@"reverse finished..."); NSLog(@"country:%@",placemark.country); NSLog(@"countryCode:%@",placemark.countryCode); NSLog(@"locality:%@",placemark.locality); NSLog(@"subLocality:%@",placemark.subLocality); NSLog(@"postalCode:%@",placemark.postalCode); NSLog(@"subThoroughfare:%@",placemark.subThoroughfare); NSLog(@"thoroughfare:%@",placemark.thoroughfare); NSLog(@"administrativeArea:%@",placemark.administrativeArea); self.locationInfo.locationName = [NSString stringWithFormat:@"%@ %@,%@ %@,%@", placemark.country, placemark.administrativeArea, placemark.locality, placemark.subLocality, placemark.thoroughfare]; NSLog(@"administrativeArea:%@",self.locationInfo.locationName); [self.target performSelector:self.callBack withObject:@"0"]; geocoder = nil;}- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error { NSLog(@"error %@" , error); [self.target performSelector:self.callBack withObject:@"1"];}

  2:獲取當前手機經緯度的, 詳細地址。

  Current location class

  -(void) startUpdatingLocation{ // Start the location manager. [[selflocationManager] startUpdatingLocation]; }#pragma mark -#pragma mark Location manager/** Return a location manager -- create one if necessary. */- (CLLocationManager *)locationManager { if (locationManager != nil) { return locationManager; } locationManager = [[CLLocationManager alloc] init]; [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]; [locationManager setDelegate:self]; return locationManager;}/** Conditionally enable the Add button: If the location manager is generating updates, then enable the button; If the location manager is failing, then disable the button. */- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { [self.target performSelector:self.callBack withObject:@"0"];}- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { [self.target performSelector:self.callBack withObject:@"1"];}

copyright © 萬盛學電腦網 all rights reserved