Google Snap to Roads

时间:2016-09-03 20:45:22

标签: google-maps

我的数据库中有一组lat long,我希望将它们显示在通过道路后面的一条线连接/绘制在一起的地图上。我在我的数据库中的一些lat不在路上,但我希望他们像跟踪应用程序一样在连接在一起的道路上查看。

我的代码显示了lats longs(坐标),但没有跟随道路,或者坐标没有在道路上拍摄。

这是我的代码:

`function initialize() {

 @foreach($track as $transac)
    lati = {{$transac->latitude}}; 
    lngi = {{$transac->longitude}};
  @endforeach

  var mapOptions = {
    zoom: 17,
    center: {lat: lati, lng: lngi}
  };

  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

var locate = [
@foreach($transactions as $location)
        ['{{ $location->address }}<br> {{ $location->details }}', {{ $location->latitude }}, {{ $location->longitude }}],   
@endforeach
];

var alltransaction = [
@foreach($alltransactions as $location)
        ['{{ $location->address }}<br> {{ $location->details }}', {{ $location->latitude }}, {{ $location->longitude }}],   
@endforeach
];

  var marker, i;

  marker = new google.maps.Marker({
  position: new google.maps.LatLng(lati,lngi),
  map: map,
  icon: 'images/car.jpg'
  });

runSnapToRoad();

}

// Snap a user-created polyline to roads and draw the snapped path
function runSnapToRoad() {
  var pathValues = [];
  var pathVal = [];
  var pathcon = [];
  var utime;

  @foreach($transactions as $transaction)
    utime = '{{$transaction->updated_at}}';
  @endforeach


  @foreach($track as $transac)
    if('{{$transac->updated_at}}' < utime)
      pathVal.push("{{$transac->latitude}},{{$transac->longitude}}")
    else 
       pathValues.push("{{$transac->latitude}},{{$transac->longitude}}"); 
  @endforeach
 pathcon.push(pathVal[pathVal.length-1]);
 pathcon.push(pathValues[0]);
alert(pathcon);
  $.get('https://roads.googleapis.com/v1/snapToRoads', {
    interpolate: true,
    key: apiKey,
    path: pathValues.join('|')
  }, function(data) {
    processSnapToRoadResponse(data);
    drawSnappedPolyline();
  });

}

// Store snapped polyline returned by the snap-to-road method.
function processSnapToRoadResponse(data) {
snappedCoordinates = [];
placeIdArray = [];
  for (var i = 0; i < data.snappedPoints.length; i++) {
    var latlng = new google.maps.LatLng(
        data.snappedPoints[i].location.latitude,
        data.snappedPoints[i].location.longitude);
snappedCoordinates.push(latlng);
placeIdArray.push(data.snappedPoints[i].placeId);
  }
}

// Draws the snapped polyline (after processing snap-to-road response).
function drawSnappedPolyline() {
  var snappedPolyline = new google.maps.Polyline({
    path: snappedCoordinates,
    strokeColor: 'green',
    strokeWeight: 3
  });

  snappedPolyline.setMap(map);
  polylines.push(snappedPolyline);
}

$(window).load(initialize);

setInterval(function myfunction()
{
  $('#btn').click();
}, 1000000);`

1 个答案:

答案 0 :(得分:1)

如果你的某些纬度/长度没有卡在路上,则可能表示点相距太远。

  

Roads demo:“捕捉算法最适合靠近的点。如果您观察到奇怪的捕捉行为,请尝试创建具有更近点的路径。“

当积分相距不超过400米时,我得到了最好的结果。这有助于插入它们之间的路径,并将折线放在路面上。

顺便说一句:the roads inspector可以方便地检查纬度/经度数据。