【发布时间】:2015-08-03 20:45:41
【问题描述】:
我目前正在调整和清理一个非常简单的 Google 地图应用程序,供用户获取从当前位置(由 GeoLocation 确定)到预设标记的步行路线,这些标记指定使用特定 LatLng 值定义的艺术作品,精确到小数点后六位。
示例中仍有大量无关代码需要删除,但核心功能按预期工作 - 几乎。
当用户从下拉菜单中提供的值中选择一个目的地并按 Enter 键时,会出现一条从他们所在的位置到所选目的地的直线路径 - 使用 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);
});
}
【问题讨论】:
标签: google-maps geolocation maps