GoogleMaps优化航路点并与原始序列相关 [英] GoogleMaps Optimize waypoints and relate to original sequence
问题描述
我正在使用googlemaps api路由和优化数据库中的一组路标.航路点以随机顺序输入.一旦通过googlemaps优化了航路点以创建正确的航路顺序,我就需要一种将它们与原始序列相关联的方法,以便可以在数据库中更新航路顺序.
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.
例如,我来自数据库的原始数据可能如下所示(LoadNum为空白开始):
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
在GoogleMaps优化路线后,可以将地址稍微更改为与每个街道上最接近的地址,如下所示;以及范围;
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还返回了一个路由段号,我想将其分配为数据库中的负荷号,但是我没有办法保证匹配.如果我可以将标识符(OrderNumber)传递给googlemaps,并返回带有理想记录的记录,那么即使地址已更改,我也知道订单号'x'是第1个停靠点.
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.
下面的代码片段,首先是JavaScript,在底部附近,我将routeSegment和EndAddress捕获到一个变量中(实际上是一个文本字段,现在我可以看到它):
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;
}
然后在后面的代码中,我想做一个数据库更新,其中"AddressInDB匹配从JavaScript返回的地址".
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();
}
在我的示例中,参数是城市"和州",但这不够具体.总而言之,是否有一种方法可以通过将返回/维护的GoogleMaps Javascript API传递一个识别参数,或者有另一种方法来实现我的追求?
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?
谢谢.
推荐答案
DirectionsRoute对象(代码中的 route
变量)包含定义原始航点和重新排序的航点之间关系的信息.看看文档中的属性 waypoint_order
:
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
如果 optimizeWaypoints 设置为 true ,则此字段将包含输入航路点的重新排列.例如,如果输入为:
If optimizeWaypoints was set to true, this field will contain the re-ordered permutation of the input waypoints. For example, if the input was:
来源:洛杉矶
终点:达拉斯,班戈,凤凰城
Waypoints: Dallas, Bangor, Phoenix
目的地:纽约
,优化后的输出顺序如下:
and the optimized output was ordered as follows:
来源:洛杉矶
终点:凤凰城,达拉斯,班戈
Waypoints: Phoenix, Dallas, Bangor
目的地:纽约
然后该字段将是一个包含值[2,0,1]的数组.请注意,航点编号是从零开始的.如果将任何输入航路点的中途停留点设置为 false ,则此字段为空,因为路线优化不适用于此类查询.
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.
这篇关于GoogleMaps优化航路点并与原始序列相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!