谷歌地图API V3,这是将标记捕捉到道路的好解决方案吗?

Google Maps API V3, Is this a good solution for snapping marker to road?

本文关键字:道路 解决方案 V3 API 谷歌地图      更新时间:2023-09-26

所以我正在开发一个移动应用程序,并且一直在研究最简单的方法来让标记捕捉到您在地图上单击的最近道路。我想出了以下解决方案,还没有在网上任何地方看到它列出,只是想看看这样做是否有问题?

我会发布我的代码,但它散布在一些对象中。所以这是它的要点。

所以基本上...- 为地图建立点击处理程序,返回点击的纬度/液化坐标(简单)

然后像.. 'var DirectionsService = new google.maps.DirectionsService();

DirectionsService.route({
        'origin': clickLatLngPosition,
        'destination': clickLatLngPosition,
        'travelMode': google.maps.TravelMode.DRIVING
    }, function(response, status){
        testLat = response.routes[0].legs[0].start_location.lat();
        testLng = response.routes[0].legs[0].start_location.lng();
        testPosition = new google.maps.LatLng(testLat, testLng);
        slammedNavigator.snMapElement.gmap('addMarker', {
            'position': testPosition
        });
    });` 

这是根据用户的点击将标记捕捉到道路上的好解决方案吗?是或否?有没有更好的方法?

如果您使用以下方法,则可以节省三行代码:

position: response.routes[0].legs[0].start_location

现在,您正在使用 LatLng 对象的各个统计信息来创建新的 LatLng 对象