我正在向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,因此该解决方案不适用于此处。
答案 0 :(得分:1)
根据the documentation,该API对单向道路返回1个响应,对两向道路返回2个响应,并且还包括原始输入点的索引:
对于每个有效的请求,Roads API将以请求URL中指定的格式返回响应。 “紧贴道路”响应中可能包含以下元素。
对于每个有效的请求,Roads API将以请求URL中指定的格式返回响应。 “紧贴道路”响应中可能包含以下元素。
snappedPoints -一系列捕捉点。每个点都包含以下字段:
- location —包含纬度和经度值。
- originalIndex —一个整数,指示原始请求中的相应值。请求中的每个点最多映射到响应中的两个段:
如果附近没有道路,则不会返回路段。
如果最近的道路是单向,则返回一个路段。
如果最近的道路是双向的,则返回两个路段。 placeId
因此,如果附近的道路是双向的,那么一个人期望的响应点数是请求中的两倍。
数据看起来很像,很多路都是两条路,返回两个点,但是数据使两个点相同。根据返回给您的请求的响应,返回了35点,所有原始点都在响应中表示。
proof of concept fiddle putting the response data into a MarkerClusterer
代码段:
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>