2011-09-30 49 views
6

我已经创建了一个脚本,它将使用地理位置库显示用户的位置,它一切正常。我已经使用PhoneGap导出了这个HTML 5脚本,并且可以在Settings-> Location Services中将My App设置为On。所以我假设每次运行我的应用程序时,我都不会得到常规提示“....想使用您当前的位置?”与选项不允许或确定。使用phoneGap的HTML 5地理位置

我不想让人们每次打开我的应用程序时单击允许。是否有一个原因,为什么该应用程序不使用服务 - >位置设置的设置?下面是一个简单的脚本:

<!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&center=" + 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> 

回答

2

OK我看了所有的地方,发现它,你必须等待设备准备就绪,然后调用里面,你window.ready jQuery来利用原生功能。以为我会张贴这个作为noob这是凝灰岩找到我正在寻找的答案。

 // 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); 
      }); 

     }