Open Layers 3 search functionality to find a map location?

Nicolas T picture Nicolas T · Dec 2, 2015 · Viewed 11k times · Source

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?

Answer

Jonatas Walker picture Jonatas Walker · Dec 3, 2015

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);
});