Skip to content

Instantly share code, notes, and snippets.

@jimmy-collazos
Created July 18, 2012 11:17
Show Gist options
  • Save jimmy-collazos/3135626 to your computer and use it in GitHub Desktop.
Save jimmy-collazos/3135626 to your computer and use it in GitHub Desktop.
Esta librería se usa para poner multiples marcas (generalmente flechas indicando la dirección) en un trayecto y mostrar sólo las que no colapsan entre si; dejando una distancia en pixeles entre dos marcas.
var TrajectoryMarkers = function(map, arrayMarkers, separation){
this.set('markers', arrayMarkers || []);
this.set('separation', separation || 50);//pixeles de separación
this.setMap(map);
}
TrajectoryMarkers.prototype = new google.maps.OverlayView();
TrajectoryMarkers.prototype.onAdd = function(){
var self = this;
google.maps.event.addListener(this.getMap(), 'zoom_changed', function(){
self.draw();
})
}
TrajectoryMarkers.prototype.draw = function () {
var n, pixelPositionA, pixelPositionB, sizeA, x, y,
abs = Math.abs,
projection = this.getProjection(),
separation = this.separation;
n = this.markers.length;
pixelPositionA = projection.fromLatLngToContainerPixel( this.markers[n-1].getPosition() );
while(n--){
this.markers[n].setMap(null);
if(n>1){
pixelPositionB = projection.fromLatLngToContainerPixel( this.markers[n-1].getPosition() );
x = (pixelPositionA.x - pixelPositionB.x);
y = (pixelPositionA.y - pixelPositionB.y);
sizeA = this.getMarkerSize(this.markers[n]);
if( this.isValid(pixelPositionA, sizeA, pixelPositionB) ){
pixelPositionA = projection.fromLatLngToContainerPixel( this.markers[n].getPosition() );
this.markers[n].setMap( this.getMap() );
}
}
}
this.markers[0] && this.markers[0].setMap(this.getMap());
}
TrajectoryMarkers.prototype.getMarkerSize = function (marker) {
var icon, size = marker.get('__iconsize');
if(!size){
icon = marker.getIcon();
if(icon instanceof google.maps.MarkerImage){
size = icon.size;
marker.set('__iconsize', size);
}
}
return size;
}
TrajectoryMarkers.prototype.isValid = function (pixelPositionA, sizeA, pixelPositionB) {
var out = false, separationX, separationY;
sizeA = sizeA || {width:0,height:0};
separationX = this.abs(pixelPositionA.x)-this.abs(pixelPositionB.x);
if( separationX > (this.separation + sizeA.width) ){
return true;
}
separationY = this.abs(pixelPositionA.y) - this.abs(pixelPositionB.y);
if( separationY > (this.separation + sizeA.height) ){
return true;
}
return out;
}
TrajectoryMarkers.prototype.abs = Math.abs;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment