Google maps api - snap marker to nearest road

前端 未结 1 1031
天涯浪人
天涯浪人 2020-12-06 15:11

I\'m trying to snap coordinate to nearest road. But still I can\'t do that in simple way. This is simple code, how to improve it that result would be marker on the road:

相关标签:
1条回答
  • 2020-12-06 15:29

    Use the directions service. Get driving directions from the point of interest to the point of interest. Should be on the road.

    var directionsService = new google.maps.DirectionsService();
    
    directionsService.route({origin:homeLatlng, destination:homeLatlng, travelMode: google.maps.DirectionsTravelMode.DRIVING}, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK)
      {
        var homeMarker = new google.maps.Marker({
          position: response.routes[0].legs[0].start_location, 
          map: map,
          title: "Check this cool location",
          icon: image,
          shadow: shadow
        });
      } else {
        var homeMarker = new google.maps.Marker({
          position: homeLatlng,
          map: map,
          title: "Check this cool location",
          icon: image,
          shadow: shadow
        });
      }
    });
    

    example

    0 讨论(0)
提交回复
热议问题