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 {
}
} ];
}
また、シミュレーターの設定を変更して、設定に移動し、ロケーションサービスを選択してオンに変更します。see down は、オフの場合はアプリケーション名であり、次にオンに変更します