谷歌地图:实时绘制和更新折线
Google maps: Live drawing and updating a Polyline
我真的是JS的新手,很抱歉,我没有附加我的代码,因为我所做的一切 - 来自Google Map Docs的"helloworld"示例。
那么,有什么问题: 我想根据用户的当前位置绘制一条折线。因此,每个google.maps.LatLng()目前都应该有坐标。在地图上应该出现整个更新方式,例如,每 5 秒更新一次。最后,它就像在地图上行走的早晨的可视化,类似的东西。
我知道,如何在var flightPlanCoordinates[]中"绘制"地图并添加点,我要求提供一些示例或链接,我可以在其中找到:
- 如何将当前位置添加到变量飞行计划坐标[]
- 如何使所有这些东西在"实时"模式下更新
感谢您的任何帮助:)
上级:
尝试做这样的事情,但不起作用
var path = poly.getPath();
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
path.push(new google.maps.LatLng(latitude, longitude));
UPD2:这是一个很酷的例子,应该如何 http://kasheftin.github.io/gmaps/
您需要使用新路径(已使用新点更新的路径)更新折线:
// get existing path
var path = poly.getPath();
// add new point
path.push(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
// update the polyline with the updated path
poly.setPath(path);
代码片段:(单击地图向折线添加点)
function initialize() {
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var poly = new google.maps.Polyline({
map: map,
path: []
})
google.maps.event.addListener(map, 'click', function(evt) {
// get existing path
var path = poly.getPath();
// add new point (use the position from the click event)
path.push(new google.maps.LatLng(evt.latLng.lat(), evt.latLng.lng()));
// update the polyline with the updated path
poly.setPath(path);
})
}
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?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas"></div>
尝试以下代码:
var path = poly.getPath().getArray();
path.push(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
poly.setPath(path);
截至目前,API 允许您只需将新的LatLng
对象推送到path
即可在地图上更新。
如他们的复杂折线示例所示,您可以执行以下操作:
var path = poly.getPath()
path.push(latLng)
poly
是您google.maps.Polyline
的地方,latLng
google.maps.LatLng
.
相关文章:
- Javascript循环不会自我更新
- 添加文字和评论功能更新Div
- AngularJS:ng之后,重复$scope值未按预期更新
- 如何通过数组更新角度子范围
- Ajax聊天消息重复而不仅仅是更新
- 通过CSV文件上载更新数据库表
- 平均值:无法将数据更新到数据库
- 用与线条相同的颜色填充多折线图上的点
- $rootScope未使用forEach进行更新
- d3基于用户选择动态更新节点
- 有条件更新d3.js力图中节点的最佳方法
- 如何在 D3 中更新我的多折线图数据
- 具有平滑更新动画的 D3 折线图
- 拉斐尔折线图动画和更新图
- 谷歌地图:实时绘制和更新折线
- D3.js:绘制简单、可更新的折线图
- d3js-更新多折线图中的文本元素时出现问题
- D3.js-动态更新折线图中的轴
- d3.js更新折线图上的圆圈
- 更新谷歌折线图