是否可以使用 Google Maps API 突出显示街道?
我能找到的唯一接近这种效果的是在它们上面画线。
但这是很多工作,而且更不准确。这些线也将越过地名。
我想要的是突出显示某些街道名称,就好像您正在从 a 点导航到 b 点一样。
因此,例如,如果 10 条街道被街头工作人员关闭,我可以突出显示这些街道。
最佳答案
这实际上可以通过使用 Maps API 方向渲染器轻松完成。
您必须提供街道起点和终点的纬度/经度坐标,渲染器会为您完成所有计算和绘制。您无需阅读说明步骤并自己绘制多段线!
在此处查看实际效果:
http://jsfiddle.net/HG7SV/15/
这是代码,所有魔法都在函数 initialize() 中完成:
<html>
<head>
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0px; padding: 0px }
#map_canvas { height: 100% }
</style>
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
function initialize() {
// init map
var myOptions = {
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
// init directions service
var dirService = new google.maps.DirectionsService();
var dirRenderer = new google.maps.DirectionsRenderer({suppressMarkers: true});
dirRenderer.setMap(map);
// highlight a street
var request = {
origin: "48.1252,11.5407",
destination: "48.13376,11.5535",
travelMode: google.maps.TravelMode.DRIVING
};
dirService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
dirRenderer.setDirections(result);
}
});
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>
如果您的街道是弯曲的并且渲染器应该找到您不想要的捷径,则可以通过添加中间路标以强制将绘制的线精确地强制到您想要的街道来轻松修改:
var request = {
origin: "48.1252,11.5407",
destination: "48.13376,11.5535",
waypoints: [{location:"48.12449,11.5536"}, {location:"48.12515,11.5569"}],
travelMode: google.maps.TravelMode.DRIVING
};
关于javascript - Google Maps API,是否可以突出显示特定街道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13310776/