簡體   English   中英

distanceFromLocation - 計算兩點之間的距離

[英]distanceFromLocation - Calculate distance between two points

關於Core Location的一個簡單問題,我正在嘗試計算兩點之間的距離,代碼如下:

    -(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對象上的方法:)

距離是在2個CLLocations之間計算的,而不是在坐標之間計算的。

您需要使用這些坐標使用以下代碼行獲取相應坐標的CLLocations

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

在斯威夫特

讓我們創建一個計算兩個位置之間距離的方法函數:

 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)
    }

}

這里的問題是你正在調用一個對象方法

- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location;

類CLLocation。

CLLocationCoordinate2D實際上是一個結構,由兩個雙精度組成:

typedef struct
{
    CLLocationDegrees latitude;
    CLLocationDegrees longitude;
} CLLocationCoordinate2D;

正確的方法是獲取CLLocation對象並在其上調用distanceFromLocation 像這樣:

CLLocation* newLocation;
CLLocation* oldLocation;
CLLocationDistance distance = [newLocation distanceFromLocation:oldLocation];

當然,您首先需要初始化這兩個值(例如,來自CLLocationManager )。

#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

摘自優秀的libary CoreLocation

- (CLLocationDistance) distanceFromCoordinate:(CLLocationCoordinate2D) fromCoord;
{
    double earthRadius = 6371.01; // Earth's radius in Kilometers

    // Get the difference between our two points then convert the difference into radians
    double nDLat = (fromCoord.latitude - self.coordinate.latitude) * kDegreesToRadians;  
    double nDLon = (fromCoord.longitude - self.coordinate.longitude) * kDegreesToRadians; 

    double fromLat =  self.coordinate.latitude * kDegreesToRadians;
    double toLat =  fromCoord.latitude * kDegreesToRadians;

    double nA = pow ( sin(nDLat/2), 2 ) + cos(fromLat) * cos(toLat) * pow ( sin(nDLon/2), 2 );

    double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));
    double nD = earthRadius * nC;

    return nD * 1000; // Return our calculated distance in meters
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM