Clean up player marker transitions to/from visible map

This commit is contained in:
Mike Primm 2011-07-31 12:41:01 -05:00
parent 58df3a8441
commit 95c9520cc9
2 changed files with 42 additions and 21 deletions

View File

@ -48,6 +48,8 @@ L.CustomMarker = L.Class.extend({
} }
map.off('viewreset', this._reset, this); map.off('viewreset', this._reset, this);
map = null;
}, },
getLatLng: function() { getLatLng: function() {
@ -60,6 +62,8 @@ L.CustomMarker = L.Class.extend({
}, },
_reset: function() { _reset: function() {
if(this._map == null)
return;
var pos = this._map.latLngToLayerPoint(this._latlng); var pos = this._map.latLngToLayerPoint(this._latlng);
if (this._element) { if (this._element) {

View File

@ -7,7 +7,8 @@ componentconstructors['playermarkers'] = function(dynmap, configuration) {
var div = document.createElement('div'); var div = document.createElement('div');
var playerImage; var playerImage;
$(player.marker._element).toggle(dynmap.world === player.location.world); var markerPosition = dynmap.getProjection().fromLocationToLatLng(player.location);
player.marker.setLatLng(markerPosition);
$(div) $(div)
.addClass('Marker') .addClass('Marker')
@ -63,18 +64,25 @@ componentconstructors['playermarkers'] = function(dynmap, configuration) {
player.healthContainer.css('display','none'); player.healthContainer.css('display','none');
} }
} }
return div; return div;
}}); }});
if(dynmap.world === player.location.world)
dynmap.map.addLayer(player.marker); dynmap.map.addLayer(player.marker);
}); });
$(dynmap).bind('playerremoved', function(event, player) { $(dynmap).bind('playerremoved', function(event, player) {
// Remove the marker. // Remove the marker.
if(dynmap.map.hasLayer(player.marker))
dynmap.map.removeLayer(player.marker); dynmap.map.removeLayer(player.marker);
}); });
$(dynmap).bind('playerupdated', function(event, player) { $(dynmap).bind('playerupdated', function(event, player) {
if(dynmap.world === player.location.world) {
// Add if needed
if(dynmap.map.hasLayer(player.marker) == false)
dynmap.map.addLayer(player.marker);
else {
// Update the marker. // Update the marker.
var markerPosition = dynmap.getProjection().fromLocationToLatLng(player.location); var markerPosition = dynmap.getProjection().fromLocationToLatLng(player.location);
$(player.marker._element).toggle(dynmap.world === player.location.world);
player.marker.setLatLng(markerPosition); player.marker.setLatLng(markerPosition);
// Update health // Update health
if (configuration.showplayerhealth) { if (configuration.showplayerhealth) {
@ -86,6 +94,10 @@ componentconstructors['playermarkers'] = function(dynmap, configuration) {
player.healthContainer.css('display','none'); player.healthContainer.css('display','none');
} }
} }
}
} else if(dynmap.map.hasLayer(player.marker)) {
dynmap.map.removeLayer(player.marker);
}
}); });
// Remove marker on start of map change // Remove marker on start of map change
$(dynmap).bind('mapchanging', function(event) { $(dynmap).bind('mapchanging', function(event) {
@ -93,7 +105,7 @@ componentconstructors['playermarkers'] = function(dynmap, configuration) {
for(name in dynmap.players) { for(name in dynmap.players) {
var player = dynmap.players[name]; var player = dynmap.players[name];
// Turn off marker - let update turn it back on // Turn off marker - let update turn it back on
$(player.marker._element).toggle(false); dynmap.map.removeLayer(player.marker);
} }
}); });
// Remove marker on map change - let update place it again // Remove marker on map change - let update place it again
@ -101,9 +113,14 @@ componentconstructors['playermarkers'] = function(dynmap, configuration) {
var name; var name;
for(name in dynmap.players) { for(name in dynmap.players) {
var player = dynmap.players[name]; var player = dynmap.players[name];
if(dynmap.world === player.location.world) {
if(dynmap.map.hasLayer(player.marker) == false)
dynmap.map.addLayer(player.marker);
var markerPosition = dynmap.getProjection().fromLocationToLatLng(player.location); var markerPosition = dynmap.getProjection().fromLocationToLatLng(player.location);
player.marker.setLatLng(markerPosition); player.marker.setLatLng(markerPosition);
$(player.marker._element).toggle(dynmap.world === player.location.world); } else if(dynmap.map.hasLayer(player.marker)) {
dynmap.map.removeLayer(player.marker);
}
} }
}); });
}; };