getBounds on featureGroup

947 views
Skip to first unread message

Rowan Winsemius

unread,
Feb 9, 2014, 4:08:29 AM2/9/14
to leaflet-ve...@googlegroups.com
Hi there,

Another rookie question.

So Im trying to get my map to automatically zoom to some filtered features from an arcgis web service, I gather the best way to do this to get the bounds of a feature group of which my WFS should belong. I'm having some trouble however getting this to work

This is what I've got at the moment

    <script>
        var map = L.map('map').setView([45.52751668442124, -122.67175197601318], 13);

        L.tileLayer('http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png', {
            maxZoom: 18,
            attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>'
        }).addTo(map);


     myAGSLayer = new lvector.AGS({
                   url: "http://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/Parks_pdx/FeatureServer/0",
                fields: "PROPERTYID,NAME,ACRES",
                uniqueField: "OBJECTID",
                where: "PROPERTYID = '1128'",
                scaleRange: [5, 20],
                    popupTemplate: 'Property ID: {PROPERTYID}</br>Acres: {ACRES}'   
                }).setMap(map);
       

        var lmdb = new L.featureGroup([myAGSLayer]);

        map.fitBounds(lmdb.getBounds());
    </script>

I've managed to get the getBounds working on another example but no luck with this one.

Any tips?

Thanks,
Rowan



This message is intended for the addressee named and may contain confidential information. If you are not the intended recipient, please delete it and notify the sender. Views expressed in this message are those of the individual sender, and are not necessarily the views of their organisation.

Jason Sanford

unread,
Feb 9, 2014, 4:35:22 PM2/9/14
to leaflet-ve...@googlegroups.com
Care to attach your code like you did with your other question? This makes it much easier to debug.


--
You received this message because you are subscribed to the Google Groups "Leaflet Vector Layers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leaflet-vector-l...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Rowan Winsemius

unread,
Feb 9, 2014, 4:45:04 PM2/9/14
to leaflet-ve...@googlegroups.com
Attached, thanks again for your assistance!
To unsubscribe from this group and stop receiving emails from it, send an email to leaflet-vector-layers+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
Archive.zip

Jason Sanford

unread,
Feb 9, 2014, 4:53:00 PM2/9/14
to leaflet-ve...@googlegroups.com
Ahh right. The Leaflet Vector Layers layers do not actually implement the iLayer interface for Leaflet. To pass in myAGSLayer to a new FeatureGroup like you're do would require LVL layers to implement the iLayer interface. There was a ticket to implement this a while back, but I never got around to it.


To unsubscribe from this group and stop receiving emails from it, send an email to leaflet-vector-l...@googlegroups.com.

Rowan Winsemius

unread,
Feb 9, 2014, 4:59:37 PM2/9/14
to leaflet-ve...@googlegroups.com
mmm I did see that ticket and was hoping it might have been actioned but I can understand why you didnt if you're doing this out of the goodness of your heart :)

Are there any other ways that you can think of for zooming to features (my filter will eventually be dynamic hence I can't have a static map centre)?

Perhaps I'll give the esri leaflet plugin a go
https://github.com/Esri/esri-leaflet
To unsubscribe from this group and stop receiving emails from it, send an email to leaflet-vector-layers+unsubscri...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.


This message is intended for the addressee named and may contain confidential information. If you are not the intended recipient, please delete it and notify the sender. Views expressed in this message are those of the individual sender, and are not necessarily the views of their organisation.

--
You received this message because you are subscribed to the Google Groups "Leaflet Vector Layers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leaflet-vector-layers+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Rowan Winsemius

unread,
Feb 10, 2014, 8:21:26 PM2/10/14
to leaflet-ve...@googlegroups.com
Hi Jason

So much for the esri plugin, a bit too complicated for me, and I seem so close with this one :)

I played around with the pull request re adding Ilayer
https://github.com/JasonSanford/leaflet-vector-layers/pull/31
and things seem to be working.

The sample html code can successfully create a LayerGroup and I think I've now succesfully tweaked it so that I have a featureGroup which I gather is required for the getbounds. However the getBounds operation isn't working, I get an error saying getBounds is not a function.

Attached is the code Im using, its the debug/almost-working.html

Any chance you can take one last look for me, if you're ever in the middle of nowhere Australia I'll buy you a beer!

Thanks,
Rowan

JasonSanford-leaflet-vector-layers-v1.5.1-0-gdaaf31d.zip

Jason Sanford

unread,
Feb 10, 2014, 9:39:45 PM2/10/14
to leaflet-ve...@googlegroups.com
The problem is that Leaflet is expecting the getBounds method to have been implemented, and I never have for Leaflet Vector Layers. Attached is a your zip, rezipped with my quick hack of that implemented. I changed some code around in almost-working.html and implemented the following in /src/layer/Layer.js

    getBounds: function () {
        var vectors = this._vectors,
            bounds = new L.LatLngBounds(),
            i,
            vector;
        for (i = 0, len = vectors.length; i < len; i++) {
            vector = vectors[i].vector;
            bounds.extend(vector.getBounds());
        }
        return bounds;
    }


--
You received this message because you are subscribed to the Google Groups "Leaflet Vector Layers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leaflet-vector-l...@googlegroups.com.
even-closer-to-working.zip
Message has been deleted

Chris Richter

unread,
Feb 12, 2014, 4:13:48 PM2/12/14
to leaflet-ve...@googlegroups.com
Image for above post attached.
leaflet-console.png

Chris Richter

unread,
Feb 12, 2014, 4:15:45 PM2/12/14
to leaflet-ve...@googlegroups.com
Hi Jason,

I had a look through the code and it appears that in getBounds - this.vectors does have objects inside it but vectors.length is 0.
In the console it looks like this if I use the following code.

getBounds: function () {
       console.log("console.dir: ");
       console.dir(this._vectors); // Displays there are no child objects
       console.log("console.log:");
       console.log(this._vectors); // displays [ ] then when you click on it, shows objects
...

So currently the bounds are not set because the vectors loop is 0.

Any ideas?

Thanks,
Chris

Chris Richter

unread,
Feb 13, 2014, 12:44:11 AM2/13/14
to leaflet-ve...@googlegroups.com
Hi Jason, Rowan,

The problem is that the vectors data does not exists in the hurricanes object at the time that you call hurricanes.getBounds()

If you put the map.fitBounds(hurricanes.getBounds()) inside a setTimeout, except for the pause, it now re-sets the map boundaries.

Put this line below in place of map.fitBounds(hurricanes.getBounds()).

setTimeout(function(){ console.log("done");map.fitBounds(hurricanes.getBounds());}, 5000);

Obviously this doesn't fix the issue but show why it doesn't work.

Is there an event for for something like layerComplete that we could use to trigger the reset bounds?

Thanks,
Chris
Reply all
Reply to author
Forward
0 new messages