Leaflet - Layers

Are you having problems with using or developing a plugin? Let me know here.
Post Reply
nitro999
Posts: 82
Joined: Tue Feb 09, 2016 7:32 pm
Location: UK
Contact:

Leaflet - Layers

Post by nitro999 » Mon Jul 23, 2018 11:06 pm

Hi all

Struggling to get any layers working with leaflet so would appreciate some help. For example, just to keep it simple, say I want to draw a polygon over the map, I wrote the following but it's not working. Suggestions gratefully received.

Many thanks

Code: Select all

<script type="text/javascript">
    if (VRS && VRS.globalDispatch && VRS.serverConfig) {
        VRS.globalDispatch.hook(VRS.globalEvent.bootstrapCreated, function(bootStrap) {
            bootStrap.hookInitialised(function(pageSettings) {
                _Map = pageSettings.mapPlugin;
                
var polygon = L.polygon([
    [51.509, -0.08],
    [51.503, -0.06],
    [51.51, -0.047]
]).addTo(_Map);


                }
            });
        });
    }
</script>


agw
Posts: 2247
Joined: Fri Feb 17, 2012 3:20 am

Re: Leaflet - Layers

Post by agw » Tue Jul 24, 2018 12:14 am

The map plugin is a wrapper around either Leaflet or Google Maps (https://github.com/vradarserver/vrs/blo ... edefs.d.ts, search in there for IMap). You need to call getNativeType() and check whether it returns GoogleMaps or OpenStreetMap (I should really have had it return Leaflet, originally the plugin was called OpenStreetMap and I forgot to change it). If it returns OpenStreetMap then a call to getNative() will return the native Leaflet map handle. You can pass the native map handle to addTo().

I know you're not actually trying to draw a polygon but if the map plugin already has wrappers for whatever you're trying to do then it's better to use those rather than going straight for the native calls. Adding and removing polygons is supported by the map wrapper.

nitro999
Posts: 82
Joined: Tue Feb 09, 2016 7:32 pm
Location: UK
Contact:

Re: Leaflet - Layers

Post by nitro999 » Tue Jul 24, 2018 8:39 am

Thanks, Andrew. Helpful as always!

Post Reply