地理定位指向建筑物入口而未定义LatLng

时间:2015-08-03 20:45:42

标签: google-maps geolocation maps

我目前正在调整和清理一个非常简单的Google地图应用程序,以便用户从当前位置(由GeoLocation确定)获取步行路线,以预设标记指定使用特定LatLng值定义为第六个小数的艺术品。

示例中仍然有很多无关代码需要删除,但核心功能正在按预期工作 - 差不多。

当用户从下拉菜单中提供的值中选择目的地并点击输入时,将使用calcRoute功能显示从它们到所选目的地的带衬里路径。

然而,该路径始终将用户引导至最近的建筑物入口 - 在某些情况下,该入口距离代码中定义的LatLng几百英尺。这似乎不是随机的不准确,因为路径总是在建筑物入口处结束。

我确信这是一个非常简单的错误,但我找不到任何似乎可以解决这种奇怪行为的帖子。

我目前正在使用Windows 8和Chrome进行基础开发,试图获得可用的版本,然后在其他浏览器上进行测试。我感谢任何建议,并将在必要时提供所有示例代码。以下是此应用中一些典型行的示例:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true">
</script>
<script>
    var locTreeWrapIII = new google.maps.LatLng(48.006640, -122.203680);
</script>

    // Tree Wrap III
var markerTreeWrapIII = new google.maps.Marker({
    map: map,
    position: locTreeWrapIII,
    title: 'Tree Wrap III'
});
var infoTreeWrapIII = new google.maps.InfoWindow({ // info window
    maxWidth: 400,
    content:
        '<img alt="" src="https:\/thumbnail-clothespins.jpg" />' +
        '<strong>' + markerTreeWrapIII.title + '<\/strong><br \/><br \/><br \/>
    <a href="http:\/\/xx\/tree-wrap-iii">Read more about Tree Wrap III</a>'
});

google.maps.event.addListener(markerTreeWrapIII, "click", function () {
    document.getElementById('end').value = "(48.006500,-122.203500)";
    infoTreeWrapIII.open(map, markerTreeWrapIII);
    infoTreeWrapIII.setZIndex(999);
});
google.maps.event.addListener(markerTreeWrapIII, "dblclick", function () {
    map.panTo(locTreeWrapIII);
});
google.maps.event.addListener(infoTreeWrapIII, "closeclick", function () {
    infoTreeWrapIII.setZIndex(1);
    infoTreeWrapIII.close();
});

function calcRoute() {

    // Retrieve the start and end locations and create
    // a DirectionsRequest using WALKING directions.
    var start = document.getElementById('start').value;
    var end = document.getElementById('end').value;
    var request = {
        origin: start,
        destination: end,
        travelMode: google.maps.TravelMode.WALKING
    };

    // Route the directions and pass the response to a
    // function to create markers for each step.
    directionsService.route(request, function (response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
        }
    });
}

function attachInstructionText(marker, text) {
    google.maps.event.addListener(marker, 'click', function () {
        // Open an info window when the marker is clicked on,
        // containing the text of the step.
        stepDisplay.setContent(text);
        stepDisplay.open(map, marker);
    });
}

1 个答案:

答案 0 :(得分:0)

我的猜测是WALKING旅行模式限制了人行道的路径,从而限制了建筑物的入口。你的代码看起来很好。

根据API documentation

  

walking要求通过人行道和步行路径行走方向人行道(如果有的话)。

因此Google很可能将特定的LatLng地理编码为地址,并且正在尝试将walker直接发送到该地址。

一种可能的,但原始的解决方案是将最终的LatLng附加到路线服务返回的路径,即所需的位置。