将当前设备位置保存在jQuery中的一个变量中

时间:2013-04-25 13:26:17

标签: jquery geolocation device

我想将设备当前位置保存到一个变量中。有可能吗?

1 个答案:

答案 0 :(得分:1)

您可以使用地理位置API获取当前位置。

http://mobile.tutsplus.com/tutorials/mobile-web-apps/html5-geolocation/

使用上面提到的API将变量保存在变量中:

var currentLocation; //this variable will store current location.
     jQuery(window).ready(function(){  
                jQuery("#btnInit").click(initiate_geolocation);  
            });  
            function initiate_geolocation() {  
              //store current location
              currentLocation =  navigator.geolocation.getCurrentPosition(handle_geolocation_query);  
            }  
            function handle_geolocation_query(position){  
                alert('Lat: ' + position.coords.latitude + ' ' +  
                      'Lon: ' + position.coords.longitude);  
            }