jQueryGeoComplete。在页面加载时显示用户的当前位置

jQueryGeoComplete。在页面加载时显示用户的当前位置,jquery,google-maps,mobile,geolocation,Jquery,Google Maps,Mobile,Geolocation,我正在为一个移动站点使用jquery.geocomlete.js——我希望它能够在地图上显示用户的当前位置。唯一的默认位置选项似乎是一组固定坐标或国家名称。是否可以说明如何在地理位置代码中实现这一点?以下是我目前拥有的: $(function(){ var options = { map: ".map_canvas", location: [-35.3075, 149.124417], map

我正在为一个移动站点使用jquery.geocomlete.js——我希望它能够在地图上显示用户的当前位置。唯一的默认位置选项似乎是一组固定坐标或国家名称。是否可以说明如何在地理位置代码中实现这一点?以下是我目前拥有的:

$(function(){
        var options = {
          map: ".map_canvas",              
          location: [-35.3075, 149.124417],
          mapOptions: {
            zoom: 3
          },
          zoom: 1,
          types: ['establishment'],              
          country: 'au'              
        };

        $("#geocomplete").geocomplete(options).val(); 

    });

谢谢你的帮助

您需要的是访问导航器对象的地理位置

if(navigator.geolocation){
    /*
     * getCurrentPosition() takes a function as a callback argument
     * The callback takes the position object returned as an argument
     */
    navigator.geolocation.getCurrentPosition(function(position){
        /**
         * pos will contain the latlng object
         * This must be passed into the setCenter instead of two float values
         */
        var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);  //
        map.setCenter(pos); //center the map based on users location
    }, function(){
        //client supports navigator object, but does not share their geolocation
    });
}else{
    //client doesn't support the navigator object
}
当然,上面的代码必须在实例化map实例之后出现

你可以

你也可以


最后

谢谢你的回复,为什么。我在地图代码之后添加了这个,现在它会询问我的位置,这很好。但是,我在将此代码应用到地图时遇到问题。我想我需要将它整合到“位置”行中。location:(navigator.geolocation)似乎不起作用,尝试将其添加到函数中也不起作用。你有什么想法吗?谢谢again@Desmond构建地图时,地图不需要传入任何
位置。您只需通过提供选项来实例化map对象,直到调用
setCenter(pos)
,然后map将加载中心,或者
setCenter(某些默认位置)
,如果它们不支持navigator@Desmond我会让你放心的,非常感谢你的帮助。地图正在使用基于位置的服务加载。我只需要弄清楚如何让Geocomplete搜索与它一起工作,我会在我完成后发回这里!再次感谢您抽出时间!