diff options
author | Dave Arter <davea@mysociety.org> | 2018-02-12 09:52:46 +0000 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2018-02-22 11:30:24 +0000 |
commit | 01f7448201d11139ae3a31745b7f26965c7282e7 (patch) | |
tree | c26749c8e1c1f733e4d40d2ddc15d8d70a78a778 /web/js/map-OpenLayers.js | |
parent | 9c8493a3b785d2cda969533c76ac6cca26f3ca00 (diff) |
Populate `usrn` field with USRN of clicked asset, if available
Some cobrands require reports to include the USRN of the clicked
road. This commit allows an asset layer to be added and designated
as a 'USRN provider' by setting its `usrn_field` property when calling
fixmystreet.assets.add.
Initially this feature used OpenLayers' getFeatureFromEvent method,
however that doesn't work if the layer isn't topmost. This is because it
uses the clicked element in the DOM to determine which feature was
clicked. This doesn't work if the layer you're trying to get the feature
from wasn't actually the DOM element that was clicked.
Instead, we add a new method, OpenLayers.Layer.Vector.getFeatureAtPoint
method which takes a Point object and iterates through the features'
geometries to find the matching point.
To make things a little more user-friendly, if an asset isn't clicked
directly we find the closest to the clicked point and use that for the
USRN. To accomplish this, this commit factors out the ‘select nearest
asset’ code into a new method OpenLayers.Layer.Vector.getNearestFeature,
which takes a Point and a distance threshold and finds the nearest
feature.
Diffstat (limited to 'web/js/map-OpenLayers.js')
-rw-r--r-- | web/js/map-OpenLayers.js | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/web/js/map-OpenLayers.js b/web/js/map-OpenLayers.js index 0f6cca2b5..5ebb9a18e 100644 --- a/web/js/map-OpenLayers.js +++ b/web/js/map-OpenLayers.js @@ -40,19 +40,32 @@ $.extend(fixmystreet.utils, { }; $.extend(fixmystreet.maps, { - // This function might be passed either an OpenLayers.LonLat (so has - // lon and lat), or an OpenLayers.Geometry.Point (so has x and y). update_pin: function(lonlat) { + // This function might be passed either an OpenLayers.LonLat (so has + // lon and lat), or an OpenLayers.Geometry.Point (so has x and y). + if (lonlat.x !== undefined && lonlat.y !== undefined) { + // It's a Point, convert to a LatLon + lonlat = new OpenLayers.LonLat(lonlat.x, lonlat.y); + } + var transformedLonlat = lonlat.clone().transform( fixmystreet.map.getProjectionObject(), new OpenLayers.Projection("EPSG:4326") ); - var lat = transformedLonlat.lat || transformedLonlat.y; - var lon = transformedLonlat.lon || transformedLonlat.x; + var lat = transformedLonlat.lat; + var lon = transformedLonlat.lon; document.getElementById('fixmystreet.latitude').value = lat; document.getElementById('fixmystreet.longitude').value = lon; + + // This tight coupling isn't ideal. A better solution would be for the + // asset code to register an event handler somewhere, but the correct + // place isn't apparent. + if (fixmystreet.assets) { + fixmystreet.assets.select_usrn(lonlat); + } + return { 'url': { 'lon': lon, 'lat': lat }, 'state': { 'lon': lonlat.lon, 'lat': lonlat.lat } |