简体   繁体   English

如何使用谷歌计算两个位置之间的距离 api in swift 4

[英]How to calculate distance between two locations using google api in swift 4

I am currently working on an IOS project in which i have to calculate distance between two location.我目前正在从事一个 IOS 项目,我必须在其中计算两个位置之间的距离。 I have done without using google but i want to use google api to get accurate distance i am sharing my code here我没有使用谷歌就完成了,但我想使用谷歌 api 来获得准确的距离我在这里分享我的代码

    let myLocation = CLLocation(latitude: CLLocationDegrees(latittude[indexPath.row])!, longitude: CLLocationDegrees(longittude[indexPath.row])!)
        let lat = UserDefaults.standard.string(forKey: "lat") ?? ""
        let long = UserDefaults.standard.string(forKey: "long") ?? ""
        let myBuddysLocation = CLLocation(latitude: CLLocationDegrees(lat)!, longitude: CLLocationDegrees(long)!)

Use distance function of CoreLocation Framework, 使用CoreLocation Framework的距离功能,

 var startLocation = CLLocation(latitude: startLatitude, longitude: startLongitude)
 var endLocation = CLLocation(latitude: endLatitude, longitude: endLongitude)
 var distance: CLLocationDistance = startLocation.distance(from: endLocation)

Swift 5+: Swift 5+:
As far as I know, there are two ways to find the distance.据我所知,有两种方法可以找到距离。 If you are looking for driving distance, you can always use MKDirections.如果您正在寻找行驶距离,您可以随时使用 MKDirections。 Here is the code for finding driving distance (You can also find walking, and transit distance by changing transport type).这是查找行车距离的代码(您还可以通过更改交通类型来查找步行和公交距离)。

let sourceP = CLLocationCoordinate2DMake( sourceLat, sourceLong)
let destP = CLLocationCoordinate2DMake( desLat, desLong)
let source = MKPlacemark(coordinate: sourceP)
let destination = MKPlacemark(coordinate: destP)
        
let request = MKDirections.Request()
request.source = MKMapItem(placemark: source)
request.destination = MKMapItem(placemark: destination)

// Specify the transportation type
request.transportType = MKDirectionsTransportType.automobile;

// If you want only the shortest route, set this to a false
request.requestsAlternateRoutes = true

let directions = MKDirections(request: request)

 // Now we have the routes, we can calculate the distance using
 directions.calculate { (response, error) in
    if let response = response, let route = response.routes.first {
                print(route.distance) //This will return distance in meters
    }
 }

If you are only looking for air distance/bird's eye distance/coordinate distance, you can use this code:如果你只是在寻找空中距离/鸟瞰距离/坐标距离,你可以使用这个代码:

let sourceP = CLLocation(latitude: sourceLat, longitude: sourceLong)
let desP = CLLocation(latitude: desLat, longitude: desLong))

let distanceInMeter = sourceP.distance(from: desP)

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

相关问题 是否有IOS API来包装Google“两个位置之间的距离” API? - Is there an IOS API that wraps the google 'distance between two locations' API? 如何在谷歌地图iOS swift中绘制两个位置之间的路线 - How to draw routes between two locations in google maps iOS swift 如何在 Swift 中使用 MapKit 在两个位置之间绘制路线? - How to draw a route between two locations using MapKit in Swift? 监测两个地点之间的距离 - Monitoring distance between two locations 计算位置与位置列表之间的距离 - calculate distance between a location and a list of locations 使用 NSPredicate 的位置之间的距离 - Distance Between Locations Using NSPredicate 使用Google API的iOS中两个位置(纬度和经度)之间的估计时间 - Estimated Time Between two Locations(Lattitude & Longitude) in iOS using Google API 如何使用mapView和CLLocationManager在iOS Swift中的两个位置之间绘制方向 - How to plot Directions between two locations in iOS Swift using mapView and CLLocationManager 如何计算当前位置与JSON文件中填充的位置之间的距离 - How to calculate the distance between my current location and locations populated in a JSON File 当一个位置更改用户位置而第二个位置更改用户位置时,如何计算位置之间的距离? - How to calculate distance between locations when one is changing user location and second one should be constant?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM