- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在没有 DirectionsRenderer
的情况下绘制自己的路线。
这是我的代码:
var map;
var directionsService;
function initialize() {
var mapOptions = { mapTypeId: google.maps.MapTypeId.ROADMAP }
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsService = new google.maps.DirectionsService();
calcRoute();
}
function calcRoute() {
var request = {
origin: 'שדי חמד',
destination: 'כפר סבא',
travelMode: google.maps.TravelMode.WALKING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
map.fitBounds(directionResult.routes[0].bounds);
createPolyline(response);
}
});
}
function createPolyline(directionResult) {
var line = new google.maps.Polyline({
path: directionResult.routes[0].overview_path,
strokeColor: '#FF0000',
strokeOpacity: 0.5,
strokeWeight: 4
});
line.setMap(map);
for (var i = 0; i < line.getPath().length; i++) {
var marker = new google.maps.Marker({
icon: { path: google.maps.SymbolPath.CIRCLE, scale: 3 },
position: line.getPath().getAt(i),
map: map
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
我得到的只是一个灰色的窗口,连一张 map 都没有。
将 DirectionsService 的
响应发送到 DirectionsRenderer
时,我得到了两条折线。
我们欢迎任何建议。
最佳答案
我收到一个 javascript 错误“directionResult is undefined”
在这一行:
map.fitBounds(directionResult.routes[0].bounds);
如果我修复它(将其更改为响应)它会起作用。
顺便说一句——我不建议使用 overview_path;如果路径很长或很复杂,说明它没有足够的细节。
代码片段:
var map;
var directionsService;
function initialize() {
var mapOptions = {
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsService = new google.maps.DirectionsService();
calcRoute();
}
function calcRoute() {
var request = {
origin: 'שדי חמד',
destination: 'כפר סבא',
travelMode: google.maps.TravelMode.WALKING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
map.fitBounds(response.routes[0].bounds);
createPolyline(response);
}
});
}
function createPolyline(directionResult) {
var line = new google.maps.Polyline({
path: directionResult.routes[0].overview_path,
strokeColor: '#FF0000',
strokeOpacity: 0.5,
strokeWeight: 4
});
line.setMap(map);
for (var i = 0; i < line.getPath().length; i++) {
var marker = new google.maps.Marker({
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 3
},
position: line.getPath().getAt(i),
map: map
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map-canvas"></div>
关于google-maps - 在 Google Map V3 中显示没有 DirectionsRenderer 的路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16505731/
尝试绘制路线时,代码运行正常,但不渲染。 下面是一个示例代码,其结构和行为方式相同: Simple markers #map { height:
我正在使用 DirectionsService 和路线方法生成 DirectionsResult。我还使用 DirectionsRenderer 对象来显示结果,因为它非常易于使用。我没问题检测 di
我已经集成了一张 map ,我想显示两个位置之间的路线方向。一切正常,方向显示完美,但我想改变 Polyline 方向的颜色,我已经像文档中说的那样尝试了这段代码: //polyline option
我使用谷歌地图绘制我的路线。我只设置了 2 个点(开始和结束)。问题是我必须将路由发送到下一页。 我知道我可以捕获事件: google.maps.event.addListener(d
因为我想在 React 中创建从当前位置到特定单击标记的路线。为了实现这个,我使用 React Google Map 库( react-google-maps )及其方向示例( Direction E
喜欢 Google Maps API V3,但在使用 DirectionsRenderer 时难以获得准确的行车路线和操作显示的目的地地址. 无论我传递的是地址字符串还是 latLng实例作为 Dir
我正在尝试从 Google map 中清除之前的路线。但是,当您进行另一次搜索时,上一次搜索的多段线和标记仍然会显示。我试过使用 directionsRenderer.setMap(null),但没有
如何在使用 DirectionsService 时向 directionsRenderer 添加鼠标悬停事件监听器? 我知道如何给直线添加监听器,但似乎无法在 directionsRenderer 中
最近,在我们的应用程序的起点和目的地位置之间使用带有“航点”的 DirectionsService 时,我们一直在返回这些在视觉上没有意义的“之字形”路线。 看起来,一切都进行得很好,但在第 7 个航
是否有一种简单的方法来访问由 DirectionsRenderer 创建的标记和信息窗口的对象/属性? (即路线的“A”和“B”端点) 我正在寻找用我从另一个“非路线”标记创建的信息窗口(包含动态内容
在 Google Maps API v3 中,我如何从 DirectionsRenderer 获取航路点标记以便向它们添加点击事件(例如删除菜单)。 最佳答案 在找到更好的解决方案之前,这里是我所采用
我正在尝试在没有 DirectionsRenderer 的情况下绘制自己的路线。 这是我的代码: var map; var directionsService; function initialize
我有一个显示在 map 上的 DirectionsResult。当用户双击 DirectionsRenderer 生成的标记时,如何访问各个多段线(腿)并更改颜色。 最佳答案 您不能对特定的路段使用不
我正在尝试在没有 DirectionsRenderer 的情况下绘制自己的路线。 这是我的代码: var map; var directionsService; function initialize
我正在使用谷歌地图 v3。 我已经抑制了标记以在 map 本身上显示我自己的标记。 我想修改方向 div 中显示的那些,但图像没有 ID 或类 是否有其他方法可以修改源,或者我需要滚动自己的方向渲染
我正在使用 NgMap,我需要在 Controller 中获取方向。我尝试过以下方法: NgMap.getMap().then(function(map) { console.log(map.
我是一名优秀的程序员,十分优秀!