我正在尝试制作一个简单的应用程序,向我显示我在谷歌地图API上的位置。
我正在使用科尔多瓦。
我为浏览器构建它,并且工作正常,但是当我为 android 或 Windows 构建时,它会构建,但当我运行它时,应用程序会打开但不起作用。
它向我展示了一个警告,说谷歌没有定义。
看看附上的照片:警告错误!
这是我的JS代码:
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
navigator.geolocation.getCurrentPosition(app.onSuccess, app.onError);
},
onSuccess: function(position){
var longitude = position.coords.longitude;
var latitude = position.coords.latitude;
var latLong = new google.maps.LatLng(latitude, longitude);
var mapOptions = {
center: latLong,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
var marker = new google.maps.Marker({
position: latLong,
map: map,
title: 'my location'
});
},
onError: function(error){
alert("cod erro " + error.code + ". n" +
"mensagem: " + error.message);
}
};
将此脚本放在 HTML 文档的末尾(将YOUR_API_KEY替换为实际的 API 密钥(
<script async defer src="https://maps.googleapis.com/maps/api/js key=YOUR_API_KEY&callback=initMap">
</script>
在此脚本中,回调称为 initMap((。它看起来像这样:
function initMap() {
var mapOptions = {
center: {lat: -34.397, lng: 150.644},
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"), mapOptions);
google.maps.event.addListenerOnce(map, 'tilesloaded', function(){
getGeoLocation();
});
}
// Gets user location
function getGeoLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
// Gets lat & lng and makes a LatLng object
var longitude = position.coords.longitude;
var latitude = position.coords.latitude;
var latLong = new google.maps.LatLng(latitude, longitude);
// Places a marker on the map
var marker = new google.maps.Marker({
position: latLong,
map: map,
title: 'my location'
});
// Centers map on user location
map.setCenter({lat:latitude, lng:longitude});
}, geoLocationError, {maximumAge:600000, timeout:10000, enableHighAccuracy: true});
}
}