2011-07-16 23:53:15 +02:00
|
|
|
L.CustomMarker = L.Class.extend({
|
|
|
|
|
|
|
|
includes: L.Mixin.Events,
|
2010-12-28 14:41:44 +01:00
|
|
|
|
2011-07-16 23:53:15 +02:00
|
|
|
options: {
|
|
|
|
contentCreator: undefined,
|
|
|
|
shadowCreator: undefined,
|
|
|
|
clickable: true,
|
|
|
|
draggable: false
|
|
|
|
},
|
2010-12-28 14:41:44 +01:00
|
|
|
|
2011-07-16 23:53:15 +02:00
|
|
|
initialize: function(latlng, options) {
|
|
|
|
L.Util.setOptions(this, options);
|
|
|
|
this._latlng = latlng;
|
|
|
|
},
|
2010-12-28 14:41:44 +01:00
|
|
|
|
2011-07-16 23:53:15 +02:00
|
|
|
onAdd: function(map) {
|
|
|
|
this._map = map;
|
2010-12-28 14:41:44 +01:00
|
|
|
|
2011-07-16 23:53:15 +02:00
|
|
|
if (!this._element && this.options.elementCreator) {
|
|
|
|
this._element = this.options.elementCreator();
|
|
|
|
|
2011-07-18 03:40:24 +02:00
|
|
|
this._element.className += ' leaflet-marker-icon';
|
2011-07-16 23:53:15 +02:00
|
|
|
|
|
|
|
this._initInteraction();
|
|
|
|
}
|
|
|
|
if (!this._shadow && this.options.shadowCreator) {
|
|
|
|
this._shadow = this.options.shadowCreator();
|
|
|
|
}
|
2010-12-28 14:41:44 +01:00
|
|
|
|
2011-07-16 23:53:15 +02:00
|
|
|
if (this._element) {
|
|
|
|
map._panes.markerPane.appendChild(this._element);
|
|
|
|
}
|
|
|
|
if (this._shadow) {
|
|
|
|
map._panes.shadowPane.appendChild(this._shadow);
|
|
|
|
}
|
2010-12-28 14:41:44 +01:00
|
|
|
|
2011-07-16 23:53:15 +02:00
|
|
|
map.on('viewreset', this._reset, this);
|
|
|
|
this._reset();
|
|
|
|
},
|
2010-12-28 14:41:44 +01:00
|
|
|
|
2011-07-16 23:53:15 +02:00
|
|
|
onRemove: function(map) {
|
|
|
|
if (this._element) {
|
|
|
|
map._panes.markerPane.removeChild(this._element);
|
|
|
|
}
|
|
|
|
if (this._shadow) {
|
|
|
|
map._panes.shadowPane.removeChild(this._elementShadow);
|
|
|
|
}
|
|
|
|
|
|
|
|
map.off('viewreset', this._reset, this);
|
2011-07-31 19:41:01 +02:00
|
|
|
|
|
|
|
map = null;
|
2011-07-16 23:53:15 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
getLatLng: function() {
|
|
|
|
return this._latlng;
|
|
|
|
},
|
|
|
|
|
|
|
|
setLatLng: function(latlng) {
|
|
|
|
this._latlng = latlng;
|
|
|
|
this._reset();
|
|
|
|
},
|
|
|
|
|
|
|
|
_reset: function() {
|
2011-07-31 19:41:01 +02:00
|
|
|
if(this._map == null)
|
|
|
|
return;
|
2011-07-16 23:53:15 +02:00
|
|
|
var pos = this._map.latLngToLayerPoint(this._latlng);
|
|
|
|
|
|
|
|
if (this._element) {
|
|
|
|
L.DomUtil.setPosition(this._element, pos);
|
|
|
|
}
|
|
|
|
if (this._shadow) {
|
|
|
|
L.DomUtil.setPosition(this._shadow, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._element) {
|
|
|
|
this._element.style.zIndex = pos.y;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_initInteraction: function() {
|
|
|
|
if (this._element && this.options.clickable) {
|
|
|
|
this._element.className += ' leaflet-clickable';
|
|
|
|
|
|
|
|
L.DomEvent.addListener(this._element, 'click', this._onMouseClick, this);
|
2011-01-12 02:03:31 +01:00
|
|
|
|
2011-07-16 23:53:15 +02:00
|
|
|
var events = ['dblclick', 'mousedown', 'mouseover', 'mouseout'];
|
|
|
|
for (var i = 0; i < events.length; i++) {
|
|
|
|
L.DomEvent.addListener(this._element, events[i], this._fireMouseEvent, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._element && L.Handler.MarkerDrag) {
|
|
|
|
this.dragging = new L.Handler.MarkerDrag(this);
|
|
|
|
|
|
|
|
if (this.options.draggable) {
|
|
|
|
this.dragging.enable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_onMouseClick: function(e) {
|
|
|
|
L.DomEvent.stopPropagation(e);
|
|
|
|
if (this.dragging && this.dragging.moved()) { return; }
|
|
|
|
this.fire(e.type);
|
|
|
|
},
|
|
|
|
|
|
|
|
_fireMouseEvent: function(e) {
|
|
|
|
this.fire(e.type);
|
|
|
|
L.DomEvent.stopPropagation(e);
|
2011-10-13 02:21:28 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
openPopup: function() {
|
|
|
|
this._popup.setLatLng(this._latlng);
|
|
|
|
this._map.openPopup(this._popup);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
closePopup: function() {
|
|
|
|
if (this._popup) {
|
|
|
|
this._popup._close();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
bindPopup: function(content, options) {
|
|
|
|
this._popup = new L.Popup(options);
|
|
|
|
this._popup.setContent(content);
|
|
|
|
this.on('click', this.openPopup, this);
|
|
|
|
|
|
|
|
return this;
|
2010-12-28 14:41:44 +01:00
|
|
|
}
|
2011-10-13 02:21:28 +02:00
|
|
|
|
2011-07-16 23:53:15 +02:00
|
|
|
});
|