Google Map路线服务 [英] Google Map directions service

查看:88
本文介绍了Google Map路线服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以看到查看"示例(directions-simple.html).

You could see View example (directions-simple.html).

在上面的示例中,用户给出了from和to,但是我正在尝试获取 通过浏览器检测到用户位置,自动来自,并且用户输入了.

In the above example from and to is given by the user but i'm trying to get From automatically by detecting the user location through browser and To is been inputted by user.

我尝试了dpaste.com/695949,但无法正常工作.

I tried this dpaste.com/695949 but could not get it working.

提前谢谢!

推荐答案

在您的示例中,您在 http://dpaste.com/695949 .我清理了它的核心,获取了地理位置并在那里设置了地图中心.我认为您可以从那里找出路线指示,但是基本上,您需要为用户提供一个输入框,以输入他们想要去的位置,并且您可以使用当前地图中心作为起点指出您的路线.

You've got a number of JavaScript errors in your example at http://dpaste.com/695949. I cleaned up the core of it, getting the geolocation and setting the map center there. I think you can then go from there to figure out the directions piece, but basically you'll need to have an input box for your user to enter the location they want to go to, and you can use the current map center as the starting point for your directions.

var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

  function mapinit(position){
     directionsDisplay = new google.maps.DirectionsRenderer();
    var chicago = new google.maps.LatLng(41.850033, -87.6500523);
    var myOptions = {
      zoom:7,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: chicago
    }

    map = new google.maps.Map(document.getElementById("mapcont"), myOptions);
    directionsDisplay.setMap(map);
    initiate_geolocation()
  }
function initiate_geolocation() {  
                navigator.geolocation.getCurrentPosition(handle_position_results);

            }
function handle_position_results(position){
    newCenter = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
    map.setCenter(newCenter);
}

这篇关于Google Map路线服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆