【问题标题】:Google Maps v3 Create routes between two pointsGoogle Maps v3 在两点之间创建路线
【发布时间】:2013-01-11 10:53:19
【问题描述】:

我正在使用 Google Maps API 开发网络应用程序。我正在尝试在两点之间创建一条路线,但由于某种原因,我还没有弄清楚如何创建它。以下是我的代码,如果我遗漏了什么,请告诉我。谢谢。

<script>
var Center=new google.maps.LatLng(18.210885,-67.140884);
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
var properties = {
    center:Center,
    zoom:20,
    mapTypeId:google.maps.MapTypeId.SATELLITE
};

map=new google.maps.Map(document.getElementById("map"), properties);
directionsDisplay.setMap(map);

var marker=new google.maps.Marker({
position:Center,
animation:google.maps.Animation.BOUNCE,
});

marker.setMap(map);

}

function Route() {

var start = new google.maps.LatLng(18.210885,-67.140884);
var end =new google.maps.latLng(18.211685,-67.141684);
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.WALKING
 };
 directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
  directionsDisplay.setDirections(result);
}
});
} 

google.maps.event.addDomListener(window,'load',initialize);
</script>

【问题讨论】:

  • 你在哪里调用函数Route()?我没有看到它在您的代码中的任何地方被调用。

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


【解决方案1】:

如果我满足以下条件,它对我有用:

  1. 调用路由函数
  2. 改变:

    var end =new google.maps.latLng(18.211685,-67.141684);
    

到:

    var end =new google.maps.LatLng(18.211685,-67.141684);

(javascript区分大小写,浏览器在javascript控制台报错)

working version

代码sn-p:

var Center = new google.maps.LatLng(18.210885, -67.140884);
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
  var properties = {
    center: Center,
    zoom: 20,
    mapTypeId: google.maps.MapTypeId.SATELLITE
  };

  map = new google.maps.Map(document.getElementById("map"), properties);
  directionsDisplay.setMap(map);

  var marker = new google.maps.Marker({
    position: Center,
    animation: google.maps.Animation.BOUNCE,
  });

  marker.setMap(map);
  Route();
}

function Route() {

  var start = new google.maps.LatLng(18.210885, -67.140884);
  var end = new google.maps.LatLng(18.211685, -67.141684);
  var request = {
    origin: start,
    destination: end,
    travelMode: google.maps.TravelMode.WALKING
  };
  directionsService.route(request, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(result);
    } else {
      alert("couldn't get directions:" + status);
    }
  });
}

google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map {
  margin: 0;
  padding: 0;
  height: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<div id="map"></div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-03
    • 2014-06-04
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 2017-03-14
    相关资源
    最近更新 更多