Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to remove search pin? #13

Open
joto opened this issue Oct 23, 2018 · 7 comments
Open

How to remove search pin? #13

joto opened this issue Oct 23, 2018 · 7 comments

Comments

@joto
Copy link
Collaborator

joto commented Oct 23, 2018

When using the search a pin will be set on the map. There is currently no way to remove the pin.

@KMR-zoar
Copy link
Contributor

KMR-zoar commented Mar 1, 2019

The seach pin of ol-geocoder.js will be added to a layer named "geocoder-layer-....".
I could remove the pin(s) with the geocode-layer by adding the following code to line 635 in app.js:

function remove_search_pin() {
  var remove_layer_name = 'geocoder-layer';
  var layers_to_remove = [];
  map.getLayers().forEach(function(layer) {
    var layer_name = layer.getProperties().name;
    if (layer_name && layer_name.match(remove_layer_name)) {
      layers_to_remove.push(layer);
    }
  });

  for (var i = 0; i < layers_to_remove.length; i++) {
    map.removeLayer(layers_to_remove[i]);
  }
}

After Geocode I called this function using Chrome's DevTools.

@joto
Copy link
Collaborator Author

joto commented Mar 3, 2019

Thanks for figuring this out! Can you wire this up so that you can click on the pin to remove it and send me a pull request? Would be great!

@KMR-zoar
Copy link
Contributor

KMR-zoar commented Mar 5, 2019

I created pull request for function and clickable button.

The button is available under the ol-geocoder button.
I made an icon by processing SVG published under CC0 license.

Search with ol-geocoder.
image1

Click the icon to delete the pin.
2

I could not create a function that hooks to ol-geocoder and can show/hide buttons...

@joto
Copy link
Collaborator Author

joto commented Mar 5, 2019

I am not too happy about the extra button. It adds cognitive load for the user, especially because its use is not obvious and it does nothing until you actually use the search function. Maybe this is something we need to add upstream to the ol-geocoder instead of trying to fix this here.

@KMR-zoar
Copy link
Contributor

KMR-zoar commented Mar 6, 2019

I agree with that idea too.

I found 'propertychange' event on map.getLayers() on OpenLayers, added function that hook this event and change css for pin remove button when geocoding at bcd3546
It should not be affected by adding or changing the QA layer.

But my code may be a little redundant.

  • The button is hidden before geocoding
  • Geocode it you will show the button
  • The button hidden when deleting the geocode layer

@joto
Copy link
Collaborator Author

joto commented Mar 9, 2019

Thanks, this looks good now. I have merged this. We can always improve on this later, if the upstream ol-geocoder adds some functionality.

@PeshawaEng
Copy link

PeshawaEng commented Sep 20, 2022

Thank you for the information.

This helps to remove or clear the mark when I hit the clear button. However, in my case, the geocoder also shows a popup of the address of the pin.
I also want to remove (clear or hide) the popup window when I hit the same button. Can you please clarify how to do that?

`
var container2 = document.getElementById('popup2');

var geocoder = new Geocoder('nominatim', {
provider: 'osm',
lang: 'en',
placeholder: 'Search for ...',
limit: 5,
debug: false,
autoComplete: true,
keepOpen: true
});
map.addControl(geocoder);

geocoder.on('addresschosen', function(evt) {
console.info(evt);
if (popup2) {
popup2.hide();
}
window.setTimeout(function() {
popup2.show(evt.coordinate, evt.address.formatted);
}, 3);
});

function clear_all() {
var remove_layer_name = 'geocoder-layer';
var layers_to_remove = [];
map.getLayers().forEach(function(layer) {
var layer_name = layer.getProperties().name;
if (layer_name && layer_name.match(remove_layer_name)) {
layers_to_remove.push(layer);
}
});

for (var i = 0; i < layers_to_remove.length; i++) {
map.removeLayer(layers_to_remove[i]);
}

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants