2

我在開始使用這項新功能時遇到了麻煩,因爲我必須在正在處理的應用程序上實施此功能。所以應用程序就像這樣工作;用戶通過按下一個按鈕在地圖上輸入一個點,並獲得一個來源的latlng。另一個模式打開,然後他們必須爲目的地執行相同的操作。我想要做的是計算兩點之間的距離並在地圖上顯示路線。我一直在試圖找到這個教程,但沒有運氣。任何人都知道如何做到這一點,或有任何工作示例回購,我可以看看它?會有很大的幫助。謝謝!計算Google Maps for Ionic 2中兩點之間的距離

+0

隨着谷歌地圖API,您可以得到兩個點之間的距離就這麼簡單傳遞2個位置。 – apineda

+0

兩個位置作爲latlng對象傳遞? api的名稱是什麼? – BleachedAxe

+0

這是一個示例:https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6655101,-73.89188969999998&destinations=40.6905615%2C-73.9976592。完整的文檔位於:https://developers.google.com/maps/documentation/distance-matrix/intro – apineda

回答

3

你見過這樣的:

https://stackoverflow.com/a/27943/52160

function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) { 
    var R = 6371; // Radius of the earth in km 
    var dLat = deg2rad(lat2-lat1); // deg2rad below 
    var dLon = deg2rad(lon2-lon1); 
    var a = 
     Math.sin(dLat/2) * Math.sin(dLat/2) + 
     Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
     Math.sin(dLon/2) * Math.sin(dLon/2) 
     ; 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c; // Distance in km 
    return d; 
    } 

    function deg2rad(deg) { 
    return deg * (Math.PI/180) 
    }