This question already has answers here:
iPhone current user location coordinates showing as (0,0)

(8个答案)


5年前关闭。




我有以下代码在我的应用上获取userLocation:
mapView.showsUserLocation=TRUE;
    mapView.userLocation.title=@"Aktuelle Position";

那是在viewDidLoad();

但是,如何通过以下方法从userLocation获取纬度和经度?
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation{

    Koordinate *user = mapView.userLocation;
    Koordinate *cord1 = [eventPoints objectAtIndex:2];
    CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:cord1.latitude longitude:cord1.longtitude];

    Koordinate *cord2 = [eventPoints objectAtIndex:3];
    CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:cord2.latitude longitude:cord2.longtitude];

    double distance = [loc1 getDistanceFrom:loc2] / 1000;

    NSLog(@"Distanz:", distance);
}

最佳答案

-(void)locationManager:(CLLocationManager *)manager
  didUpdateToLocation:(CLLocation *)newLocation
      fromLocation:(CLLocation *)oldLocation
{
    float lat = newLocation.coordinate.latitude;
    float lon = newLocation.coordinate.longitude;
}

关于objective-c - 来自mapView.user位置的纬度和经度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2325076/

10-12 14:49