oxjs/source/js/Ox.MapMarker.js
2011-04-23 18:45:50 +02:00

131 lines
3.9 KiB
JavaScript

// vim: et:ts=4:sw=4:sts=4:ft=js
Ox.MapMarker = function(options) {
var options = Ox.extend({
map: null,
place: null
}, options),
that = this;
Ox.forEach(options, function(val, key) {
that[key] = val;
});
that.marker = new google.maps.Marker({
raiseOnDrag: false,
shape: {coords: [8, 8, 8], type: 'circle'},
title: that.place.name,
//zIndex: 1000
});
setOptions();
function click() {
if (!that.place.selected) {
that.map.options({selected: that.place.id});
} else if (that.map.getKey() == 'meta') {
that.map.options({selected: null});
} else if (that.map.getKey() == 'shift') {
that.map.zoomToPlace();
} else {
that.map.panToPlace();
}
}
function dragstart(e) {
}
function drag(e) {
var northSouth = (that.place.north - that.place.south) / 2,
lat = Ox.limit(
e.latLng.lat(),
Ox.MIN_LATITUDE + northSouth,
Ox.MAX_LATITUDE - northSouth
),
lng = e.latLng.lng(),
span = Math.min(
that.place.sizeEastWest * Ox.getDegreesPerMeter(lat) / 2, 179.99999999
);
degreesPerMeter = Ox.getDegreesPerMeter(lat);
that.place.south += lat - that.place.lat;
that.place.north += lat - that.place.lat;
that.place.west = lng - span;
that.place.east = lng + span;
if (that.place.west < -180) {
that.place.west += 360;
} else if (that.place.east > 180) {
that.place.east -= 360;
}
Ox.print('west', that.place.west, 'east', that.place.east, 'span', span);
that.place.update();
that.marker.setOptions({
position: that.place.center
})
that.place.rectangle.update();
}
function dragend(e) {
}
function setOptions() {
// workaround to prevent marker from appearing twice
// after setting draggable from true to false
var fix = that.marker.getDraggable() && !that.place.editing;
that.marker.setOptions({
// fixme: cursor remains pointer
cursor: that.place.editing ? 'move' : 'pointer',
draggable: that.place.editing,
icon: new google.maps.MarkerImage(
Ox.UI.PATH + 'png/ox.map/marker' +
(that.place.id[0] == '_' ? 'Result' : '') +
(that.place.editing ? 'Editing' : (
that.place.selected ? 'Selected' : ''
)) + '.png',
new google.maps.Size(16, 16),
new google.maps.Point(0, 0),
new google.maps.Point(8, 8)
),
position: that.place.center
});
if (fix) {
that.marker.setVisible(false);
setTimeout(function() {
that.marker.setVisible(true);
}, 0);
}
}
that.add = function() {
//Ox.print('MapMarker add', that)
that.marker.setMap(that.map.map);
google.maps.event.addListener(that.marker, 'click', click);
return that;
};
that.edit = function() {
setOptions();
google.maps.event.addListener(that.marker, 'dragstart', dragstart);
google.maps.event.addListener(that.marker, 'drag', drag);
google.maps.event.addListener(that.marker, 'dragend', dragend);
};
that.remove = function() {
that.marker.setMap(null);
google.maps.event.clearListeners(that.marker);
return that;
};
that.submit = function() {
google.maps.event.clearListeners(that.marker, 'dragstart');
google.maps.event.clearListeners(that.marker, 'drag');
google.maps.event.clearListeners(that.marker, 'dragend');
}
that.update = function() {
setOptions();
}
return that;
};