admin管理员组文章数量:1332359
I'm trying to fit a map to bounds defined by a 2D array. I keep getting the error Error: Bounds are not valid. leaflet.js:5:21909
even though the markers are added to the map and are valid coords.
var map = L.map('map', { zoomControl:false });
L.tileLayer('http://{s}.tile.osm/{z}/{x}/{y}.png', {
attribution: '© <a href="">OpenStreetMap contributors</a>'
}).addTo(map);
map.setView([0, 0], 2);
var markers = new L.FeatureGroup();
map.addLayer(markers);
function drawResults(){
// get offer keys
var offers = new Array();
var articles = [];
offersRef.once("value", function(snapshot) {
var offerKeys = Object.keys(snapshot.val());
for (var i=0; i<offerKeys.length; i++){
var offer = snapshot.val()[offerKeys[i]];
var lat = offer.lat;
var lng = offer.lng;
console.log(lat);// outputs 33.2321
console.log(lng);// outputs 101.1234
offers.push([lat, lng]);
var marker = L.marker([lat, lng]).addTo(markers);
}
});
map.fitBounds(markers.getBounds());
}
console.log(offers);
}
drawResults();
Can anyone see what I'm doing wrong?
Edit: Console logs
35.0721909 app.js:350:13
-106.48798399999998 app.js:351:13
35.0526641 app.js:350:13
-78.87835849999999 app.js:351:13
I'm trying to fit a map to bounds defined by a 2D array. I keep getting the error Error: Bounds are not valid. leaflet.js:5:21909
even though the markers are added to the map and are valid coords.
var map = L.map('map', { zoomControl:false });
L.tileLayer('http://{s}.tile.osm/{z}/{x}/{y}.png', {
attribution: '© <a href="http://openstreetmap/copyright">OpenStreetMap contributors</a>'
}).addTo(map);
map.setView([0, 0], 2);
var markers = new L.FeatureGroup();
map.addLayer(markers);
function drawResults(){
// get offer keys
var offers = new Array();
var articles = [];
offersRef.once("value", function(snapshot) {
var offerKeys = Object.keys(snapshot.val());
for (var i=0; i<offerKeys.length; i++){
var offer = snapshot.val()[offerKeys[i]];
var lat = offer.lat;
var lng = offer.lng;
console.log(lat);// outputs 33.2321
console.log(lng);// outputs 101.1234
offers.push([lat, lng]);
var marker = L.marker([lat, lng]).addTo(markers);
}
});
map.fitBounds(markers.getBounds());
}
console.log(offers);
}
drawResults();
Can anyone see what I'm doing wrong?
Edit: Console logs
35.0721909 app.js:350:13
-106.48798399999998 app.js:351:13
35.0526641 app.js:350:13
-78.87835849999999 app.js:351:13
Share
Improve this question
edited Jan 9, 2017 at 11:46
IvanSanchez
19.1k3 gold badges37 silver badges49 bronze badges
asked Jan 9, 2017 at 5:56
David J.David J.
1,91313 gold badges52 silver badges100 bronze badges
4
-
4
Why do you need
group
? Shouldn't it bemap.fitBounds(markers.getBounds())
?markers
itself is aFeatureGroup
. – cartant Commented Jan 9, 2017 at 6:06 - @cartant You're right -- that's how I originally had it and it was giving me the same error, so I decided to try something. Editing code to show without pointless extra featuregroup; I get the exact same error. – David J. Commented Jan 9, 2017 at 9:29
-
Your ments state that
lat
andlng
output particular values. How many points do you have? If more than one, are they coincident? What's returned bygetBounds
? – cartant Commented Jan 9, 2017 at 10:07 -
@cartant There are two points -- just added them to my question. I thought only lines could be coincident? I don't know what it means for points to be.
console.log(markers.getBounds())
doesn't print anything to the console... – David J. Commented Jan 9, 2017 at 10:24
1 Answer
Reset to default 4You will need to move the call to map.fitBounds
into the callback, as the once
method (which looks like a Firebase API call) is likely asynchronous:
offersRef.once("value", function(snapshot) {
var offerKeys = Object.keys(snapshot.val());
for (var i = 0; i < offerKeys.length; i++) {
var offer = snapshot.val()[offerKeys[i]];
var lat = offer.lat;
var lng = offer.lng;
offers.push([lat, lng]);
var marker = L.marker([lat, lng]).addTo(markers);
}
map.fitBounds(markers.getBounds());
});
If it's called outside the callback, there won't be any markers in the feature group and the group's bounds won't be valid.
本文标签: javascriptLeaflet map won39t fit boundsStack Overflow
版权声明:本文标题:javascript - Leaflet map won't fit bounds - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742316483a2451918.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论