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

Access to Marker Object(s) #132

Closed
mepa1363 opened this issue Jul 25, 2016 · 4 comments
Closed

Access to Marker Object(s) #132

mepa1363 opened this issue Jul 25, 2016 · 4 comments

Comments

@mepa1363
Copy link

I would like to know if there is a way (besides hacking the code) to get access to the marker object(s). What I'm looking for is to disable popup (or change its content), and to make the marker draggable.
Thanks.

@louh
Copy link
Contributor

louh commented Jul 25, 2016

Hi @mepa1363,

The first thing you can do (and is officially supported) is to set your default marker options during in plugin initalization:

var geocoder = L.control.geocoder('<your-api-key>', {
    markers: {
        draggable: true
    }
}).addTo(map);

Notice that I assigned the geocoder to a variable. This way I have access to it later, and read the markers property on the geocoder object:

console.log(geocoder.markers); // --> [ Array ]
geocoder.markers[0].setPopupContent('Hello world');

The array represents any markers that are currently visible, so if something in the plugin clears the markers on the map, then the references to that marker are cleaned up as well.

Referring to the markers in this way is not documented but I think it's useful if it were officially supported, so please give it a try and let me know what you think. If we can iron out any kinks in this implementation then I'll go ahead and release it as a documented feature in a future version.

@mepa1363
Copy link
Author

mepa1363 commented Jul 26, 2016

Hi @louh,

Thanks for your help. Everything worked well except for the function that unbinds (disable) the popup. Is there any way to disable popups?

One more thing; I am using two geocoder controls in a single interface. Since the markers are draggable, I am sending the coordinates to the Mapzen reverse geocoder to get the address back. Now I want to change the text in the associated search box accordingly. Is there a built-in function to that?

Thanks.

@louh
Copy link
Contributor

louh commented Jul 26, 2016

Re: disabling popups. Hmm. It might be hard because the plugin binds and shows the popup immediately so there's not a way to hook into that and modify the behavior (without hacks). I did write a proposal for a popup option that would allow you to control its behavior, here: #72, which we would need to build into the plugin. Let me know if that would work the way you'd expect.

Re: changing text in the search box after a reverse geocode - it sounds like you've implemented your own reverse geocoding functionality that occurs on marker dragging. You can set the input in the text box like this:

geocoder._input.value = 'Your text';

(Assuming you have geocoder variable assigned to one instance of the control)

_input is an internal variable and it's not documented, but it is not likely to change.

@mepa1363
Copy link
Author

Hi @louh,

Thanks so much for you help. It works like a charm.

@louh louh closed this as completed Mar 7, 2017
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

2 participants