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

method call on marker click #354

Open
Khodor1997 opened this issue Oct 28, 2022 · 2 comments
Open

method call on marker click #354

Khodor1997 opened this issue Oct 28, 2022 · 2 comments

Comments

@Khodor1997
Copy link

Is it possible to implement marker click events after a search to call a custom function? Please advise how this can be done.
Using vue2
Thanks

@ahmedkhairydev
Copy link

ahmedkhairydev commented Mar 28, 2023

I have built my custom workaround to do that.

First, Add the search control to your map.

const searchControl = SearchControl({
  style: "bar",
  marker: {
    opacity: 0,
  },
  provider: new OpenStreetMapProvider(),
  autoCompleteDelay: 500,
  autoClose: true,
});

map.addControl(searchControl);

// popupopen logic
searchControl.markers.on("popupopen", () => {
  // enter your custom logic here...
});

Second, Add the keydown event listener to get the selected result option when the user presses Enter in the search input.

searchControl.searchElement.input.addEventListener("keydown", (event) => {
  const ENTER_KEY = "Enter",
    results = searchControl.resultList.results,
    inputValue = event.target["value"];

  if (event.key === ENTER_KEY && results.length) {
    // check if the user chooses an option from the results by the keyboard arrows
    if (results.find((result) => result.label === inputValue)) {
      getTheSelectedOption(
        results.findIndex((result) => result.label === inputValue)
      );
    } else {
      // choose the first option of the results in case the user pressed `Enter` without marking any option by the keyboard arrows
      getTheSelectedOption();
    }
  }
});

Third, Add the click event listener to get the selected option.

searchControl.resultList.container.addEventListener("click", (event) => {
  const itemIndex = +(event.target as HTMLElement).getAttribute(
    "data-key"
  );
  getTheSelectedOption(itemIndex);
});

Finally, Add the getTheSelectedOption function which you should write your custom logic inside it.

const getTheSelectedOption = (itemIndex = 0) => {
  const item = searchControl.resultList.results[itemIndex];

  // enter your custom logic here...
};

I hope this will help you.

@ahmedkhairydev
Copy link

@Khodor1997

I found this event also, map.on('geosearch/showlocation', yourEventHandler);

@smeijer smeijer closed this as completed Apr 22, 2023
@smeijer smeijer reopened this Apr 22, 2023
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