【问题标题】:How to use GPS to get the real time position?如何使用 GPS 获取实时位置?
【发布时间】:2016-03-12 10:44:49
【问题描述】:

我已经用 Jboss 工具(IDE:Eclipse)安装了cordova;我需要学习如何使用 GPS 来获取我的实时位置,比如手机中的应用程序谷歌地图?

【问题讨论】:

    标签: cordova jquery-mobile hybrid-mobile-app


    【解决方案1】:

    要找到当前位置需要安装cordova geolocation插件,然后使用以下代码

    function CheckCurrentLocation(){
    
      if( navigator.geolocation )    {
          var options={maximumAge:60000, timeout:5000, enableHighAccuracy:true };
                  navigator.geolocation.getCurrentPosition( success, fail);
                  }  
                  else  {          alert("Sorry, your browser does not support geolocation services.");  }  }
    
    function success(position)  {
         alert( "latitude :"+position.coords.latitude +", longititude : "+position.coords.longitude); 
    
             var userLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                writeAddressName(userLatLng);
       function writeAddressName(userLatLng) {
              var geocoder = new google.maps.Geocoder();
                geocoder.geocode({
              "location": userLatLng
            },
            function(results, status) {
               if (status == google.maps.GeocoderStatus.OK)
               {
    
              alert("first address is : "+results[0].address_components[0].long_name);
                   alert("Street Name is : "+results[0].address_components[1].long_name);
                   alert("CityName  is : "+results[0].address_components[2].long_name);
                   alert("Dristic name is : "+results[0].address_components[3].long_name);
                   alert("State name is : "+results[0].address_components[4].long_name);
                   alert("Country name is : "+results[0].address_components[5].long_name);
    
                }
              else{
                alert( "Unable to retrieve your address" );
                }
            });
          }
          function fail(){
          alert(" Fail!, To find the location");
          }
          }
    

    在上面的代码中google.maps.Geocoder();用于从谷歌地图中查找地址,要使用它,您必须包含以下脚本。

    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?AIzaSyA2E0vmaArSYL8UH5-NFzYfMsx7QvGK6Lg&sensor=true"> </script>
    <script type="text/javascript" src="http://maps.google.com/maps?file=api&v=3&key= your key" ></script>
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多