Skip to content
Advertisement

Circle getBounds() method fails in Leaflet

I have:

const map = L.map("mapid", {preferCanvas: true});
//....    
const circle = L.circle([47.6652642, -122.3161248], {
    color: '#ea647f',
    fillOpacity: 0.4,
    radius: 30
}).addTo(map);

but calling getBounds() on circle fails:

const bounds = circle.getBounds();

It fails inside function getBounds in Circle.js which is Leaflet code,
The Leaflet getBounds method code is:

getBounds: function () {
        var half = [this._radius, this._radiusY || this._radius];

        return new LatLngBounds(
            this._map.layerPointToLatLng(this._point.subtract(half)),
            this._map.layerPointToLatLng(this._point.add(half)));
    }

Trying to access this._map.layerPointToLatLng fails
I get the error that this._map is undefined
Any ideas?

===================================================

Please Note:
I also have a polygon defined, and calling the getBounds() on the polygon passes fine and works correctly, shows correctly on the map.
=> It is only the Circle.getBounds() that fails

Advertisement

Answer

Add center and zoom to the map.

const map = L.map("map", {center:[47.6652642, -122.3161248], zoom: 16 ,preferCanvas: true});
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement