oxjs/source/Ox.UI/js/Map/Ox.MapMarker.js

276 lines
8.8 KiB
JavaScript
Raw Normal View History

2011-04-23 16:45:50 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=js
2011-05-16 10:49:48 +00:00
/*@
Ox.MapMarker <f> MapMarker Object
(options) -> <f> MapMarker Object
options <o> Options object
color <a|[255, 0, 0]> marker color
map <o|null> map
place <o|null> place
size <n|16> size
@*/
2011-04-22 22:03:10 +00:00
Ox.MapMarker = function(options) {
options = Ox.extend({
color: [255, 0, 0],
map: null,
place: null,
size: 16
}, options);
2011-05-30 18:57:22 +00:00
var that = this,
typeColor = {
2011-06-01 07:45:21 +00:00
country: [64, 64, 255],
region: [0, 192, 192],
2011-05-30 18:57:22 +00:00
city: [255, 0, 0],
borough: [255, 128, 0],
street: [255, 255, 0],
2011-06-01 11:11:28 +00:00
building: [255, 64, 128],
2011-06-01 12:08:29 +00:00
feature: [0, 192, 0]
2011-05-31 07:24:03 +00:00
},
areaSize = {
2011-06-01 10:50:43 +00:00
100: 10, // 10 x 10 m
10000: 12, // 100 x 100 m
1000000: 14, // 1 x 1 km
100000000: 16, // 10 x 10 km
10000000000: 18, // 100 x 100 km
1000000000000: 20, // 1,000 x 1,000 km
2011-06-01 12:08:29 +00:00
100000000000000: 22 // 10,000 x 10,000 km
2011-05-30 18:57:22 +00:00
};
2011-05-31 07:24:03 +00:00
2011-04-22 22:03:10 +00:00
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) {
2011-06-14 14:34:09 +00:00
if (that.map.getKey() == 'shift') {
2011-06-14 15:03:26 +00:00
var place = that.map.getSelectedPlace(),
bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(place.south, place.west),
new google.maps.LatLng(place.north, place.east)
),
southWest, northEast;
bounds = bounds.union(that.place.bounds);
southWest = bounds.getSouthWest();
northEast = bounds.getNorthEast();
2011-06-14 14:41:20 +00:00
that.map.newPlace(new Ox.MapPlace({
2011-06-14 14:48:46 +00:00
// fixme: duplicated, see Ox.Map.js
alternativeNames: [],
countryCode: '',
editable: true,
geoname: '',
id: '_' + Ox.encodeBase32(Ox.uid()), // fixme: stupid
2011-06-14 14:49:41 +00:00
map: that.map,
2011-06-14 14:48:46 +00:00
name: '',
type: 'feature',
2011-06-14 14:34:09 +00:00
south: southWest.lat(),
west: southWest.lng(),
north: northEast.lat(),
east: northEast.lng()
2011-06-14 14:41:20 +00:00
}));
2011-06-14 14:34:09 +00:00
} else {
that.map.options({selected: that.place.id});
}
2011-04-22 22:03:10 +00:00
} else {
2011-06-14 14:34:09 +00:00
if (that.map.getKey() == 'meta') {
that.map.options({selected: null});
} else if (that.map.getKey() == 'shift') {
that.map.zoomToPlace();
} else {
that.map.panToPlace();
}
2011-04-22 22:03:10 +00:00
}
}
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) {
that.map.triggerEvent('changeplaceend', that.place);
2011-04-22 22:03:10 +00:00
}
function getMarkerImage(options, callback) {
// fixme: unused
options = Ox.extend({
background: [255, 0, 0],
editing: false,
result: false,
selected: false,
size: 16
}, options);
var background = options.result ? [255, 255, 0] : [255, 0, 0],
border = options.editing ? [128, 128, 255] :
options.selected ? [255, 255, 255] : [0, 0, 0],
c = Ox.canvas(options.size, options.size),
image,
r = options.size / 2;
if (Ox.isArray(background)) {
c.context.fillStyle = 'rgba(' + background.join(', ') + ', 0.5)';
c.context.arc(r, r, r - 2, 0, 360);
c.context.fill();
renderImage();
} else {
image = new Image();
image.onload = renderImage;
image.src = background;
}
function renderImage() {
//var i;
if (Ox.isString(background)) {
c.context.drawImage(image, 1, 1, options.size - 2, options.size - 2);
/*
c.imageData = c.context.getImageData(0, 0, options.size, options.size);
c.data = c.imageData.data;
for (i = 3; i < c.data.length; i += 1) {
c.data[i] = Math.round(c.data[i] * 0.5);
}
c.context.putImageData(c.imageData, 0, 0);
*/
}
c.context.beginPath();
c.context.lineWidth = 2;
c.context.strokeStyle = 'rgb(' + border.join(', ') + ')';
c.context.arc(r, r, r - 1, 0, 360);
c.context.stroke();
callback(new google.maps.MarkerImage(
c.canvas.toDataURL(),
new google.maps.Size(options.size, options.size),
new google.maps.Point(0, 0),
new google.maps.Point(r, r)
));
}
}
2011-04-22 22:03:10 +00:00
function setOptions() {
// workaround to prevent marker from appearing twice
// after setting draggable from true to false (google maps bug)
2011-04-22 22:03:10 +00:00
var fix = that.marker.getDraggable() && !that.place.editing;
2011-06-14 14:53:32 +00:00
Ox.print('setOptions, that.map: ', that.map)
2011-06-01 14:16:48 +00:00
if (that.map.options('showTypes')) {
that.color = typeColor[that.place.type];
that.size = 8;
Ox.forEach(areaSize, function(size, area) {
if (that.place.area > area) {
that.size = size;
2011-06-02 08:04:22 +00:00
} else {
return false;
2011-06-01 14:16:48 +00:00
}
});
}
2011-04-22 22:03:10 +00:00
that.marker.setOptions({
// fixme: cursor remains pointer
cursor: that.place.editing ? 'move' : 'pointer',
draggable: that.place.editing,
icon: Ox.MapMarkerImage({
color: that.color,
mode: that.place.editing ? 'editing' :
that.place.selected ? 'selected' : 'normal',
size: that.size,
type: that.place.id[0] == '_' ? 'result' : 'place'
}),
2011-04-22 22:03:10 +00:00
position: that.place.center
});
if (fix) {
that.marker.setVisible(false);
setTimeout(function() {
that.marker.setVisible(true);
}, 0);
}
}
2011-05-16 10:49:48 +00:00
/*@
add <f> add to map
() -> <f> add to map, returns MapMarker
@*/
2011-04-22 22:03:10 +00:00
that.add = function() {
that.marker.setMap(that.map.map);
google.maps.event.addListener(that.marker, 'click', click);
return that;
};
2011-05-16 10:49:48 +00:00
/*@
edit <f> edit marker
() -> <f> edit marker, returns MapMarker
@*/
2011-04-22 22:03:10 +00:00
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);
2011-05-16 10:49:48 +00:00
return that;
2011-04-22 22:03:10 +00:00
};
2011-05-16 10:49:48 +00:00
/*@
remove <f> remove marker
() -> <f> remove marker from map, returns MapMarker
@*/
2011-04-22 22:03:10 +00:00
that.remove = function() {
that.marker.setMap(null);
google.maps.event.clearListeners(that.marker);
return that;
};
2011-05-16 10:49:48 +00:00
/*@
submit <f> submit marker
() -> <f> clear edit listeners, returns MapMarker
@*/
2011-04-22 22:03:10 +00:00
that.submit = function() {
google.maps.event.clearListeners(that.marker, 'dragstart');
google.maps.event.clearListeners(that.marker, 'drag');
google.maps.event.clearListeners(that.marker, 'dragend');
2011-05-16 10:49:48 +00:00
return that;
2011-04-22 22:03:10 +00:00
}
2011-05-16 10:49:48 +00:00
/*@
update <f> update marker
() -> <f> update marker, returns MapMarker
@*/
2011-04-22 22:03:10 +00:00
that.update = function() {
setOptions();
2011-05-16 10:49:48 +00:00
return that;
2011-04-22 22:03:10 +00:00
}
return that;
};