Ios distanceFromLocation-计算两点之间的距离
只是一个关于核心位置的快速问题,我试图计算两点之间的距离,代码如下:Ios distanceFromLocation-计算两点之间的距离,ios,objective-c,core-location,Ios,Objective C,Core Location,只是一个关于核心位置的快速问题,我试图计算两点之间的距离,代码如下: -(void)locationChange:(CLLocation *)newLocation:(CLLocation *)oldLocation { // Configure the new event with information from the location. CLLocationCoordinate2D newCoordinate = [newLocation
-(void)locationChange:(CLLocation *)newLocation:(CLLocation *)oldLocation
{
// Configure the new event with information from the location.
CLLocationCoordinate2D newCoordinate = [newLocation coordinate];
CLLocationCoordinate2D oldCoordinate = [oldLocation coordinate];
CLLocationDistance kilometers = [newCoordinate distanceFromLocation:oldCoordinate] / 1000; // Error ocurring here.
CLLocationDistance meters = [newCoordinate distanceFromLocation:oldCoordinate]; // Error ocurring here.
}
我在最后两行中遇到以下错误:
错误:无法转换为指针类型
我一直在谷歌搜索,但什么也找不到。试试这个:
CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
您尝试使用的方法是对象上的方法:)距离是在两个位置之间计算的,而不是在到坐标之间 您需要使用以下代码行使用这些坐标来获取相应坐标的位置
CLLocation *newLocation = [[CLLocation alloc] initWithCoordinate: newCoordinate altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:nil];
CLLocationDistance kilometers = [newLocation distanceFromLocation:oldLocation] / 1000;
同样,对于其他坐标,可以使用以下代码行计算这两个位置之间的距离
CLLocation *newLocation = [[CLLocation alloc] initWithCoordinate: newCoordinate altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:nil];
CLLocationDistance kilometers = [newLocation distanceFromLocation:oldLocation] / 1000;
希望这对你有帮助
更新:Swift 3.0
let distanceKiloMeters = (newLocation.distance(from: oldLocation))/1000
这里的问题是您正在调用一个对象: 类别CLLocation CLLocationCoordinate2D实际上是一个结构,由两个双重结构组成:
typedef struct
{
CLLocationDegrees latitude;
CLLocationDegrees longitude;
} CLLocationCoordinate2D;
正确的方法是获取CLLocation
对象并在其上调用distance fromLocation
。像这样:
CLLocation* newLocation;
CLLocation* oldLocation;
CLLocationDistance distance = [newLocation distanceFromLocation:oldLocation];
当然,您首先需要初始化这两个值(例如,从
CLLocationManager
) 摘自优秀图书馆:
在Swift中 让我们创建一个计算两个位置之间距离的方法函数:
func distanceBetweenTwoLocations(source:CLLocation,destination:CLLocation) -> Double{
var distanceMeters = source.distanceFromLocation(destination)
var distanceKM = distanceMeters / 1000
let roundedTwoDigit = distanceKM.roundedTwoDigit
return roundedTwoDigit
}
如果只需要两位数字:
extension Double{
var roundedTwoDigit:Double{
return Double(round(100*self)/100)
}
}
如果要使用2个CLLocationCoordinate2D值,则可以使用此值 这是Xcode 7.1上的Swift 2.1
import CoreLocation
extension CLLocationCoordinate2D {
func distanceInMetersFrom(otherCoord : CLLocationCoordinate2D) -> CLLocationDistance {
let firstLoc = CLLocation(latitude: self.latitude, longitude: self.longitude)
let secondLoc = CLLocation(latitude: otherCoord.latitude, longitude: otherCoord.longitude)
return firstLoc.distanceFromLocation(secondLoc)
}
}
#导入
CLLocation*locA=[[CLLocation alloc]initWithLatitude:“Value”经度:“Value”];
CLLocation*locB=[[CLLocation alloc]initWithLatitude:“Value”经度:“Value”];
CLLocationDistance距离=[locA distanceFromLocation:locB];
NSLog(@“距离:-%f”,距离)//斜接距离
,同时请记住“这种方法通过追踪两个位置之间的一条线来测量两个位置之间的距离,这条线遵循地球的曲率”。所以这不会给出通过道路的距离。如何通过道路找到距离@deanWombourne@AnkitSrivastava如何通过道路找到距离最简单的方法是询问有关堆栈溢出的问题;)谷歌有一个API,你可以使用它(),苹果也可以()我想,因为你问的是一个iOS问题,你可能会想要苹果的:)答案不错,但在iOS9中,这个代码函数比iOS8要花更多的时间。你知道如何修复吗?如果有人好奇,这是一个很好的答案。只是一个小问题。应将var
替换为let
InSide Distance Between WoLocations func;-)需要解释这个答案吗?这不是Android开发代码。这是ios开发代码,用于“计算两点之间的距离”,无论是ios、Android还是其他技术领域。我认为答案应该对代码有一点解释…只是不要复制粘贴代码..请礼貌地解释一下Hi Burhanuddin Rashid这个答案不是复制和过去的。获取距离是苹果的默认功能。我的回答很少,但它的效果很好。输入locA输入Lat,long locB输入Lat,long计算locA到locB的距离。更多信息,谢谢。。
#import <CoreLocation/CoreLocation.h>
CLLocation *locA = [[CLLocation alloc] initWithLatitude:"Value" longitude:"Value"];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:"Value" longitude:"Value"];
CLLocationDistance distance = [locA distanceFromLocation:locB];
NSLog(@"distance:-%f",distance);//distance in Miter