我有一個Ionic/PhoneGap應用程序,可使用Google地圖google.maps.DistanceMatrixService()
服務計算兩個位置之間的距離。Google Maps JavaScript API可在瀏覽器中運行,但不適用於設備上的Cordova應用程序
這工作正常,當我在網絡瀏覽器中的應用程序ionic serve
,但是當我將它安裝到我的測試設備(三星Galaxy S4 - 5.0)它不起作用,並且我的意思是我沒有得到任何回調(來自被拒絕對象或try catch語句)使得無法調試。
我的代碼
廠包裝類:
.factory('GmapsWrapper', function ($q) {
function _GetLatLngObject(lat, lng) {
console.log('Gmaps Wrapper - getting new lat lng object...');
try {
return new google.maps.LatLng(lat, lng);
} catch (err) {
console.log('*google.maps.LatLng()* error: ' + err.message);
return null;
}
}
function _GetJourneyDistance(startLat, startLng, endLat, endLng) {
console.log('Gmaps Wrapper - calculating journey distance...');
var d = $q.defer();
try {
var origin = _GetLatLngObject(startLat, startLng);
var destination = _GetLatLngObject(endLat, endLng);
console.log('origin: ' + angular.toJson(origin));
console.log('destination: ' + angular.toJson(destination));
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
origins: [origin],
destinations: [destination],
travelMode: google.maps.TravelMode.DRIVING
}, function (response, status) {
console.log('*service.getDistanceMatrix* callback: response = ' + angular.toJson(response) + ', status = ' + status);
d.resolve(response, status);
});
} catch (err) {
console.log('*google maps service.getDistanceMatrix()* error: ' + err.message);
d.reject(err);
}
return d.promise;
}
return {
GetJourneyDistance: _GetJourneyDistance,
GetLatLngObject: _GetLatLngObject
}
});
控制器功能調用GetJourneyDistance
方法:
function Calc() {
GmapsWrapper.GetJourneyDistance(52.8425701, -1.3493778, 52.8460761, -1.3363366).then(function (data, status) {
PopupWrapper.ShowAlert('Calculte Distance', 'Success.');
if (data.rows && data.rows.length > 0) {
var distance = data.rows[0].elements[0].distance;
PopupWrapper.ShowAlert('Distance', 'Km = ' + EbiLibrary.ConvertMetersToKm(distance.value) + '<br />Miles = '
+ EbiLibrary.ConvertMetersToMiles(distance.value));
}
}, function (err) {
PopupWrapper.ShowAlert('Calculate Distance', 'Error: ' + angular.toJson(err));
});
}
index.html的腳本參考:
<script src="https://maps.googleapis.com/maps/api/js?key=[MY KEY]&sensor=true"></script>
config.xml文件白名單:
<access origin="https://maps.googleapis.com/maps/api/*" />
附加信息
我知道谷歌地圖腳本在我index.html
文件正確添加,因爲當我console.log
它顯示他們作爲一個出發地和目的地的變量lat lng對象,它使用new google.maps.LatLng(lat, lng)
方法創建。
您是否已對Google網址進行了白名單? – Joerg
是的,在我的'config.xml'中我有''當我檢查它時,我看到相關的腳本添加到頭標籤。如果白名單不正確,會出現這些腳本嗎? –