Files
contao-leaflet-maps/module/templates/be_leaflet_geocode.html5

66 lines
2.5 KiB
Plaintext
Raw Normal View History

2015-01-06 21:52:36 +01:00
<?php $GLOBALS['TL_CSS'][] = 'assets/leaflet/libs/leaflet/leaflet.css'; ?>
<?php $GLOBALS['TL_CSS'][] = 'assets/leaflet/libs/control-geocoder/Control.Geocoder.css'; ?>
2014-12-29 12:17:40 +01:00
2015-01-06 21:52:36 +01:00
<?php $GLOBALS['TL_JAVASCRIPT'][] = 'assets/leaflet/libs/leaflet/leaflet.js'; ?>
<?php $GLOBALS['TL_JAVASCRIPT'][] = 'assets/leaflet/libs/control-geocoder/Control.Geocoder.js'; ?>
2014-12-29 12:17:40 +01:00
<div id="map_<?php echo $this->field; ?>" style="margin-top: 5px; width: 670px; height: 300px"></div>
<script>
var map = L.map('map_<?php echo $this->field; ?>').setView([0, 0], 2);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
2016-10-05 08:39:28 +02:00
var element = $('<?php echo $this->field; ?>');
2014-12-29 12:17:40 +01:00
var geocoder = L.Control.geocoder({
2016-10-05 08:58:10 +02:00
defaultMarkGeocode: false,
2014-12-29 12:17:40 +01:00
collapsed: false,
2015-01-19 14:25:01 +01:00
placeholder: '<?php echo $GLOBALS['TL_LANG']['leaflet']['searchPosition']; ?>'
2014-12-29 12:17:40 +01:00
}).addTo(map);
2016-10-05 08:23:34 +02:00
geocoder.on('markgeocode', function(event) {
2014-12-29 12:17:40 +01:00
var container = document.createElement('div');
var link = document.createElement('button');
2016-10-05 08:23:34 +02:00
var result = event.geocode;
2014-12-29 12:17:40 +01:00
link.set('style', 'margin-left: 10px;');
2015-01-19 14:25:01 +01:00
link.appendText('<?php echo $GLOBALS['TL_LANG']['leaflet']['applyPosition']; ?>');
2014-12-29 12:17:40 +01:00
link.addEvent('click', function(e) {
e.stop();
element.set('value', result.center.lat + ',' + result.center.lng);
});
container.appendHTML(result.html || result.name);
container.appendChild(link);
if (this._geocodeMarker) {
map.removeLayer(this._geocodeMarker);
}
map.fitBounds(result.bbox, { padding: [0, 70]});
map.panTo(result.center);
2016-10-05 08:58:10 +02:00
this._geocodeMarker = new L.Marker(result.center, {draggable: true})
2014-12-29 12:17:40 +01:00
.bindPopup(container, {
keepInView: true,
autoPanPaddingTopLeft: [0, 70]
})
.addTo(map)
.openPopup();
2016-10-05 08:39:28 +02:00
this._geocodeMarker.on('dragend', function (event) {
element.set('value', event.target._latlng.lat + ',' + event.target._latlng.lng);
});
2016-10-05 08:23:34 +02:00
});
2014-12-29 12:17:40 +01:00
<?php if ($this->marker): ?>
2016-10-05 08:39:28 +02:00
geocoder._geocodeMarker = L.marker(<?php echo $this->marker; ?>, {draggable: true}).addTo(map);
geocoder._geocodeMarker.on('dragend', function (event) {
element.set('value', event.target._latlng.lat + ',' + event.target._latlng.lng);
});
2014-12-29 12:17:40 +01:00
map.setZoom(16);
map.panTo(<?php echo $this->marker; ?>);
<?php endif; ?>
</script>