如何获得谷歌handle_geolocation_query地图显示方向从用户的位置

How to get google handle_geolocation_query map to display directions from users location

本文关键字:方向 用户 位置 显示 query 谷歌 何获得 handle geolocation 地图      更新时间:2023-09-26
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>

如果您的其余代码正常工作,这应该与提交相同,但会在运行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();
            });
 });