上我試圖做一個標記移動(不會消失,再次出現)地圖的道路上的車輛移動上。顯示移動標記在地圖
我的latLng的兩個值,我想,直到下一個點是由車輛發送到移動兩者的標記。然後再重複這個過程。
我試過:這不是一個非常有效的方法,我知道]
我的想法是實現上述使用下面點的技術:
1)之間劃出一條線他們倆。
2)獲取的每個點的位置的經緯度的折線的1/10級分。
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;
}
面臨的問題:
1)的標記沒有出現在地圖上,因爲繪製和刪除標記的過程是如此之快,標記在屏幕上不可見。我試過setTimeOut,它根本沒有幫助。
2)如果我alow運行爲5分鐘以上此代碼的瀏覽器,瀏覽器崩潰。
注意:使用setInterval每10秒調用Above函數。
什麼可以是更好的解決方案?請幫助..
[沿任意多段線的動畫標記(來自XML)](http://www.geocodezip.com/v3_animate_marker_xml.html); [DirectionsService的多段線上的動畫標記](http://www.geocodezip.com/v3_animate_marker_directions.html) – geocodezip 2013-05-02 13:14:17
使用回調函數時出現了什麼問題也在你插入的每個點上繪製了一個標記?所以你畫一個標記;在下一個週期中,您將其刪除;並在下一個計算的latlng上放置一個標記... – Thomas 2013-05-02 13:16:17
感謝GeocodeZip ..!讓我嘗試一次。 – writeToBhuwan 2013-05-02 13:18:03