0

所以我试图这样做:

  • 输入地址
  • 获取地址的纬度/经度
  • 把它放在带有最终目的地(纬度/经度)的“方向”上
  • 在我的两个 div 中获取路线

听起来很容易。该脚本返回“OK”状态。所以你应该认为它会起作用,但不是。

<div id="map_canvas" class="pic50"></div>
<div id="map_directions" class="text50"></div>

bring_me_back('map_canvas','map_directions','some address');
<script>
// bring me back
var directionDisplay;
var directionsService = new google.maps.DirectionsService();

function bring_me_back(call_back_map,call_back_directions,address) {
    var latlng = new google.maps.LatLng(51.764696,5.526042); // where you're at
  directionsDisplay = new google.maps.DirectionsRenderer();
  var myOptions = {
    zoom: 14,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: false
  };

  var map = new google.maps.Map(document.getElementById(call_back_map),myOptions);
  directionsDisplay.setMap(map);
  directionsDisplay.setPanel(document.getElementById(call_back_directions));
  var marker = new google.maps.Marker({
    position: latlng,
    map: map,
    title:"My location"
  });

  // find the address
    geocoder = new google.maps.Geocoder (map);
    geocoder.geocode ( { 'address': address }, function(results, status)  {
    if (status == google.maps.GeocoderStatus.OK)  {
        var end = results [0].geometry.location; // where you need to go
      map.setCenter(results [0].geometry.location);
      marker.setPosition(results [0].geometry.location);
          var start = "51.764696,5.526042"; // where you're at (random place)
          var request = {
            origin:start,
            destination:end,
            travelMode: google.maps.DirectionsTravelMode.WALKING
          };
          directionsService.route(request, function(response, status) {
             alert("Directions was not successful for the following reason: " + status);
          });
    } 
    else {
      $('#'+call_back).html("Geocode was not successful for the following reason: " + status);
    }
  });
}
</script>

我知道我在做一些愚蠢的事情,因为我的错误控制台显示:目标:[我的网站]# 未找到

有趣的是,地图确实以带有标记的地理定位器的正确位置为中心。所以除了推回方向它做得很好(我认为)

4

1 回答 1

0

您在 DirectionsService 回调例程中不执行任何操作,除了输出消息“由于以下原因,路线不成功:” + 状态:

      directionsService.route(request, function(response, status) {
         alert("Directions was not successful for the following reason: " + status);
      });

如果要显示路线,需要调用DirectionsRenderer

尝试这样的事情:

directionsService.route(request, function(response, status) {
  if (status == google.maps.DirectionsStatus.OK) {
    directionsDisplay.setDirections(response);
  } else  alert("Directions was not successful for the following reason: " + status);
});
于 2013-04-02T21:45:07.813 回答