使用Long / Lat和Google Maps API的方向准确性

时间:2013-06-21 09:24:23

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

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

除了终点的准确性之外,这在技术上工作正常。我已经将我的物业的Long / Lat定义为确切点,但路线仍然会带您到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();

            });

        }

2 个答案:

答案 0 :(得分:1)

假设x,y中的getDirections(x, y)是您的媒体资源的坐标,您只使用x

TRY ;

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();

        });

    }

答案 1 :(得分:0)

您可以传递“radius”变量。不知道如何在JS中做,但用web-api看看:

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

它什么都不返回

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