【问题标题】:Google Maps - directions from users geo-location谷歌地图 - 来自用户地理位置的方向
【发布时间】:2013-01-13 06:14:39
【问题描述】:

我有以下功能,可以检查浏览器是否支持地理位置,然后获取用户的地理位置并将其放在地图上。

我需要添加什么才能让我从用户的地理位置为用户提供指向固定位置(这不会改变)的方向?

if (navigator.geolocation)
{
  navigator.geolocation.getCurrentPosition(function(position)
  {                                                              
    var latitude = position.coords.latitude;                    
    var longitude = position.coords.longitude;                 
    var coords = new google.maps.LatLng(latitude, longitude);
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    var mapOptions = 
    {
      zoom: 15,  
      center: coords, 
      mapTypeControl: true, 
      navigationControlOptions:
      {
        style: google.maps.NavigationControlStyle.SMALL
      },
       mapTypeId: google.maps.MapTypeId.ROADMAP
    };
     map = new google.maps.Map(document.getElementById("mapContainer"), mapOptions);
     var marker = new google.maps.Marker( 
       {
         position: coords, 
         map: map,                 
        });
   });
}
else
{
   alert("Geolocation API is not supported in your browser.");

我已将此函数添加到我的代码中:

  function calcRoute() {
    var start = position;
    var end = "London";
    var request = {
      origin: start,
      destination: end,
      travelMode: google.maps.TravelMode.DRIVING
    };
    directionsService.route(request, function (result, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(result);
      }
    });
  }

HTML: <div id="mapContainer" onload="calcRoute()"></div>

但它似乎仍然无法正常工作。

【问题讨论】:

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


    【解决方案1】:

    获得position 后,使用方向服务在地图上呈现方向。

    你可以在这里找到示例代码-https://developers.google.com/maps/documentation/javascript/directions#DisplayingResults

    【讨论】:

      【解决方案2】:

      用下面的代码搞定了:

      if (navigator.geolocation) { //Checks if browser supports geolocation
         navigator.geolocation.getCurrentPosition(function (position) {                                                              //This gets the
           var latitude = position.coords.latitude;                    //users current
           var longitude = position.coords.longitude;                 //location
           var coords = new google.maps.LatLng(latitude, longitude); //Creates variable for map coordinates
           var directionsService = new google.maps.DirectionsService();
           var directionsDisplay = new google.maps.DirectionsRenderer();
           var mapOptions = //Sets map options
           {
             zoom: 15,  //Sets zoom level (0-21)
             center: coords, //zoom in on users location
             mapTypeControl: true, //allows you to select map type eg. map or satellite
             navigationControlOptions:
             {
               style: google.maps.NavigationControlStyle.SMALL //sets map controls size eg. zoom
             },
             mapTypeId: google.maps.MapTypeId.ROADMAP //sets type of map Options:ROADMAP, SATELLITE, HYBRID, TERRIAN
           };
           map = new google.maps.Map( /*creates Map variable*/ document.getElementById("map"), mapOptions /*Creates a new map using the passed optional parameters in the mapOptions parameter.*/);
           directionsDisplay.setMap(map);
           directionsDisplay.setPanel(document.getElementById('panel'));
           var request = {
             origin: coords,
             destination: 'BT42 1FL',
             travelMode: google.maps.DirectionsTravelMode.DRIVING
           };
      
           directionsService.route(request, function (response, status) {
             if (status == google.maps.DirectionsStatus.OK) {
               directionsDisplay.setDirections(response);
             }
           });
         });
       }
      

      【讨论】:

      • 它工作正常。但是我如何自定义我的家庭位置的工具提示标记?假设我想在目的地显示我的机构名称?
      猜你喜欢
      • 1970-01-01
      • 2017-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多