【问题标题】:Google Maps API: find a route between two points in IsraelGoogle Maps API:查找以色列两点之间的路线
【发布时间】:2011-08-08 14:36:27
【问题描述】:

我正在尝试在我的 html 页面中手动输入的两点之间找到一条路线,但我得到了 ZERO_RESULTS 返回值。

用户输入两个位置 src_address = "Tel Aviv" 和 dst_address = "Haifa"。

我通过为每个地址调用 geocoder.geocode 两次来获取它们的几何位置。然后设置: src_latlng = 结果[0].geometry.location; dst_latlng = 结果[0].geometry.location;

但是,当我询问路由器时,我得到 ZERO_RESULTS:

代码如下:

    <!DOCTYPE html>
<html>
<img src="globe123.jpg" width="72" height="75"/> 
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps JavaScript API v3 Example: Geocoding Simple</title>
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>



<script type="text/javascript">
  var directionDisplay;
  var directionsService = new google.maps.DirectionsService();

  var geocoder;
  var map;
  var geodesic;
  var poly;
  var src_latlng;
  var dst_latlng;

  function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();

    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);


    var polyOptions = {
      strokeColor: '#FF0000',
      strokeOpacity: 1.0,
      strokeWeight: 3
    }
    poly = new google.maps.Polyline(polyOptions);
    poly.setMap(map);
    directionsDisplay.setMap(map);
    // Add a listener for the click event
    //google.maps.event.addListener(map, 'click', addLocation);
  }

  function codeAddress() {
    var src_address = document.getElementById("SRC_ADDR").value;
    var dst_address = document.getElementById("DST_ADDR").value;

    geocoder.geocode( { 'address': src_address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var path = poly.getPath();
        path.push(results[0].geometry.location);
        src_latlng = results[0].geometry.location;
        var marker = new google.maps.Marker({
            map: map, 
            position: results[0].geometry.location
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
    geocoder.geocode( { 'address': dst_address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        map.setZoom(8);

        var path = poly.getPath();
        path.push(results[0].geometry.location);
        dst_latlng = results[0].geometry.location;
        var marker = new google.maps.Marker({
            map: map, 
            position: results[0].geometry.location
        });


        calcRoute();

      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });



  }

  function calcRoute() {



      var start = src_latlng;
      var end = dst_latlng;
      var request = {
        origin:start, 
        destination:end ,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
      };

      directionsService.route(request, function(result, status) {

        if (status == google.maps.DirectionsStatus.OK) {         
          directionsDisplay.setDirections(result);
        } else {
            alert(status);            
        }    
        });
    }


</script>
</head>
<body onload="initialize()">


  <div style="text-align:left">
  Source: <input id="SRC_ADDR" type="textbox" value="Tel-Aviv, Israel">
  Destination: <input id="DST_ADDR" type="textbox" value="Haifa, Israel">
  <input type="button" value="Calculate Travel Time" onclick="codeAddress()">
  </div>
  <div id="map_canvas" style="height:40%;width:40%;top:120px"></div>
  <!-- <div id="map_canvas" style="width: 320px; height: 480px;"></div> 
  -->



</body>
</html>

【问题讨论】:

  • 我不确定,但看起来问题是来源和目的地不在美国时的原因!
  • 我已经添加了html页面的完整代码。
  • 我做到了。抱歉,我没有注意到我应该接受。我对stackoverflow有点陌生。现在我正在这样做。

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


【解决方案1】:

Google 尚未覆盖以色列的 Maps API,因此 directionsService.route 函数调用会返回 ZERO_RESULTS 状态。

请参阅 Google 的受支持区域的地图覆盖范围详细信息工作表 here。电子表格的直接链接是here。

【讨论】:

  • 确实,以色列不在。但是,为什么在 maps.google.com 中搜索对以色列有用?
  • 实际上,我并不完全确定。 :)
【解决方案2】:

您好,您可以使用此代码获取来源和目的地之间的路线

函数 setRoute()
{
      var fromAddress=document.getElementById("txtfromAddress").value;
      var toAddress=document.getElementById("txttoAddress").value;
      if(fromAddress!="" && toAddress!="")
      {
       if(!fromAddress && !toAddress)
       {
       alert('找不到路由');
       }
       GDir1.load("从:" + fromAddress +" 到:" + toAddress);


      }
      别的
      {
        alert('需要路线指示');
      }
}

【讨论】:

  • 抱歉,但这仍然不适用于以色列地图。看起来谷歌地图在 API 中不支持以色列
【解决方案3】:

*http://gmaps-samples.googlecode.com/svn/trunk/mapcoverage_filtered.html* 查看它是否涵盖您所在的地区它包括街景或卫星视图等的详细视图是否可用 总账

【讨论】:

    猜你喜欢
    • 2016-06-06
    • 1970-01-01
    • 2010-11-03
    • 2013-01-11
    • 2016-04-22
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多