嗨,我想创建一个地图,当调用一个函数时,它会根据用户当前的地理位置生成指向某个点的方向。我已经尝试过这样做,但到目前为止我的尝试都没有结果,这是我当前的 JS 代码:
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var start = latlon;
var end = "Darwin, NSW, Australia";
var request =
{
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.DRIVIN
};
function initialize()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
function showPosition(position)
{
directionsDisplay = new google.maps.DirectionsRenderer();
var latlon = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var mapOptions =
{
zoom:15,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: latlon
}
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
directionsDisplay.setMap(map);
}
}
function getdirections()
{
directionsService.route(request, function(response, status)
{
if (status == google.maps.DirectionsStatus.OK)
{
directionsDisplay.setDirections(response);
}
});
}
任何帮助将不胜感激,但请记住,它必须使用谷歌地图 api,v3。谢谢 :)