1
0
Fork 0
forked from 0x2620/oxjs
oxjs/source/UI/js/Map/MapMarker.js

350 lines
11 KiB
JavaScript
Raw Normal View History

2011-11-05 17:46:53 +01:00
'use strict';
2011-05-16 12:49:48 +02:00
/*@
2012-05-31 12:32:54 +02:00
Ox.MapMarker <f> MapMarker
(options) -> <o> MapMarker object
2011-05-16 12:49:48 +02:00
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-23 00:03:10 +02:00
Ox.MapMarker = function(options) {
options = Ox.extend({
map: null,
place: null
}, options);
2011-05-30 20:57:22 +02:00
var that = this,
2011-05-31 09:24:03 +02:00
areaSize = {
2011-06-01 12:50:43 +02: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 14:08:29 +02:00
100000000000000: 22 // 10,000 x 10,000 km
2012-12-28 18:01:13 +01:00
},
themeData = Ox.Theme.getThemeData(),
typeColor = {};
[
'country', 'region', 'city', 'borough',
'street', 'building', 'feature'
].forEach(function(type) {
typeColor[type] = themeData[
'mapPlace' + Ox.toTitleCase(type) + 'Color'
];
});
2011-05-31 09:24:03 +02:00
2011-04-23 00:03:10 +02:00
Ox.forEach(options, function(val, key) {
that[key] = val;
});
2025-08-05 18:50:06 +02:00
setColor()
2025-08-06 19:29:16 +02:00
setSize()
2025-08-05 18:50:06 +02:00
const element = document.createElement('div')
element.style.border = '2px solid black'
element.style.borderRadius = '50px'
element.style.backgroundColor = '#' + Ox.toHex(that.color)
2025-08-06 19:29:16 +02:00
element.style.width = element.style.height = that.size + 'px'
2025-08-05 18:50:06 +02:00
that.marker = new maplibregl.Marker({
2011-04-23 00:03:10 +02:00
raiseOnDrag: false,
2025-08-05 18:50:06 +02:00
shape: {coords: [8, 8, 8], type: 'circle'},
element: element,
2011-10-31 11:29:59 +00:00
//title: that.place.name,
2011-04-23 00:03:10 +02:00
//zIndex: 1000
});
2025-08-05 18:50:06 +02:00
that.tooltip = new maplibregl.Popup({
closeButton: false,
closeOnClick: false,
className: 'tooltip'
});
that.tooltip.addClass
2011-04-23 00:03:10 +02:00
setOptions();
2025-08-05 18:50:06 +02:00
function click(event) {
event.preventDefault()
event.stopPropagation()
var key = that.map.getKey(),
place, bounds, southWest, northEast;
2011-04-23 00:03:10 +02:00
if (!that.place.selected) {
if (
that.map.options('editable')
&& (key == 'meta' || key == 'shift')
) {
place = that.map.getSelectedPlace();
}
if (place) {
2025-08-05 18:50:06 +02:00
bounds = new maplibregl.LngLatBounds(
new maplibregl.LngLatBounds(place.west, place.south),
new maplibregl.LngLatBounds(place.east, place.north)
);
2011-06-14 17:03:26 +02:00
bounds = bounds.union(that.place.bounds);
southWest = bounds.getSouthWest();
northEast = bounds.getNorthEast();
2011-06-14 16:41:20 +02:00
that.map.newPlace(new Ox.MapPlace({
2011-06-14 16:48:46 +02:00
// fixme: duplicated, see Ox.Map.js
alternativeNames: [],
countryCode: '',
editable: true,
geoname: '',
id: '_' + Ox.encodeBase32(Ox.uid()), // fixme: stupid
2011-06-14 16:49:41 +02:00
map: that.map,
2011-06-14 16:48:46 +02:00
name: '',
type: 'feature',
2025-08-05 18:50:06 +02:00
south: southWest.lat,
west: southWest.lng,
north: northEast.lat,
east: northEast.lng
}));
2011-06-14 16:34:09 +02:00
} else {
that.map.options({selected: that.place.id});
}
2011-04-23 00:03:10 +02:00
} else {
if (key == 'meta') {
2011-06-14 16:34:09 +02:00
that.map.options({selected: null});
} else {
that.map.panToPlace();
}
2011-04-23 00:03:10 +02:00
}
}
function dblclick() {
that.place.selected && that.map.zoomToPlace();
}
2011-04-23 00:03:10 +02:00
function dragstart(e) {
Ox.$body.addClass('OxDragging');
2011-04-23 00:03:10 +02:00
}
function drag(e) {
var northSouth = (that.place.north - that.place.south) / 2,
lat = Ox.limit(
2025-08-05 18:50:06 +02:00
e.lngLat.lat,
2011-04-23 00:03:10 +02:00
Ox.MIN_LATITUDE + northSouth,
Ox.MAX_LATITUDE - northSouth
),
2025-08-05 18:50:06 +02:00
lng = e.lngLat.lng,
2011-04-23 00:03:10 +02:00
span = Math.min(
that.place.sizeEastWest * Ox.getDegreesPerMeter(lat) / 2, 179.99999999
2011-12-31 19:51:32 +05:30
),
2011-04-23 00:03:10 +02:00
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;
}
2011-11-04 16:54:28 +01:00
Ox.Log('Map', 'west', that.place.west, 'east', that.place.east, 'span', span);
2011-04-23 00:03:10 +02:00
that.place.update();
2025-08-05 18:50:06 +02:00
that.marker.setLngLat(that.place.center)
2011-04-23 00:03:10 +02:00
that.place.rectangle.update();
}
function dragend(e) {
Ox.$body.removeClass('OxDragging');
that.map.triggerEvent('changeplaceend', that.place);
2011-04-23 00:03:10 +02: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();
2025-08-05 18:50:06 +02:00
callback(new maplibregl.MarkerImage(
c.canvas.toDataURL(),
2025-08-05 18:50:06 +02:00
new maplibregl.Size(options.size, options.size),
new maplibregl.Point(0, 0),
new maplibregl.Point(r, r)
));
}
}
2011-10-31 11:29:59 +00:00
function mouseover(e) {
2025-08-05 18:50:06 +02:00
that.tooltip.setLngLat(that.place.center).setHTML(
'<div style="display: flex; gap: 8px; margin: 2px 2px"><img src="' + Ox.getFlagByGeoname(that.place.geoname, 16)
+ '" style="border-radius: 4px;margin: auto"/>'
+ '<div style="font-size: 9px;">'
+ that.map.options('markerTooltip')(that.place) + '</div></div>'
).addTo(that.map.map);
2011-10-31 11:29:59 +00:00
}
function mouseout() {
2025-08-05 18:50:06 +02:00
that.tooltip.remove();
2011-10-31 11:29:59 +00:00
}
2025-08-05 18:50:06 +02:00
function setColor() {
var color = that.map.options('markerColor');
2011-11-04 16:54:28 +01:00
//Ox.Log('Map', 'setOptions, that.map: ', that.map)
if (color == 'auto') {
that.color = typeColor[that.place.type] || typeColor['mapPlaceFeatureColor'];
} else if (Ox.isArray(color)) {
that.color = color;
} else {
that.color = color(that.place);
}
2025-08-05 18:50:06 +02:00
}
2025-08-06 19:29:16 +02:00
function setSize() {
var size = that.map.options('markerSize');
if (size == 'auto') {
that.size = 8;
Ox.forEach(areaSize, function(size, area) {
if (that.place.area >= area) {
that.size = size;
} else {
2012-07-05 10:58:08 +02:00
return false; // break
}
});
} else if (Ox.isNumber(size)) {
that.size = size;
} else {
that.size = size(that.place);
2011-06-01 16:16:48 +02:00
}
2025-08-06 19:29:16 +02:00
}
function setOptions() {
// workaround to prevent marker from appearing twice
// after setting draggable from true to false (google maps bug)
var fix = false, // that.marker.getDraggable() && !that.place.editing,
size = that.map.options('markerSize');
//Ox.Log('Map', 'setOptions, that.map: ', that.map)
setColor()
setSize()
2025-08-05 18:50:06 +02:00
/* fixme, some of those can be set some not
2011-04-23 00:03:10 +02:00
that.marker.setOptions({
// fixme: cursor remains pointer
cursor: that.place.editing ? 'move' : 'pointer',
draggable: that.place.editing,
2025-08-05 18:50:06 +02:00
element: 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'
}),
2025-08-05 18:50:06 +02:00
})
*/
//that.marker._color = that.color;
that.marker._element.style.cursor = that.place.editing ? 'move' : 'pointer';
2025-08-06 19:29:16 +02:00
that.marker._element.height = that.marker._element.width = that.size + 'px'
2025-08-05 18:50:06 +02:00
that.marker.setDraggable(that.place.editing);
that.marker.setLngLat(that.place.center);
2011-04-23 00:03:10 +02:00
if (fix) {
that.marker.setVisible(false);
setTimeout(function() {
that.marker.setVisible(true);
}, 0);
}
2011-10-31 11:29:59 +00:00
}
2011-04-23 00:03:10 +02:00
2011-05-16 12:49:48 +02:00
/*@
add <f> add to map
() -> <f> add to map, returns MapMarker
@*/
2011-04-23 00:03:10 +02:00
that.add = function() {
2025-08-05 18:50:06 +02:00
that.marker.addTo(that.map.map);
const element = that.marker.getElement()
if(element) {
element.addEventListener('click', click)
element.addEventListener('dblclick', dblclick)
element.addEventListener('mouseover', mouseover)
element.addEventListener('mouseout', mouseout)
}
2011-04-23 00:03:10 +02:00
return that;
};
2011-05-16 12:49:48 +02:00
/*@
edit <f> edit marker
() -> <f> edit marker, returns MapMarker
@*/
2011-04-23 00:03:10 +02:00
that.edit = function() {
setOptions();
2025-08-05 18:50:06 +02:00
that.marker.on('dragstart', dragstart);
that.marker.on('drag', drag);
that.marker.on('dragend', dragend);
2011-05-16 12:49:48 +02:00
return that;
2011-04-23 00:03:10 +02:00
};
2011-11-03 16:42:41 +01:00
2011-05-16 12:49:48 +02:00
/*@
remove <f> remove marker
() -> <f> remove marker from map, returns MapMarker
@*/
2011-04-23 00:03:10 +02:00
that.remove = function() {
2025-08-05 18:50:06 +02:00
that.marker.remove();
//that.marker.off('dragstart');
//that.marker.off('drag');
//that.marker.off('dragend');
//fixme does this work to remove all events?
that.marker.off();
2011-04-23 00:03:10 +02:00
return that;
};
2011-05-16 12:49:48 +02:00
/*@
submit <f> submit marker
() -> <f> clear edit listeners, returns MapMarker
@*/
2011-04-23 00:03:10 +02:00
that.submit = function() {
2025-08-05 18:50:06 +02:00
that.marker.off('dragstart');
that.marker.off('drag');
that.marker.off('dragend');
2011-05-16 12:49:48 +02:00
return that;
2011-04-23 00:03:10 +02:00
}
2011-05-16 12:49:48 +02:00
/*@
update <f> update marker
() -> <f> update marker, returns MapMarker
@*/
2011-04-23 00:03:10 +02:00
that.update = function() {
setOptions();
2011-05-16 12:49:48 +02:00
return that;
2011-04-23 00:03:10 +02:00
}
return that;
};