Moving markers in Google Maps API v3 without being so processor intensive?
I'm trying to move a marker on a GoogleMap to simulate real time object movements. At present the JavaScript pseudo code for how I do this is:
var marker = new google.maps.Marker({
position: myLatlng,
map: map
});
var myMovementArray[] = new movementArray(startPoint, endPoint);
drawMovement(int pos){
marker.setPosition(myMovementArray[pos]["lat"], myMovementArray[pos]["lng"]);
pos++;
if (pos < myMovementArray.length()){
setTimeout('drawMovement('+pos+')', 33);
}
}
init(){
drawMovement(0);
}
开发者_JAVA百科
Where each element in the movement array is calculated through:
deltaLat = (endPos.lat - startPos.lat)/frames;
deltaLng = (endPos.lng - startPos.lng)/frames;
myMovementArray[i]["lat"] = startPos.lat + (deltaLat * i);
myMovementArray[i]["lng"] = startPos.lng + (deltaLng * i);
For reference, the full JavaScript file I am using is up at: http://spad.es/js/com.kamkash.locateme.viewer.dev.js
The problem I have is that this method of moving markers on Google maps seems to be quite processor intensive. I've searched around to see if the Google Maps API has a clean way of animating marker movement from point A to point B, but can't find anything. And the other most referenced method for doing this that I've found is exemplified at http://www.geocodezip.com/v3_animate_marker_directions.html but then that uses the same method I have deployed.
The code is used in practice at: www.spad.es/random
Does anybody have a more processor-efficient/cleaner way of doing this?
Thanks
This may be a problem with canvas markers. Try setting the optimized: false
marker option - this causes markers to not render as canvas markers.
You can optimize deltaLat and deltaLng a lot with a raster algorithmus like the Bresenahm algorithmus: http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm. In x-axe it uses always 1 pixel step and in y-axe it depends on an clever error correction.
deltaLat = (endPos.lat - startPos.lat)/frames;
deltaLng = (endPos.lng - startPos.lng)/frames;
精彩评论