Ad
How To Add A Search Box In Open Layers 3
I tried an example of open layers 3, it only adds a search box but doesn't search for places, below is that code -
<div id="search" style="position: absolute; top: 10px; right: 10px; padding: 5px; background-color: rgba(255,255,255,0.5);">
<input type="text" placeholder="place" style="width: 200px">
<button type="button">Search</button>
</div>
This is the other method I tried but, it says "geocoder is undefined", can anyone suggest any other method?
var geocoder = new Geocoder('nominatim', {
provider: 'mapquest',
key: 'osm',
lang: 'pt-BR', //en-US, fr-FR
placeholder: 'Search for ...',
targetType: 'text-input',
limit: 5,
keepOpen: true
});
map.addControl(geocoder);
geocoder.on('addresschosen', function(evt){
var feature = evt.feature,
coord = evt.coordinate,
address = evt.address;
// some popup solution
content.innerHTML = '<p>'+ address.formatted +'</p>';
overlay.setPosition(coord);
});
Ad
Answer
It looks like you tried to implement the Geocoder plugin by Jonatas Walker : https://github.com/jonataswalker/ol-geocoder (or has this implemented in the latest version of openlayers? - where did you take this example from?)
In this case, there are two things I think you are missing:
1) Add the css and javascript code in your template or index.html:
<link target="_blank" rel="nofollow noreferrer" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/ol-geocoder.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/ol-geocoder"></script>
2) Create an account and Register for a key at Mapquest since this is mandatory for this provider, or try with another provider that doesn't require key, like osm
:
var geocoder = new Geocoder('nominatim', {
provider: 'osm', //change it here
lang: 'en-US',
placeholder: 'Search for ...',
targetType: 'text-input',
limit: 5,
keepOpen: true
});
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad