如何使用方向路由对象来获取数据

时间:2014-12-18 13:00:10

标签: javascript google-maps-api-3

使用下面的代码,我显示两点之间的旅程。

现在我尝试使用方向结果对象和方向腿来获取变量中的步,腿或距离等信息,但我不知道如何使用此方法......

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var haight = new google.maps.LatLng(44.912198, -0.605530);
var oceanBeach = new google.maps.LatLng(44.893356, -0.739138);

function initialize() {

    directionsDisplay = new google.maps.DirectionsRenderer();
    var mapOptions = {
        zoom: 14,
        center: haight
    }
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    directionsDisplay.setMap(map);

    calcRoute()
}

calcRoute()

function calcRoute() {

    var request = {
        origin: haight,
        destination: oceanBeach,
        // Note that Javascript allows us to access the constant
        // using square brackets and a string value as its
        // "property."
        travelMode: google.maps.TravelMode.DRIVING
    };
    directionsService.route(request, function (response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
        }
    });
}

google.maps.event.addDomListener(window, 'load', initialize);

由于

1 个答案:

答案 0 :(得分:0)

我已用此代码解决了部分问题:

可变持续时间,路径和折线1都可以,我已经尝试过警报。

现在我想将这些变量保存在像sql这样的数据库中,我该怎么做?使用PHP?

function calcRoute() {
 var request = {
     origin: start,
  destination: end,
  travelMode: google.maps.TravelMode.WALKING
};
  directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
  var warnings = document.getElementById('warnings_panel');
  warnings.innerHTML = '<b>' + response.routes[0].warnings + '</b>';
  directionsDisplay.setDirections(response);
  showSteps(response);
}
 });
}

function showSteps(directionResult) {

 var myRoute = directionResult.routes[0].legs[0];
  for (var i = 0; i < myRoute.steps.length; i++) {


var duration = myRoute.steps[i].duration.text;

var path = myRoute.steps[i].path;

var polyline1 = myRoute.steps[i].distance.text;


}
}