首页 文章

Google Maps API v3 Road Snapping - 如何使用有限的航路点数据提高准确性

提问于
浏览
0

我有一个应用程序正在接收有限的航路点数据,虽然谷歌 Map 道路捕捉做了令人钦佩的工作,根据该数据猜测正确的道路,我仍然遇到一定程度的不准确,这对我们的应用程序不起作用 .

Here's an example

在此示例中,紫色标记表示正在发送的真实“源”航点,而蓝色标记表示基于来自源航点的数据从Google返回的捕捉的航点数据 . 我正在使用捕捉的蓝色航点生成折线以显示路线(紫色折线),不幸的是,捕捉的航点和后续路线应该看起来更像红色折线 .

我已经在具有类似“源”路径点的API的在线演示中手动测试了这个,并且路线仍然捕捉到不正确的路线,所以我只能假设谷歌没有足够的数据准确捕捉 . 问题是,考虑到这样有限的数据,有没有办法提高正确快照的几率?有没有办法可以插入有限的源路点,试图“引导”谷歌提供更准确的快照?

这里's code similar to what I'米使用 - JSFiddle is here

//setup vars
var trip = [{
   "lat": -27.068,
   "lng": 153.1483
}, {
   "lat": -27.0642,
   "lng": 153.1546
}, {
   "lat": -27.0552,
   "lng": 153.156
}, {
   "lat": -27.0518,
   "lng": 153.1563
}, {
   "lat": -27.0503,
   "lng": 153.1552
}, {
   "lat": -27.0457,
   "lng": 153.1456
}, {
   "lat": -27.042,
   "lng": 153.1463
}, {
   "lat": -27.0349,
   "lng": 153.1476
}];

var unsnappedWaypoints = [];

var snappedWaypoints = [];

var map = new google.maps.Map(document.getElementById('map'), {
   zoom: 14,
   center: {
       lat: 0,
       lng: 0
   }
});

var bounds = new google.maps.LatLngBounds();

//add each waypoint to an array of lat/lngs
$.each(trip, function(key, waypoint) {

   unsnappedWaypoints.push(waypoint.lat + ',' + waypoint.lng);

   var marker = new google.maps.Marker({
       map: map,
       icon: 'http://mt.google.com/vt/icon/name=icons/spotlight/spotlight-ad.png',
       position: {
           lat: waypoint.lat,
           lng: waypoint.lng
       }
   });

});

//perform Google Maps API call with joined array for snapped results
$.ajax({
   url: 'https://roads.googleapis.com/v1/snapToRoads?path=' + unsnappedWaypoints.join('|') + '&key=AIzaSyA1JWR3ohBFQ_P7F5eSMSJb0dwV9PbB3pA&interpolate=true',
   crossDomain: true,
   dataType: 'jsonp'
}).done(function(response) {

   //iterate through returned waypoints to create array of lat/lngs for polyline
   $.each(response, function(key, snappedPoints) {
       $.each(snappedPoints, function(key, snappedPoint) {

           snappedWaypoints.push({
               lat: snappedPoint.location.latitude,
               lng: snappedPoint.location.longitude
           });

           //add snapped waypoints to map to show difference between originals and returned
           var marker = new google.maps.Marker({
               map: map,
               icon: 'http://mt.google.com/vt/icon?color=ff004C13&name=icons/spotlight/spotlight-waypoint-blue.png',
               position: {
                   lat: snappedPoint.location.latitude,
                   lng: snappedPoint.location.longitude
               }
           });

           //increase the bounds to take into account waypoints
           bounds.extend(new google.maps.LatLng(snappedPoint.location.latitude, snappedPoint.location.longitude));

       });
   });

   //create polyline from snapped waypoints
   var tripRoute = new google.maps.Polyline({
       path: snappedWaypoints,
       gseodesic: true,
       strokeColor: '#663496',
       strokeOpacity: 1.0,
       strokeWeight: 2
   });

   tripRoute.setMap(map);

   //fit these bounds to the map
   map.fitBounds(bounds);

});

1 回答

  • 0
    var ActualRoadpoints = routes[0].overview_path;
     var resolution=100;// This number you can change.But less the number add more points on road.
    
     getFineData(ActualRoadpoints, resolution).forEach(function (point) {
     var ActualRoadmarker = new google.maps.Marker({
                                    position: new google.maps.LatLng(point.lat(), point.lng()),
                                    map: map,
                                    // animation: google.maps.Animation.DROP,
                                    visible: false
                                });
    });
    
    //This function call internally to find more points on route
     function getFineData(roughData, resolution) {
                    var fineData = [];
                    var latLngA;
                    var latLngB;
                    var steps;
                    var step;
                    for (var i = 1; i < roughData.length; i++) {
                        latLngA = roughData[i - 1];
                        latLngB = roughData[i];
                        distanceDiff = google.maps.geometry.spherical.computeDistanceBetween(latLngA, latLngB);
                        steps = Math.ceil(distanceDiff / resolution);
                        step = 1 / steps;
                        previousInterpolatedLatLng = latLngA;
                        for (var j = 0; j < steps; j++) {
                            var interpolated = google.maps.geometry.spherical.interpolate(latLngA, latLngB, step * j);
                            fineData.push(interpolated)
                        }
                    }
                   // console.log(fineData.length)
                    return fineData;
                }
    

相关问题