Open Layers 3 search functionality to find a map location?

12,605

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);
});
Share:
12,605
Nicolas T
Author by

Nicolas T

Updated on June 21, 2022

Comments

  • Nicolas T
    Nicolas T almost 2 years

    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?