Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom UI to leaflet map?

Tags:

leaflet

I am using Leaflet to create a map game (very basic).

Basically I want to add an input <div> on the map so that when a user types in a location it will pan to a coordinate on the map.

I have tried creating elements and appending to the map <div> with variations of:

var d1 = document.getElementsByClassName('leaflet-control-container')[0]; 

d1.insertAdjacentHTML('afterbegin', '<div id="two">two</div>');

But the <div> is displayed behind the map and the image covers it.

How can I get it to show like the Zoom Control?

like image 293
maxflow Avatar asked Dec 19 '22 01:12

maxflow


1 Answers

If I understand correctly, you would like to create your own "Control" (somehow visually similar to the Leaflet default Zoom Control, but with different functionality), that would allow looking for different locations and navigate to them.

As for styling a Control similar to Leaflet default ones (zoom, layers control), you need to:

  1. Extend L.Control
  2. Specify an onAdd method that returns the DOM element to be used as Control on the map. Steps 1 and 2 will make your Control add-able to a map corner as a standard Control, with proper z-index and margin.
  3. Style it using your own class. To get a visual effect similar to the Zoom and Layers Controls, you can build on the leaflet-bar class:
.leaflet-bar {
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.65);
  border-radius: 5px;
}

Example: (derived from the "Extending Leaflet: Handlers and Controls" tutorial)

var map = L.map('map').setView([48.86, 2.35], 11);

L.Control.MyControl = L.Control.extend({
  onAdd: function(map) {
    var el = L.DomUtil.create('div', 'leaflet-bar my-control');

    el.innerHTML = 'My Control';

    return el;
  },

  onRemove: function(map) {
    // Nothing to do here
  }
});

L.control.myControl = function(opts) {
  return new L.Control.MyControl(opts);
}

L.control.myControl({
  position: 'topright'
}).addTo(map);

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
.my-control {
  background: #fff;
  padding: 5px;
}
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ==" crossorigin="" />
<script src="https://unpkg.com/[email protected]/dist/leaflet-src.js" integrity="sha512-2h9aokfcaYW7k0VPn1JqbQDQCaNQRrZJwetlnQ88yJrtIzGLVW/2StdQKoE+TIVNNTUxf6SVa+2vW2KB2EXnnA==" crossorigin=""></script>

<div id="map" style="height: 200px"></div>

That being said, the Control functionality that you would like to implement sounds very similar to that of the Leaflet Control Search plugin (aka "leaflet-search")

A Leaflet control that search markers/features location by custom property.

like image 95
ghybs Avatar answered Dec 29 '22 06:12

ghybs