wri*_*wan 7 javascript jquery google-maps cross-browser google-maps-api-3
当车辆在路上行驶时,我试图在地图上移动标记(不会消失并再次出现).
我有两个latLng值,我想在两者之间移动标记,直到车辆发送下一个点.然后再次重复该过程.
我尝试了什么:[这不是一种非常有效的方式,我知道]
我的想法是使用下面的技术实现上述方法:
1)在两者之间划一条线.
2)获得折线的1/10分数上的每个点的latLng.
3)在地图上标记10个点以及折线.
这是我的代码:
var isOpen = false;
var deviceID;
var accountID;
var displayNameOfVehicle;
var maps = {};
var lt_markers = {};
var lt_polyLine = {};
function drawMap(jsonData, mapObj, device, deleteMarker) {
var oldposition = null;
var oldimage = null;
var arrayOflatLng = [];
var lat = jsonData[0].latitude;
var lng = jsonData[0].longitude;
//alert(jsonData[0].imagePath);
var myLatLng = new google.maps.LatLng(lat, lng);
if (deleteMarker == true) {
if (lt_markers["marker" + device] != null) {
oldimage = lt_markers["marker" + device].getIcon().url;
oldposition = lt_markers["marker" + device].getPosition();
lt_markers["marker" + device].setMap(null);
lt_markers["marker" + device] = null;
}
else {
console.log('marker is null');
oldimage = new google.maps.MarkerImage(jsonData[0].imagePath,
null,
null,
new google.maps.Point(5, 17), //(15,27),
new google.maps.Size(30, 30));
oldposition = myLatLng;
}
}
var image = new google.maps.MarkerImage(jsonData[0].imagePath,
null,
null,
new google.maps.Point(5, 17), //(15,27),
new google.maps.Size(30, 30));
lt_markers["marker" + device] = new google.maps.Marker({
position: myLatLng,
icon: image,
title: jsonData[0].address
});
if (oldposition == myLatLng) {
alert('it is same');
lt_markers["marker" + device].setMap(mapObj);
mapObj.panTo(myLatLng);
}
else {
alert('it is not same');
var markMarker = null;
var i = 10;
for (i = 10; i <= 100; i + 10) {
//-------
// setTimeout(function() {
if (markMarker != null) {
markMarker.setMap(null);
markMarker = null;
}
alert('inside the loop');
var intermediatelatlng = mercatorInterpolate(mapObj, oldposition, myLatLng, i / 100);
alert('Intermediate Latlng is :' + intermediatelatlng);
arrayOflatLng.push(intermediatelatlng);
var flightPath = new google.maps.Polyline({
path: arrayOflatLng,
strokeColor: "#FFFFFF",
strokeOpacity: 1.0,
strokeWeight: 1
});
flightPath.setMap(mapObj);
if (i != 100) {
markMarker = new google.maps.Marker({
position: intermediatelatlng,
icon: image,
title: jsonData[0].address,
map: mapObj
});
}
else {
markMarker = new google.maps.Marker({
position: intermediatelatlng,
icon: oldimage,
title: jsonData[0].address,
map: mapObj
});
}
mapObj.panTo(intermediatelatlng);
//--------
// }, 1000);
}
}
}
function mercatorInterpolate(map, latLngFrom, latLngTo, fraction) {
// Get projected points
var projection = map.getProjection();
var pointFrom = projection.fromLatLngToPoint(latLngFrom);
var pointTo = projection.fromLatLngToPoint(latLngTo);
// Adjust for lines that cross the 180 meridian
if (Math.abs(pointTo.x - pointFrom.x) > 128) {
if (pointTo.x > pointFrom.x)
pointTo.x -= 256;
else
pointTo.x += 256;
}
// Calculate point between
var x = pointFrom.x + (pointTo.x - pointFrom.x) * fraction;
var y = pointFrom.y + (pointTo.y - pointFrom.y) * fraction;
var pointBetween = new google.maps.Point(x, y);
// Project back to lat/lng
var latLngBetween = projection.fromPointToLatLng(pointBetween);
return latLngBetween;
}
Run Code Online (Sandbox Code Playgroud)
面临的问题:
1)标记没有显示在地图上,因为绘图和删除标记的过程非常快,以至于标记在屏幕上不可见.我已经尝试过setTimeOut,它根本没用.
2)如果我让浏览器运行此代码超过5分钟,浏览器崩溃.
注意:使用setInterval每10秒调用一次Above函数.
什么是更好的解决方案?请帮忙..
jli*_*vni 18
要使标记移动相对平稳,您需要
例如,类似于:
var counter = 0;
interval = window.setInterval(function() {
counter++;
// just pretend you were doing a real calculation of
// new position along the complex path
var pos = new google.maps.LatLng(35, -110 + counter / 100);
marker.setPosition(pos);
if (counter >= 1000) {
window.clearInterval(interval);
}
}, 10);
Run Code Online (Sandbox Code Playgroud)
我在http://jsfiddle.net/bmSbU/2/上做了一个简单的例子,它显示了一条沿直线路径移动的标记.如果这是您想要的,那么您上面的大部分代码都可以重复使用(或查看http://broady.github.io/maps-examples/points-along-line/along-directions. HTML)
您可以使用marker-animate-unobtrusive库使标记从一个位置平滑过渡到另一个位置(而不是重新出现).
您可以像这样初始化您的标记:
var marker = new SlidingMarker({
//your original marker options
});
Run Code Online (Sandbox Code Playgroud)
每次新车辆的坐标到达时,只需调用marker.setPosition().
PS我是图书馆的作者.
| 归档时间: |
|
| 查看次数: |
27611 次 |
| 最近记录: |