最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Programmatically opening a popup in a Mapbox map - Stack Overflow

programmeradmin3浏览0评论

I have a mapbox map at / which has popups that open when clicking a marker.

My problem is that I need a way to set a default popup to be open on page load based on a value in the URL. This can be a latitude and longitude or some other value, whatever's easiest.

I have banged on this for a while and it seems like I either need to:

  1. Programmatically open a popup via the Mapbox JS API, which I can't figure out since it seems that my popups are auto-generated on the fly when a marker is clicked, or
  2. Programmatically click a marker to open its popup. I can't figure this out because 1) I don't know how to find a marker by lat/lon and 2) I can't figure out how to click a marker with JS.

I've tried this:

map.gridLayer.fire('click', {latLng: L.latLng(28.04419, -81.947864)});

which closes any existing open popups but doesn't seem to open its own. I've also tried digging through the map and the leaflet objects to see if location/marker data is stored in there and I can't find anything besides the tiles.

Most of the examples I can find seem to be using GeoJSON which I'm not using so that makes things difficult.

Any advice?

I have a mapbox map at http://bei.dev.bclcmaps./ which has popups that open when clicking a marker.

My problem is that I need a way to set a default popup to be open on page load based on a value in the URL. This can be a latitude and longitude or some other value, whatever's easiest.

I have banged on this for a while and it seems like I either need to:

  1. Programmatically open a popup via the Mapbox JS API, which I can't figure out since it seems that my popups are auto-generated on the fly when a marker is clicked, or
  2. Programmatically click a marker to open its popup. I can't figure this out because 1) I don't know how to find a marker by lat/lon and 2) I can't figure out how to click a marker with JS.

I've tried this:

map.gridLayer.fire('click', {latLng: L.latLng(28.04419, -81.947864)});

which closes any existing open popups but doesn't seem to open its own. I've also tried digging through the map and the leaflet objects to see if location/marker data is stored in there and I can't find anything besides the tiles.

Most of the examples I can find seem to be using GeoJSON which I'm not using so that makes things difficult.

Any advice?

Share Improve this question asked Aug 19, 2013 at 21:04 Mike CrittendenMike Crittenden 5,9276 gold badges49 silver badges77 bronze badges 1
  • Could you share some sample code? I've never used Leaflet before, but it can't be any worse than debugging/extending OpenLayers shudder. Maybe this is an easy one for Mapbox/Leaflet expersts, otherwise I'd be happy to take a shot at this with a little more context. – natevw Commented Aug 20, 2013 at 15:51
Add a ment  | 

2 Answers 2

Reset to default 3
map.fireEvent('click', {latlng: L.latLng(28.04419, -81.947864)});

You were close - the 'click' event needs a latLng object named 'latlng' (lower case)

For mapbox gl you can currently use map.fire('click', [-118.3214,34.0318])

but it may be deprecated in the future https://github./mapbox/mapbox-gl-js/blob/master/CHANGELOG.md#0450

发布评论

评论列表(0)

  1. 暂无评论