Google API最短路线

时间:2017-08-04 10:38:23

标签: routes shortest

嗨,谢谢你。我有一个路由和成本计算器脚本,但它没有采用最短路径。我已经尝试对此进行更改而无处可去。谁能帮忙。它需要计算最短的成本路线。这是代码:

请问您如何在此脚本中施加最短距离(驾驶)?

//ISO 3166-1 Alpha-2 country code, use 
http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#GB
var countrycode="GB"
//Rate per km (number)
var rateperkm=1.5;
//Minimum fare (number)
var minimum_fare=3;
//Currrency Symbol
var currencysymbol="£";
//Avoid motorways / highways? true/false
var avoidHighways=false;
//Avoid toll roads? true/false
var avoidTolls=false;
//Show summary? true/false
var showsummary=false;
//Show Route Map
var showroutemap=true;
//Disclaimer text
var disclaimer="Please be aware this is only an estimated fare and the final 
price will be quoted on request"
//API Key for map
var apikey="ADD YOUR KEY HERE";
//----------End Settings--------------------------------    
function initialize() 
{
var options = {componentRestrictions: {country: countrycode}};
var input = /** @type {HTMLInputElement} 
*/(document.getElementById('inputfrom'));
var searchBoxfrom = new google.maps.places.Autocomplete(input,options);
var input = /** @type {HTMLInputElement} 
*/(document.getElementById('inputto'));
var searchBoxto = new google.maps.places.Autocomplete(input,options);
}

function ftn_estimate()
{
if (document.getElementById('inputfrom').value!="" && 
document.getElementById('inputto').value!="")
{
    var origin = document.getElementById('inputfrom').value;
    var destination = document.getElementById('inputto').value;

    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
      {
        origins: [origin],
        destinations: [destination],
        travelMode: google.maps.TravelMode.DRIVING,
        unitSystem: google.maps.UnitSystem.METRIC,
        avoidHighways: avoidHighways,
        avoidTolls: avoidTolls,
      }, callback); 
    }
}

function callback(response, status) {
if (status != google.maps.DistanceMatrixStatus.OK) {
alert('Error was: ' + status);
} else {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;

for (var i = 0; i < origins.length; i++) {
  var results = response.rows[i].elements;

  for (var j = 0; j < results.length; j++) {

    if(showsummary)
    {
        document.getElementById('summary').innerHTML=origins[i] + ' to ' + 
destinations[j]  + ': ' + results[j].distance.text + ' in '+ 
results[j].duration.text;
        document.getElementById('summary').innerHTML+="<br /><font 
color='red'>" + disclaimer + "</font>"
    }
    document.getElementById('distance').value=
(results[j].distance.value/1000).toFixed(1);
    document.getElementById('time').value=
(results[j].duration.value/60).toFixed(1);

    var calc_fare=(results[j].distance.value/1000)*rateperkm;

    if (calc_fare<minimum_fare)
    {
        calc_fare=minimum_fare;
    }   

document.getElementById('fare').value=currencysymbol+calc_fare.toFixed(2);
  }
}

if (showroutemap)
{
    var origin = document.getElementById('inputfrom').value;
    var destination = document.getElementById('inputto').value;
    getpath(origin,destination);
    }
  }
}
google.maps.event.addDomListener(window, 'load', initialize);


function getpath(a,b) {
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer({
preserveViewport: true
});

directionsService.route({
origin: a,
destination:b,
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
  // directionsDisplay.setDirections(response);
var polyline = new google.maps.Polyline({
    path: [],
    strokeColor: '#0000FF',
    strokeWeight: 3
  });

var legs = response.routes[0].legs;

 for (i = 0; i < legs.length; i++) {
    var steps = legs[i].steps;
    for (j = 0; j < steps.length; j++) {
    var nextSegment = steps[j].path;
    for (k = 0; k < nextSegment.length; k++) {
        polyline.getPath().push(nextSegment[k]);
      }
    }
  }

do{
    var newpath = [];
        for (k = 0; k < polyline.getPath().length; k += 2) {
            newpath.push(polyline.getPath().getAt(k));
        }
        polyline.setPath(newpath);
}
while (polyline.getPath().length>1000)

var path = polyline.getPath();
var encodeString = google.maps.geometry.encoding.encodePath(path);


   document.getElementById('mapspan').innerHTML='<center><img 
 src="https://maps.googleapis.com/maps/api/staticmap?


size=400x400&path=weight:3%7Ccolor:red%7Cenc:'+encodeString
+'&key='+apikey+'" /></center>';

} else {
  window.alert('Directions request failed due to ' + status);
}
});
}

0 个答案:

没有答案