我试图在我的 phonegap 构建中显示一个跟踪用户位置的 map 。我正在尝试使用这个例子,
http://view.jquerymobile.com/master/demos/map-geolocation/
我已经将 html css 和 js 添加到我的应用程序中并添加了
<feature name="http://api.phonegap.com/1.0/geolocation"/>
<feature name="http://api.phonegap.com/1.0/network"/>
到我的 config.xml 文件。
我得到的只是一个没有任何加载的白屏。
我做错了什么!?!?
谢谢大家!你们在过去都帮助了我很多,这是我完成这个应用程序所需要做的最后一件事!
再次感谢!
我使用的html代码是
<div role="main" class="ui-content" id="map-canvas"></div>
我使用的CSS代码是
#map-page, #map-canvas { width: 100%; height: 100%; padding: 0; }
我用的js代码是
<script>
$( document ).on( "pageinit", "#map-page", function() {
var defaultLatLng = new google.maps.LatLng(34.0983425, -118.3267434); // Default to Hollywood, CA when no geolocation support
if ( navigator.geolocation ) {
function success(pos) {
// Location found, show map with these coordinates
drawMap(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
}
function fail(error) {
drawMap(defaultLatLng); // Failed to find location, show default map
}
// Find the users current position. Cache the location for 5 minutes, timeout after 6 seconds
navigator.geolocation.getCurrentPosition(success, fail, {maximumAge: 500000, enableHighAccuracy:true, timeout: 6000});
} else {
drawMap(defaultLatLng); // No geolocation support, show default map
}
function drawMap(latlng) {
var myOptions = {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
// Add an overlay to the map of current lat/lng
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: "Greetings!"
});
}
});
</script>
我将我的 js 放在我的 html 之前,紧接着
<div data-role="content" class="content">
但我也试过把它放在头上。
最佳答案
尝试在地理调用之前将 lat 和 lng var 设置为 0。
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
navigator.geolocation.getCurrentPosition(onSuccess, onError, { maximumAge:60000, timeout:5000, enableHighAccuracy:true });
}
var lat = 0;
var lng = 0;
function onSuccess(position) {
lat += position.coords.latitude
lng += position.coords.longitude
}
关于javascript - Phonegap 构建中的地理位置白屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19902795/