Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Layers 3 search functionality to find a map location?

What is the best way to implement a search functionality into an OL3 map?

I need a search input that will show me a few options while searching and then pan and zoom to the specific search term. Pretty much like google maps do.

Do I need to integrate google maps into my OL3?

like image 466
Nicolas T Avatar asked Dec 02 '15 16:12

Nicolas T


People also ask

What is the use of OpenLayers?

OpenLayers makes it easy to put a dynamic map in any web page. It can display map tiles, vector data and markers loaded from any source. OpenLayers has been developed to further the use of geographic information of all kinds.

How do you change basemap on OpenLayers?

Use the layer switcher menu at the top right to select and explore different basemap layer styles.


1 Answers

There's no native Search/Geocoder in Openlayers 3. But you can use the ol-geocoder extension (I've wrote it) to address this need.

The instructions are at the provided link. I just want to show how I generally use it:

//Instantiate with some options and add the Control
var geocoder = new Geocoder('nominatim', {
  provider: 'google',
  lang: 'pt-BR',
  placeholder: 'Pesquise por um endereço',
  limit: 5,
  key: '....',
  keepOpen: false,
  debug: true
});
map.addControl(geocoder);

// I don't want/need Geocoder layer to be visible
geocoder.getLayer().setVisible(false);

//Listen when an address is chosen
geocoder.on('addresschosen', function(evt){
  var feature = evt.feature;
  var coord = evt.coordinate;

  // application specific
  app.addMarker(feature, coord);
});
like image 109
Jonatas Walker Avatar answered Jan 02 '23 03:01

Jonatas Walker