问题
I'm using googlemaps api to route and optimize a set of waypoints from a Database. The waypoints are fed in, in random order. Once the waypoints are optimized via googlemaps to create a proper route order, I need a way to correlate them to the original sequence so I can update the route order in a database.
For example my original data from the DB might look like the below (LoadNum is blank to start):
LoadNum Order address City State Zip
blank 1 456 Elm Street Chicago IL 12345
blank 2 123 Main Street Cleveland OH 12345
blank 3 678 Market Street Beverly Hills CA 90210
After GoogleMaps Optimizes the route the addresses could be changed a little to the nearest match on each of those streets, as in below with the ranges;
LoadNum Order address City State Zip
blank 2 120-160 Main Street Cleveland OH 12345
blank 1 200-300 Elm Street Chicago IL 12345
blank 3 678 Market Street Beverly Hills CA 90210
GoogleMaps also returns a route segment number that I want to assign as loadnumber in the database, but I don't have a way to guarantee the match. If I could pass an identifier (OrderNumber) to googlemaps, and have it returned with that record that would be ideal, so even if the address has changed I know order number 'x' is stop 1.
Code snippets below, first is the JavaScript, near the bottom I'm capturing routeSegment and EndAddress into a variable (actually a text field for now so I can see it):
directionsService.route({
origin: document.getElementById('StartBoxAjax').value,
destination: document.getElementById('EndBoxAjax').value,
waypoints: waypts,
optimizeWaypoints: true,
travelMode: 'DRIVING'
}, function (response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response);
var route = response.routes[0];
var string = [];
document.getElementById('<%=JScriptParm.ClientID %>').value = ""
var summaryPanel = document.getElementById('directions-panel');
summaryPanel.innerHTML = '';
// For each route, display summary information.
for (var i = 0; i < route.legs.length; i++) {
var routeSegment = i + 1;
summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment +
'</b><br>';
summaryPanel.innerHTML += route.legs[i].start_address + ' to ';
summaryPanel.innerHTML += route.legs[i].end_address + '<br>';
summaryPanel.innerHTML += route.legs[i].distance.text + '<br><br>';
//document.getElementById('JScriptParm').value += routeSegment;
document.getElementById('<%=JScriptParm.ClientID %>').value += routeSegment + ',' + route.legs[i].end_address + '|';
// string += routeSegment + ',' + route.legs[i].end_address + '|';
// document.getElementById('<%=JScriptParm.ClientID %>').value = string;
}
Then in code behind I would like to do a DB update where "AddressInDB matches Address returned from JavaScript".
protected void AssignLoadNum(string city, string state, string stop)
{
//Call Stored proc to update load number.
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
string connStr = ConfigurationManager.ConnectionStrings["FurnitureDB"].ConnectionString;
conn.ConnectionString = connStr;
conn.Open();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Connection = conn;
cmd.Parameters.Add("@state", SqlDbType.VarChar).Value = state;
cmd.Parameters.Add("@city", SqlDbType.VarChar).Value = city;
cmd.Parameters.Add("@stop", SqlDbType.Int).Value = Convert.ToInt32(stop);
cmd.CommandText = "UpdateLoadOrd";
cmd.ExecuteNonQuery();
//var dt = new DataTable();
}
In my example the parameters are City and State, but that is not going to be specific enough. The long and short of it is, is there a way to pass an identifying parameter through GoogleMaps Javascript API that will be returned/maintained, or is there another way to achieve what I'm after?
Thanks.
回答1:
The DirectionsRoute object (route variable in your code) contains information that defines relationship between original waypoints and re-ordered waypoints. Have a look at the property waypoint_order in the documentation:
https://developers.google.com/maps/documentation/javascript/reference#DirectionsRoute
If optimizeWaypoints was set to true, this field will contain the re-ordered permutation of the input waypoints. For example, if the input was:
Origin: Los Angeles
Waypoints: Dallas, Bangor, Phoenix
Destination: New York
and the optimized output was ordered as follows:
Origin: Los Angeles
Waypoints: Phoenix, Dallas, Bangor
Destination: New York
then this field will be an Array containing the values [2, 0, 1]. Note that the numbering of waypoints is zero-based. If any of the input waypoints has stopover set to false, this field will be empty, since route optimization is not available for such queries.
来源:https://stackoverflow.com/questions/47764868/googlemaps-optimize-waypoints-and-relate-to-original-sequence