CLLocationCoordinate2D与2点之间的距离

E-R*_*die 4 core-location cllocationmanager ios ios7

细节

我正在尝试测量地图上两个坐标(经度,纬度)之间的距离.第一个坐标是我当前的位置是a CLLocation,另一个坐标是地图上的一个类型 CLLocationCoordinate2D.

问题

我打电话distanceFromLocation来获取我当前的位置,CLLocation但它说的接收器类型不好CLLocationCoordinate2D.

CLLocationCoordinate2D locationCoordinate;

// Set the latitude and longitude
locationCoordinate.latitude  = [[item objectForKey:@"lat"] doubleValue];
locationCoordinate.longitude = [[item objectForKey:@"lng"] doubleValue];

CLLocationDistance dist = [locationCoordinate distanceFromLocation:locationManager.location.coordinate]; // bad receiver type from CLLocationCoordinate2D
Run Code Online (Sandbox Code Playgroud)

我想以公制单位找到这两个坐标之间的距离,我知道类型不匹配,但是如何转换它们以便我可以找到这两个节点之间的距离?

man*_*jmv 10

使用以下方法查找两个位置之间的距离

-(float)kilometersfromPlace:(CLLocationCoordinate2D)from andToPlace:(CLLocationCoordinate2D)to  {

    CLLocation *userloc = [[CLLocation alloc]initWithLatitude:from.latitude longitude:from.longitude];
    CLLocation *dest = [[CLLocation alloc]initWithLatitude:to.latitude longitude:to.longitude];

    CLLocationDistance dist = [userloc distanceFromLocation:dest]/1000;

    //NSLog(@"%f",dist);
    NSString *distance = [NSString stringWithFormat:@"%f",dist];

    return [distance floatValue];

}
Run Code Online (Sandbox Code Playgroud)


ner*_*lfe 6

例如:

   +(double)distanceFromPoint:(double)lat lng:(double)lng
   {

      CLLocation *placeLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lng];
      CLLocationCoordinate2D usrLocation = locationManager.location;
      CLLocation * userLocation = [[CLLocation alloc] initWithLatitude:usrLocation.latitude longitude:usrLocation.longitude];

      double meters = [userLocation distanceFromLocation:placeLocation];

      return meters;
   }
Run Code Online (Sandbox Code Playgroud)