Set marker coordinates as map center when map location fields has valid coordinates

When a user recovers a page from browser history where placed a
marker in different map pane (visible map layer) marker was
successfully added to the map but the map center is the one
defined at Settings map properties so the marker was not visible
to the user.

Now when map_location form has valid coordinates we use them
instead of default map center settings. This will avoid the user to
have to rellocate the marker (or find the correct pane where the
marker was added) if already placed.
This commit is contained in:
Senén Rodero Rodríguez
2020-08-11 13:02:32 +02:00
parent 6aa94a787c
commit b9ce68bc82
2 changed files with 55 additions and 6 deletions

View File

@@ -20,14 +20,12 @@
App.Map.maps = []; App.Map.maps = [];
}, },
initializeMap: function(element) { initializeMap: function(element) {
var addMarkerInvestments, clearFormfields, createMarker, editable, formCoordinates, getPopupContent, var addMarkerInvestments, clearFormfields, createMarker, dataCoordinates, editable, formCoordinates,
latitudeInputSelector, longitudeInputSelector, map, mapAttribution, mapCenterLatLng, getPopupContent, latitudeInputSelector, longitudeInputSelector, map, mapAttribution, mapCenterLatLng,
mapCenterLatitude, mapCenterLongitude, mapTilesProvider, marker, markerIcon, markerLatitude, mapCenterLatitude, mapCenterLongitude, mapTilesProvider, marker, markerIcon, markerLatitude,
markerLongitude, moveOrPlaceMarker, openMarkerPopup, removeMarker, removeMarkerSelector, markerLongitude, moveOrPlaceMarker, openMarkerPopup, removeMarker, removeMarkerSelector,
updateFormfields, zoom, zoomInputSelector; updateFormfields, zoom, zoomInputSelector;
App.Map.cleanInvestmentCoordinates(element); App.Map.cleanInvestmentCoordinates(element);
mapCenterLatitude = $(element).data("map-center-latitude");
mapCenterLongitude = $(element).data("map-center-longitude");
mapTilesProvider = $(element).data("map-tiles-provider"); mapTilesProvider = $(element).data("map-tiles-provider");
mapAttribution = $(element).data("map-tiles-provider-attribution"); mapAttribution = $(element).data("map-tiles-provider-attribution");
latitudeInputSelector = $(element).data("latitude-input-selector"); latitudeInputSelector = $(element).data("latitude-input-selector");
@@ -38,12 +36,23 @@
long: $(longitudeInputSelector).val(), long: $(longitudeInputSelector).val(),
zoom: $(zoomInputSelector).val() zoom: $(zoomInputSelector).val()
}; };
dataCoordinates = {
lat: $(element).data("marker-latitude"),
long: $(element).data("marker-longitude")
};
if (App.Map.validCoordinates(formCoordinates)) { if (App.Map.validCoordinates(formCoordinates)) {
markerLatitude = formCoordinates.lat; markerLatitude = formCoordinates.lat;
markerLongitude = formCoordinates.long; markerLongitude = formCoordinates.long;
mapCenterLatitude = formCoordinates.lat;
mapCenterLongitude = formCoordinates.long;
} else if (App.Map.validCoordinates(dataCoordinates)) {
markerLatitude = dataCoordinates.lat;
markerLongitude = dataCoordinates.long;
mapCenterLatitude = dataCoordinates.lat;
mapCenterLongitude = dataCoordinates.lat;
} else { } else {
markerLatitude = $(element).data("marker-latitude"); mapCenterLatitude = $(element).data("map-center-latitude");
markerLongitude = $(element).data("marker-longitude"); mapCenterLongitude = $(element).data("map-center-longitude");
} }
if (App.Map.validZoom(formCoordinates.zoom)) { if (App.Map.validZoom(formCoordinates.zoom)) {
zoom = formCoordinates.zoom; zoom = formCoordinates.zoom;

View File

@@ -134,6 +134,38 @@ shared_examples "mappable" do |mappable_factory_name, mappable_association_name,
expect(page.execute_script("return App.Map.maps[0].getZoom();")).to eq(11) expect(page.execute_script("return App.Map.maps[0].getZoom();")).to eq(11)
end end
end end
scenario "shows marker at map center", :js do
do_login_for user
visit send(mappable_new_path, arguments)
within ".map_location" do
expect(page).not_to have_css(".map-icon")
end
place_map_at(-68.592487, -62.391357)
find("#new_map_location").click
within ".map_location" do
expect(page).to have_css(".map-icon")
end
if management
click_link "Select user"
expect(page).to have_content "User management"
else
click_link "Help"
expect(page).to have_content "CONSUL is a platform for citizen participation"
end
go_back
within ".map_location" do
expect(page).to have_css(".map-icon")
end
end
end end
scenario "Skip map", :js do scenario "Skip map", :js do
@@ -335,3 +367,11 @@ def map_zoom_in
sleep 0.01 sleep 0.01
end end
end end
def place_map_at(latitude, longitude)
page.execute_script("App.Map.maps[0].setView(new L.LatLng(#{latitude}, #{longitude}))")
until page.execute_script("return App.Map.maps[0].getCenter().lat === #{latitude};") do
sleep 0.01
end
end