diff --git a/bower.json b/bower.json index 8aa20b7d0850f18fa1adef0d324f4e96962c1ced..2f304dd2c17a3dc16e6319decb57b79e903f5d57 100644 --- a/bower.json +++ b/bower.json @@ -18,6 +18,7 @@ "jquery": "~2.2.3", "leaflet.markercluster": "~0.4.0", "leaflet-marker-rotate": "https://git.xhost.io/anton/leaflet-marker-rotate.git", - "Leaflet.label": "~0.2.1" + "Leaflet.label": "~0.2.1", + "Leaflet.GeometryUtil": "makinacorpus/Leaflet.GeometryUtil#^0.4.0" } } diff --git a/static/js/map.js b/static/js/map.js index 836785e7519d5c89366b02111583abc7a54fbfee..7694b32eb9a16fe085671f6903b74b3bf049e10d 100644 --- a/static/js/map.js +++ b/static/js/map.js @@ -486,15 +486,14 @@ BusMap.Map = function(opts) { // Go to zoom level and map center where vehicles are visible. function goToVehicles() { + if (Object.keys(that.vehicleMarkers).length == 0) return false; var zoom = BusMap.zoomShowVehicles; - var veh_keys = Object.keys(that.vehicleMarkers); - if (veh_keys.length > 0) { - var ll = that.vehicleMarkers[veh_keys[0]].getLatLng(); - } else { - var ll = that.leaflet.getCenter(); - } - console.log("setView("+ll+","+zoom+")"); - that.leaflet.setView(ll, zoom); + var center = that.leaflet.getCenter(); + // make vehicleMarkers an array because GeometryUtil can't handle obj/dict. + var vehicles = Object.keys(that.vehicleMarkers) + .map(function(k) { return that.vehicleMarkers[k]; }); + var goto_veh = L.GeometryUtil.closestLayer(that.leaflet, that.vehicleMarkers, center); + that.leaflet.setView(goto_veh.latlng, zoom); } init(); diff --git a/templates/map.html b/templates/map.html index 438d678bbe5519c6e9f864baca0d12055ef23e78..5805de2bf1bbcf571b99d52ca9d50ee4a82ea9fd 100644 --- a/templates/map.html +++ b/templates/map.html @@ -16,6 +16,7 @@ <script src="bower/Leaflet.label/dist/leaflet.label.js"></script> <script src="bower/leaflet.markercluster/dist/leaflet.markercluster.js"></script> <script src="bower/leaflet-marker-rotate/leaflet.marker.rotate.js"></script> + <script src="bower/Leaflet.GeometryUtil/dist/leaflet.geometryutil.js"></script> <script src="bower/jquery/dist/jquery.min.js"></script> <script src="static/js/map.js"></script> <script>