【问题标题】:Accuracy of directions using Long/Lat with Google Maps API使用 Long/Lat 和 Google Maps API 的方向准确性
【发布时间】:2013-08-24 07:07:41
【问题描述】:

我正在使用 Google Maps API v3 显示从用户定义的起点到我的财产的路线。

除了终点的准确性之外,这在技术上工作正常。我已将我的财产的经度/纬度定义为准确的点,但方向仍会将您带到大约 100 米外的街道。

有没有办法强制指示将您带到确切的点?

这是我的代码,以防万一……

function getDirections(x, y) {

            $("#directionsPanel").html("");
            var directionsService = new google.maps.DirectionsService();
            var directionsDisplay = new google.maps.DirectionsRenderer();

            var myOptions = {
                zoom: 10,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                scrollwheel: false
            }

            var map = new google.maps.Map(document.getElementById("map"), myOptions);
            directionsDisplay.setPanel(document.getElementById('directionsPanel'));
            directionsDisplay.setMap(map);

            var request = {
                origin: x,
                destination: $('.lat').text() + ',' + $('.lng').text() ,
                travelMode: google.maps.DirectionsTravelMode.DRIVING,
                unitSystem: google.maps.UnitSystem.IMPERIAL,
                provideRouteAlternatives: true,
                region: "GB"
            };

            directionsService.route(request, function (response, status) {

                    directionsDisplay.setDirections(response);
                    $(".noValidRoute").hide();

            });

        }

【问题讨论】:

  • 您的 travelMode 正在开车。
  • 是的,但是为什么这些指示会停在路上几百码甚至附近的道路上?

标签: javascript google-maps google-maps-api-3


【解决方案1】:

假设getDirections(x, y) 中的x,y 是您仅使用x 的属性的坐标。

试试 ;

function getDirections(x, y) {

        var origin = new google.maps.LatLng(x,y)
        $("#directionsPanel").html("");
        var directionsService = new google.maps.DirectionsService();
        var directionsDisplay = new google.maps.DirectionsRenderer();

        var myOptions = {
            zoom: 10,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            scrollwheel: false
        }

        var map = new google.maps.Map(document.getElementById("map"), myOptions);
        directionsDisplay.setPanel(document.getElementById('directionsPanel'));
        directionsDisplay.setMap(map);

        var request = {
            origin: origin,
            destination: $('.lat').text() + ',' + $('.lng').text() ,
            travelMode: google.maps.DirectionsTravelMode.DRIVING,
            unitSystem: google.maps.UnitSystem.IMPERIAL,
            provideRouteAlternatives: true,
            region: "GB"
        };

        directionsService.route(request, function (response, status) {

                directionsDisplay.setDirections(response);
                $(".noValidRoute").hide();

        });

    }

【讨论】:

    【解决方案2】:

    您可以传递一个“半径”变量。不知道如何在 JS 中做,但是用 web-api 看看这个:

    http://cbk0.google.com/cbk?output=xml&ll=43.19199,-5.4288&radius=500

    什么都不返回

    将半径更改为 5000 即可得到结果。如果这不起作用,请尝试更高的缩放级别?

    【讨论】:

      猜你喜欢
      • 2016-09-18
      • 1970-01-01
      • 2018-09-06
      • 2018-06-17
      • 2012-01-29
      • 1970-01-01
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      相关资源
      最近更新 更多