添加CoreLocation.framework和MapKit.framework
而
在.H
#import <MapKit/MapKit.h>
CLLocationManager *locationManager;
CLGeocoder *geocoder;
鑑於沒有負載
- (void)viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
geocoder = [[CLGeocoder alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
然後
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
LongitudeLbl.text = [NSString stringWithFormat:@"%.8f",currentLocation.coordinate.longitude];
LatitudeLbl.text = [NSString stringWithFormat:@"%.8f",currentLocation.coordinate.latitude];
}
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
NSString *addresstemp2 = [[NSString alloc] init];
NSString *subThoroughfare2 = [NSString stringWithFormat:@"%@",placemark.subThoroughfare];
NSString *thoroughfare2 = [NSString stringWithFormat:@"%@",placemark.thoroughfare];
NSString *postalCode2 = [NSString stringWithFormat:@"%@",placemark.postalCode];
NSString *locality2 = [NSString stringWithFormat:@"%@",placemark.locality];
NSString *administrativeArea2 = [NSString stringWithFormat:@"%@",placemark.administrativeArea];
NSString *country2 = [NSString stringWithFormat:@"%@",placemark.country];
if (![subThoroughfare2 isEqualToString:@"(null)"]) {
addresstemp2 = [NSString stringWithFormat:@"%@",subThoroughfare2];
}
if (![thoroughfare2 isEqualToString:@"(null)"]) {
addresstemp2 = [NSString stringWithFormat:@"%@ %@ \n",addresstemp2,thoroughfare2];
}
if (![postalCode2 isEqualToString:@"(null)"]) {
addresstemp2 = [NSString stringWithFormat:@"%@ %@",addresstemp2,postalCode2];
}
if (![locality2 isEqualToString:@"(null)"]) {
addresstemp2 = [NSString stringWithFormat:@"%@ %@ \n",addresstemp2,locality2];
}
if (![administrativeArea2 isEqualToString:@"(null)"]) {
addresstemp2 = [NSString stringWithFormat:@"%@ %@ \n",addresstemp2,administrativeArea2];
}
if (![country2 isEqualToString:@"(null)"]) {
addresstemp2 = [NSString stringWithFormat:@"%@ %@",addresstemp2,country2];
}
AddressLbl.text = [[NSString alloc] initWithString:addresstemp2];
[AddressLbl sizeToFit];
[locationManager stopUpdatingLocation];
} else {
}
} ];
}
你也改變你的模擬器設置通過設置,選擇位置服務將其更改爲ON。向下看是你的應用程序的名稱,如果它關閉,那麼改爲ON
你在哪裏添加了這段代碼? – 2013-03-07 10:28:26
我已更新我的問題 – 2013-03-07 10:30:04
'[CLLocationManager authorizationStatus]'給出了什麼結果? – fguchelaar 2013-03-07 10:31:44