我创建了一个脚本,它将使用地理定位库显示用户的位置,一切正常.我已经使用PhoneGap导出了这个 HTML 5脚本,可以看到在Settings- Location Services我的应用程序设置为On.所以我假设每次运行我的
我不想让人们每次打开我的应用程序时都单击“允许”.应用程序是否没有使用“服务” – >“位置设置”中的设置?以下是简单的脚本:
<!DOCTYPE html> <html> <head> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-status-bar-style" content="black" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> <link rel="apple-touch-icon-precomposed" href="custom_icon_precomposed.png"/> <link rel="apple-touch-startup-image" href="apple-touch-icon-precomposed.png"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script> <script> jQuery(window).ready(function(){ jQuery("#btnInit").click(initiate_watchlocation); jQuery("#btnStop").click(stop_watchlocation); }); var watchProcess = null; function initiate_watchlocation() { if (watchProcess == null) { watchProcess = navigator.geolocation.watchPosition(handle_geolocation_query, handle_errors, {enableHighAccuracy:true}); } } function stop_watchlocation() { if (watchProcess != null) { navigator.geolocation.clearWatch(watchProcess); watchProcess = null; } } function handle_errors(error) { switch(error.code) { case error.PERMISSION_DENIED: alert("user did not share geolocation data"); break; case error.POSITION_UNAVAILABLE: alert("could not detect current position"); break; case error.TIMEOUT: alert("retrieving position timedout"); break; default: alert("unknown error"); break; } } function handle_geolocation_query(position) { var text = "Latitude: " + position.coords.latitude + "<br/>" + "Longitude: " + position.coords.longitude + "<br/>" + "Accuracy: " + position.coords.accuracy + "m<br/>" + "Time: " + new Date(position.timestamp); jQuery("#info").html(text); var image_url = "http://maps.google.com/maps/api/staticmap?sensor=false¢er=" + position.coords.latitude + ',' + position.coords.longitude + "&zoom=14&size=300x400&markers=color:blue|label:S|" + position.coords.latitude + ',' + position.coords.longitude; jQuery("#map").remove(); jQuery(document.body).append( jQuery(document.createElement("img")).attr("src", image_url).attr('id','map') ); } </script> </head> <body> <div> <button id="btnInit" >Monitor my location</button> <button id="btnStop" >Stop monitoring</button> </div> <div id="info"></div> </body> </html>好吧,我全神贯注地发现它必须等待设备准备就绪,然后调用你的window.ready jquery里面的那个来利用原生函数.以为我会发布这个作为一个菜鸟,它找到了我正在寻找的答案.
// Wait for PhoneGap to load // document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap is ready // function onDeviceReady() { jQuery(window).ready(function(){ jQuery("#btnInit").click(initiate_watchlocation); jQuery("#btnStop").click(stop_watchlocation); }); }