简体   繁体   English

Phonegap构建地理位置不起作用

[英]Phonegap build Geolocation not working

js: i use simple alert to display the location, and use phonegap build to compile. js:我使用简单的警报来显示位置,并使用phonegap构建进行编译。 Device ready alert is shown! 显示设备就绪警报! But not any other alerts! 但没有其他任何警报!

document.addEventListener("deviceready", getDeviceLocation , false); 

    function  getDeviceLocation () {
         alert('device ready');      
         navigator.geolocation.getCurrentPosition(showPosition, showError, { enableHighAccuracy: true } );
    }

     function showPosition(position) {
        alert('show postion called');
          alert("Latitude: " + position.coords.latitude + 
                    "Longitude: " + position.coords.longitude);    
  }

  function showError(error) {
        alert('show error called');
        alert("Errorcode: "    + error.code    + 
                 "Errormessage: "+ error.message );
    }

html:the html page is as follows: html:html页面如下:

     <!DOCTYPE html>
      <html ng-app>
        <head>
             <link rel="stylesheet" href="style.css">
             <!--<link rel="stylesheet" href="bootstrap.css">--> 
            <!-- Latest compiled and minified CSS -->
            <link rel="stylesheet" href="bootstrap.css">
            <!-- Latest compiled and minified JavaScript -->
             <script src="angular.min.js"></script> 
              <script src="cordova.js"></script> 
             <script src="angular.animate.min.js"></script>
             <script src="angular.route.min.js"></script>     
             <link rel="stylesheet" href="font-awesome.css">
             <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        </head>
        <body>
</body>

     <script type="text/javascript" src="pro.js"></script>
    <script type="text/javascript" src="bootstrap.js"></script>
     <script type="text/javascript" src="script.js"></script>
  </html>

This code works for me. 该代码对我有用。 Hope will help you. 希望会对您有所帮助。

newMapForGuest: function()
{
    // setting here default latitude and longitude in case geolocation does not work.
    var defaultLatLng = new google.maps.LatLng(1.290, 103.851);
    if ( navigator.geolocation ) 
    {
        function success(pos) // function will call on success
        {
            alert('Latitude: '+ pos.coords.latitude +' Longitude: '+pos.coords.longitude);
        }

        function fail(error) // function will call on fail
        {
            alert('error message: ' +error);
        }
        // intializing geolocation to get current position
        navigator.geolocation.getCurrentPosition(success, fail, {maximumAge: 500000, enableHighAccuracy:true, timeout: 10000});
    } 
    else 
    {
        // in case geolocation does not work then call code mentioned here.
    }
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM