Calculate second point knowing the starting point and distance

后端 未结 4 2078
南旧
南旧 2020-11-28 03:32

using a Latitude and Longitude value (Point A), I am trying to calculate another Point B, X meters away bearing 0 radians from point A. Then display the point B Latitude and

4条回答
  •  没有蜡笔的小新
    2020-11-28 03:41

    Here is an updated version using Swift:

    let location = CLLocation(latitude: 41.88592 as CLLocationDegrees, longitude: -87.62788 as CLLocationDegrees)
    
    let distanceInMeter : Int = 500
    let directionInDegrees : Int = 135
    
    let lat = location.coordinate.latitude
    let long = location.coordinate.longitude
    
    let radDirection : CGFloat = Double(directionInDegrees).degreesToRadians
    
    let dx = Double(distanceInMeter) * cos(Double(radDirection)) 
    let dy = Double(distanceInMeter) * sin(Double(radDirection))
    
    let radLat : CGFloat = Double(lat).degreesToRadians
    
    let deltaLongitude = dx/(111320 * Double(cos(radLat)))  
    let deltaLatitude = dy/110540                   
    
    let endLat = lat + deltaLatitude
    let endLong = long + deltaLongitude
    

    Using this extension:

    extension Double {
        var degreesToRadians : CGFloat {
            return CGFloat(self) * CGFloat(M_PI) / 180.0
        }
    }
    

提交回复
热议问题