如何获取谷歌处理地理位置查询地图以显示来自用户位置的路线

时间:2014-08-15 09:31:47

标签: javascript jquery google-maps google-maps-api-3

var endlocation = {
    'center': '52.5606064,2.0312582999999904',
    'zoom': 10
};
var start = navigator.geolocation.getCurrentPosition(handle_geolocation_query);
var themap;
var destination = "Wednesbury, WS10 7TB";

$(document).ready(function () {
    $('#map').gmap({
        'center': endlocation.center,
        'zoom': endlocation.zoom,
        'disableDefaultUI': true,
        'callback': function () {
            themap = this;

            $('#submit').click(function () {
                themap.displayDirections({
                    'origin': start,
                    'destination': destination,
                    'travelMode': google.maps.DirectionsTravelMode.DRIVING,
                    'unitSystem': google.maps.UnitSystem.IMPERIAL
                }, {
                    'panel': document.getElementById('directions')
                },

                function (response, status) {
                    (status === 'OK') ? $('#results').show() : $('#results').hide();
                });
                return false;
            });
        }
    });
    navigator.geolocation.getCurrentPosition(handle_geolocation_query);
});

function handle_geolocation_query(position) {
    start = new google.maps.LatLng(lat, lon);
    themap.get('map').panTo(start);
}       

我似乎无法弄清楚我应该传递给handle_geolocation_query来获取地图 从用户位置向固定点发出指示。在此先感谢您的帮助,对不起,我是地图的菜鸟。继承人html:

<div id="map" style="height:500px;"><div>

1 个答案:

答案 0 :(得分:0)

如果其余代码有效,则应该与提交相同,但是在运行handle_geolocation_query函数时会自动执行。

function handle_geolocation_query(position) {
  start = new google.maps.LatLng(lat, lon);
  themap.displayDirections({
                'origin': start,
                'destination': destination,
                'travelMode': google.maps.DirectionsTravelMode.DRIVING,
                'unitSystem': google.maps.UnitSystem.IMPERIAL
            }, {
                'panel': document.getElementById('directions')
            },
            function (response, status) {
                (status === 'OK') ? $('#results').show() : $('#results').hide();
            });
 });