Google Roads API 的 Nearest Roads 函数返回的点数多于给定点,包含重复项
Posted
技术标签:
【中文标题】Google Roads API 的 Nearest Roads 函数返回的点数多于给定点,包含重复项【英文标题】:Google Roads API's Nearest Roads function returns more points than given, contains duplicates 【发布时间】:2019-03-10 05:52:56 【问题描述】:我正在向 Google 的 Roads API 发送一些点,以将它们调整为道路。请注意,这是“最近的道路”功能,而不是“对齐道路”功能,因此这些点不会形成路径。当我得到我的结果时,返回的分数比给定的多,但其中许多分数是重复的。当我删除重复项时,比给出的要少。 该问题可以通过以下 URL 和点集重现,只需输入您自己的 API 密钥即可。我提供了 20 分,但最终得到了 35 分。
'https://roads.googleapis.com/v1/nearestRoads?points=33.9882659912109,-118.47038269043001|33.9992523193359,-118.462142944336|33.9937591552734,-118.447036743164|33.9951324462891,-118.456649780273|33.991012573242195,-118.455276489258|33.9827728271484,-118.469009399414|33.9882659912109,-118.464889526367|33.981399536132805,-118.463516235352|34.003372192382805,-118.462142944336|33.986892700195305,-118.47175598144501|33.997879028320305,-118.463516235352|34.003372192382805,-118.452529907227|33.9882659912109,-118.474502563477|33.996505737304695,-118.47175598144501|33.981399536132805,-118.466262817383|34.001998901367195,-118.463516235352|33.996505737304695,-118.46076965331999|33.9937591552734,-118.466262817383|33.9992523193359,-118.469009399414|33.9937591552734,-118.463516235352&key=INSERT_YOUR_API_KEY_HERE'
我发现 this question 有类似的问题,但它特定于 Snap To Roads,因此该解决方案不适用于此处。
【问题讨论】:
【参考方案1】:根据the documentation,该 API 返回 1 个单向道路响应,2 个双向道路响应,并且还包括原始输入点的索引:
对于每个有效请求,Roads API 将以请求 URL 中指示的格式返回响应。 Snap to Roads 响应中可能存在以下元素。
对于每个有效请求,Roads API 将以请求 URL 中指示的格式返回响应。 Snap to Roads 响应中可能存在以下元素。
snappedPoints — 一组捕捉点。每个点由以下字段组成:
location - 包含纬度和经度值。 originalIndex — 一个整数,指示原始请求中的相应值。请求中的每个点最多映射到响应中的两个段:如果附近没有道路,则不返回任何路段。 如果最近的道路是单向的,则返回一个路段。 如果最近的道路是双向的,则返回两个路段。 地点标识
因此,如果附近的道路是双向的,那么响应中的点数将是请求中的两倍。
从您的数据看来,许多道路是两条路,返回两个点,但数据是这样的,这两个点是相同的。根据您的请求返回的响应,返回了 35 个点,所有原始点都在响应中表示。
proof of concept fiddle putting the response data into a MarkerClusterer
代码sn-p:
var geocoder;
var map;
function initialize()
var map = new google.maps.Map(
document.getElementById("map_canvas"),
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
);
var bounds = new google.maps.LatLngBounds();
var gmarkers = [];
console.log("snappedPoints.length=" + roadsApiSnapToRoadResult.snappedPoints.length);
for (var i = 0; i < roadsApiSnapToRoadResult.snappedPoints.length; i++)
var marker = new google.maps.Marker(
position:
lat: roadsApiSnapToRoadResult.snappedPoints[i].location.latitude,
lng: roadsApiSnapToRoadResult.snappedPoints[i].location.longitude,
,
icon:
url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle.png",
size: new google.maps.Size(7, 7),
anchor: new google.maps.Point(3.5, 3.5)
,
title: "" + roadsApiSnapToRoadResult.snappedPoints[i].originalIndex,
map: map
);
gmarkers.push(marker);
bounds.extend(marker.getPosition());
map.fitBounds(bounds);
var markerClusterer = new MarkerClusterer(map, gmarkers,
imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m',
maxZoom: 18
);
google.maps.event.addDomListener(window, "load", initialize);
var roadsApiSnapToRoadResult =
"snappedPoints": [
"location":
"latitude": 33.988152465043711,
"longitude": -118.4704415352344
,
"originalIndex": 0,
"placeId": "ChIJ07TI4bi6woARVzS3zNDS75A"
,
"location":
"latitude": 33.999373102445233,
"longitude": -118.46239768997125
,
"originalIndex": 1,
"placeId": "ChIJVwy5xOm6woARsndu3i5UqVw"
,
"location":
"latitude": 33.999373102445233,
"longitude": -118.46239768997125
,
"originalIndex": 1,
"placeId": "ChIJVwy5xOm6woARs3du3i5UqVw"
,
"location":
"latitude": 33.993767373940663,
"longitude": -118.44703991835411
,
"originalIndex": 2,
"placeId": "ChIJHUeXXoy6woARSrO6Q7pKMCc"
,
"location":
"latitude": 33.993767373940663,
"longitude": -118.44703991835411
,
"originalIndex": 2,
"placeId": "ChIJHUeXXoy6woARS7O6Q7pKMCc"
,
"location":
"latitude": 33.995020152633714,
"longitude": -118.45642063549312
,
"originalIndex": 3,
"placeId": "ChIJbdaMr-y6woAR7kSZm8ROfMc"
,
"location":
"latitude": 33.995020152633714,
"longitude": -118.45642063549312
,
"originalIndex": 3,
"placeId": "ChIJbdaMr-y6woAR70SZm8ROfMc"
,
"location":
"latitude": 33.991049979984176,
"longitude": -118.45535641955625
,
"originalIndex": 4,
"placeId": "ChIJtb81pZO6woARcCdVIfSh_G8"
,
"location":
"latitude": 33.991049979984176,
"longitude": -118.45535641955625
,
"originalIndex": 4,
"placeId": "ChIJtb81pZO6woARcSdVIfSh_G8"
,
"location":
"latitude": 33.98285945845808,
"longitude": -118.46908608980472
,
"originalIndex": 5,
"placeId": "ChIJISAPfbu6woARyKxdnrZG5oE"
,
"location":
"latitude": 33.98285945845808,
"longitude": -118.46908608980472
,
"originalIndex": 5,
"placeId": "ChIJISAPfbu6woARyaxdnrZG5oE"
,
"location":
"latitude": 33.988333802620495,
"longitude": -118.46480571557542
,
"originalIndex": 6,
"placeId": "ChIJs7rlQb66woARIeOfKMcdQX4"
,
"location":
"latitude": 33.981527850209972,
"longitude": -118.4636347455167
,
"originalIndex": 7,
"placeId": "ChIJnZB4paK6woARXLQoGfodVh0"
,
"location":
"latitude": 33.981527850209972,
"longitude": -118.4636347455167
,
"originalIndex": 7,
"placeId": "ChIJnZB4paK6woARXbQoGfodVh0"
,
"location":
"latitude": 34.003497235152565,
"longitude": -118.46240183188755
,
"originalIndex": 8,
"placeId": "ChIJd5Hp-ue6woARatY78BByNBg"
,
"location":
"latitude": 34.003497235152565,
"longitude": -118.46240183188755
,
"originalIndex": 8,
"placeId": "ChIJd5Hp-ue6woARa9Y78BByNBg"
,
"location":
"latitude": 33.98685767022473,
"longitude": -118.47172496327262
,
"originalIndex": 9,
"placeId": "ChIJ3TKdobm6woARS2DIe-NRymk"
,
"location":
"latitude": 33.997747613047771,
"longitude": -118.46341368261827
,
"originalIndex": 10,
"placeId": "ChIJc3IO_um6woARWgni7EMZaJ0"
,
"location":
"latitude": 33.997747613047771,
"longitude": -118.46341368261827
,
"originalIndex": 10,
"placeId": "ChIJc3IO_um6woARWwni7EMZaJ0"
,
"location":
"latitude": 34.00333556691659,
"longitude": -118.45250243554641
,
"originalIndex": 11,
"placeId": "ChIJK7ABtu-6woARyCzwvT7-vco"
,
"location":
"latitude": 34.00333556691659,
"longitude": -118.45250243554641
,
"originalIndex": 11,
"placeId": "ChIJK7ABtu-6woARySzwvT7-vco"
,
"location":
"latitude": 33.988346309645785,
"longitude": -118.4743705301974
,
"originalIndex": 12,
"placeId": "ChIJ1TslIri6woARQ_h9FkDa-yc"
,
"location":
"latitude": 33.9964313646278,
"longitude": -118.47170049959587
,
"originalIndex": 13,
"placeId": "ChIJhZYhN8S6woAR5pUEEEhYbCU"
,
"location":
"latitude": 33.9964313646278,
"longitude": -118.47170049959587
,
"originalIndex": 13,
"placeId": "ChIJhZYhN8S6woAR55UEEEhYbCU"
,
"location":
"latitude": 33.981307554994437,
"longitude": -118.46641918691026
,
"originalIndex": 14,
"placeId": "ChIJxTT5KKO6woAR_uY0J0oCLDU"
,
"location":
"latitude": 33.981307554994437,
"longitude": -118.46641918691026
,
"originalIndex": 14,
"placeId": "ChIJxTT5KKO6woAR_-Y0J0oCLDU"
,
"location":
"latitude": 34.002195599071726,
"longitude": -118.46391163712804
,
"originalIndex": 15,
"placeId": "ChIJqXQkrcK6woAR_sD5qVq2Yl8"
,
"location":
"latitude": 34.002195599071726,
"longitude": -118.46391163712804
,
"originalIndex": 15,
"placeId": "ChIJqXQkrcK6woAR_8D5qVq2Yl8"
,
"location":
"latitude": 33.99648760375095,
"longitude": -118.46073154820924
,
"originalIndex": 16,
"placeId": "ChIJl0OqT-q6woARstLIStpm2NU"
,
"location":
"latitude": 33.99648760375095,
"longitude": -118.46073154820924
,
"originalIndex": 16,
"placeId": "ChIJl0OqT-q6woARs9LIStpm2NU"
,
"location":
"latitude": 33.993670902273607,
"longitude": -118.46619470748932
,
"originalIndex": 17,
"placeId": "ChIJWes6V8C6woARc4NO9L0r978"
,
"location":
"latitude": 33.999376276569294,
"longitude": -118.46882770430223
,
"originalIndex": 18,
"placeId": "ChIJ2TxBfcO6woAR4oW0l5gn22c"
,
"location":
"latitude": 33.999376276569294,
"longitude": -118.46882770430223
,
"originalIndex": 18,
"placeId": "ChIJ2TxBfcO6woAR44W0l5gn22c"
,
"location":
"latitude": 33.99367154305785,
"longitude": -118.46333135141523
,
"originalIndex": 19,
"placeId": "ChIJL1Q9H8C6woAR6JtmbvA0dcU"
,
"location":
"latitude": 33.99367154305785,
"longitude": -118.46333135141523
,
"originalIndex": 19,
"placeId": "ChIJL1Q9H8C6woAR6ZtmbvA0dcU"
]
html,
body,
#map_canvas
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
<script src="https://cdn.rawgit.com/googlemaps/v3-utility-library/master/markerclustererplus/src/markerclusterer.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?ext=.js"></script>
<div id="map_canvas"></div>
【讨论】:
以上是关于Google Roads API 的 Nearest Roads 函数返回的点数多于给定点,包含重复项的主要内容,如果未能解决你的问题,请参考以下文章
Google Maps 的 Roads API 不适用于 XHR
Google Maps Roads API 返回重复的坐标和 placeIds
Google Maps Roads API:让“快速上路”更加宽容
如何使用带有 Google Maps Roads API 的坐标列表计算距离