简体   繁体   English

CLLocationCoordinate2D与2点之间的距离

[英]Distance between 2 points with CLLocationCoordinate2D

Details 细节

I'm trying to measure the distance between two coordinates on a map (longitude, latitude). 我正在尝试测量地图上两个坐标(经度,纬度)之间的距离。 The first coordinate is my current location which is a CLLocation , and the other one is a pin on the map which is of type CLLocationCoordinate2D . 第一个坐标是我当前的位置 ,它是一个CLLocation ,另一个坐标是地图上的一个类型为CLLocationCoordinate2D的引脚。

Issue 问题

I call distanceFromLocation to get my current location in CLLocation but it says bad receiver type from 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

Question

I want to find the distance between these 2 coordinates in metric unit, and I know that types are mismatching, but how to convert them so I can find the distance between these 2 nodes? 我想以公制单位找到这两个坐标之间的距离,我知道类型不匹配,但是如何转换它们以便我可以找到这两个节点之间的距离?

Use the below method for finding distance between two locations 使用以下方法查找两个位置之间的距离

-(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];

}

For example: 例如:

   +(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;
   }

Try to convert CLLocationCoordinate2D to CLLocation , 尝试将CLLocationCoordinate2D转换为CLLocation

Create a CLLocation object using, 使用,创建一个CLLocation对象,

-(id)initWithLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude

Based on @manujmv's answer above, here's a Swift extension (returns metres): 基于@ manujmv上面的答案,这里是一个Swift扩展(返回米):

import Foundation
import CoreLocation

extension CLLocationCoordinate2D {
    func distanceTo(coordinate: CLLocationCoordinate2D) -> CLLocationDistance {
        let thisLocation = CLLocation(latitude: self.latitude, longitude: self.longitude)
        let otherLocation = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)

        return thisLocation.distanceFromLocation(otherLocation)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM