为什么我无法在Objective C中获得真实设备上的纬度和经度

时间:2017-03-02 07:14:28

标签: objective-c google-maps

我是iOS的新手,我面临的问题是在真实设备上获取当前的纬度和经度,因为我可以在模拟器上获得纬度和经度。 我的代码就像这样

    if (nil == locationManager)
            locationManager = [[CLLocationManager alloc] init];

        locationManager.delegate = self;
        //Configure Accuracy depending on your needs, default is kCLLocationAccuracyBest

        locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;

        // Set a movement threshold for new events.
        locationManager.distanceFilter = kCLDistanceFilterNone; // meters

        [locationManager startUpdatingLocation];

        Currentlatitude = locationManager.location.coordinate.latitude;
        Currentlongitude = locationManager.location.coordinate.longitude;

        NSLog(@"Latitude =%f",Currentlatitude);
        NSLog(@"Longitude =%f",Currentlongitude);

        CheckString=@"2";
        // Your location from latitude and longitude
        CLLocation *location = [[CLLocation alloc] initWithLatitude:Currentlatitude longitude:Currentlongitude];

        // Call the method to find the address
        [self getAddressFromLocation:location completionHandler:^(NSMutableDictionary *d) {
            NSLog(@"address informations : %@", d);
            //  NSLog(@"formatted address : %@", [placemark.addressDictionary valueForKey:@"FormattedAddressLines"]);
            NSLog(@"Street : %@", [d valueForKey:@"Street"]);
            NSLog(@"ZIP code : %@", [d valueForKey:@"ZIP"]);
            NSLog(@"City : %@", [d valueForKey:@"City"]);
            CityNameCurrent=[d valueForKey:@"City"];

            // etc.
        } failureHandler:^(NSError *error) {
            NSLog(@"Error : %@", error);
        }];

- (void)getAddressFromLocation:(CLLocation *)location completionHandler:(void (^)(NSMutableDictionary *placemark))completionHandler failureHandler:(void (^)(NSError *error))failureHandler
{
    NSMutableDictionary *d = [NSMutableDictionary new];
    CLGeocoder *geocoder = [CLGeocoder new];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        if (failureHandler && (error || placemarks.count == 0)) {
            failureHandler(error);
        } else {
            CLPlacemark *placemark = [placemarks objectAtIndex:0];
            if(completionHandler) {
                completionHandler(placemark.addressDictionary);
            }
        }
    }];
}

在ViewDidLod()

  mapViewMap.delegate=self;
    mapViewMap.myLocationEnabled=YES;
    locationtbl.hidden=YES;

    if (nil == locationManager)
        locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate = self;
    //Configure Accuracy depending on your needs, default is kCLLocationAccuracyBest

    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;

    // Set a movement threshold for new events.
    locationManager.distanceFilter = kCLDistanceFilterNone; // meters


    [locationManager startUpdatingLocation];

    Currentlatitude = locationManager.location.coordinate.latitude;
    Currentlongitude = locationManager.location.coordinate.longitude;

    NSLog(@"Latitude =%f",Currentlatitude);
    NSLog(@"Longitude =%f",Currentlongitude);


    mapViewMap.settings.myLocationButton = YES;
    mapViewMap.settings.scrollGestures = YES;
    mapViewMap.settings.zoomGestures = YES;
    mapViewMap.settings.tiltGestures=YES;
    mapViewMap.settings.rotateGestures=NO;
    mapViewMap.settings.compassButton = YES;

我没有得到我做错的事。请帮忙。谢谢你!

3 个答案:

答案 0 :(得分:0)

模拟器在生成位置之前不必等待修复。该设备。您必须为位置管理器实现委托,并等待通过更新调用委托。

答案 1 :(得分:0)

您还需要向plist添加以下一个(或两个)键: NSLocationWhenInUseUsageDescription 要么 NSLocationAlwaysUsageDescription

并相应地调用

[self.locationManager requestWhenInUseAuthorization]

[self.locationManager requestAlwaysAuthorization] 

答案 2 :(得分:0)

您需要实施CLLocationManager委托方法didUpdateLocations,这是您获取位置更新的地方。

你可以这样做。

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    // latest location
    self.currentLocation = [locations lastObject]; 
    float longitude = self.currentLocation.coordinate.longitude;
    float latitude = self.currentLocation.coordinate.latitude;
   // Here you can perform operations with your location coordinates 

}

您还需要根据您的位置需求在.plist文件中添加以下条目。

NSLocationWhenInUseUsageDescription  要么 NSLocationAlwaysUsageDescription

通过调用

请求位置使用权限
[locationManager requestWhenInUseAuthorization];

[locationManager requestAlwaysAuthorization] 

文档链接Location and Maps Programming Guide