src/pyams_gis/resources/js/pyams_gis.js
changeset 16 5755c811bf11
parent 4 96f1ba4f8fc7
child 47 ee8602c0df78
--- a/src/pyams_gis/resources/js/pyams_gis.js	Fri Jan 26 15:56:21 2018 +0100
+++ b/src/pyams_gis/resources/js/pyams_gis.js	Fri Jan 26 15:57:57 2018 +0100
@@ -50,52 +50,53 @@
 								if (first_load) {
 									L = globals.L;
 									L.Control.Layers.prototype._addItem = PyAMS_GIS._layersControlAddItem;
-									MyAMS.getCSS('/--static--/pyams_gis/css/leaflet-1.0.3' + MyAMS.devext + '.css',
-												 'leaflet');
 								}
-								MyAMS.ajax.post('get-map-configuration.json', {}, function(config) {
-									var data = map.data();
-									var settings = {
-										preferCanvas: data.mapLeafletPreferCanvas || false,
-										attributionControl: data.mapLeafletAttributionControl === undefined ?
-											config.attributionControl :
-											data.mapLeafletAttributionControl,
-										zoomControl: data.mapLeafletZoomControl === undefined ?
-											config.zoomControl :
-											data.mapLeafletZoomControl,
-										crs: data.mapLeafletCrs || MyAMS.getObject(config.crs) || globals.L.CRS.EPSG3857,
-										center: data.mapLeafletCenter || config.center,
-										zoom: data.mapLeafletZoom || config.zoom
-									};
-									settings = $.extend({}, settings, options);
-									map.trigger('map.init', [map, settings, config]);
-									var leafmap = L.map(map.attr('id'), settings);
-									if (config.layers) {
-										for (var index = 0; index < config.layers.length; index++) {
-											var layerConfig = config.layers[index];
-											map.trigger('map.layer.init', [map, layerConfig]);
-											PyAMS_GIS.getLayer(layerConfig).addTo(leafmap);
+								MyAMS.getCSS('/--static--/pyams_gis/css/leaflet-1.0.3' + MyAMS.devext + '.css',
+											 'leaflet', function() {
+									MyAMS.ajax.post('get-map-configuration.json', {}, function(config) {
+										var data = map.data();
+										var settings = {
+											preferCanvas: data.mapLeafletPreferCanvas || false,
+											attributionControl: data.mapLeafletAttributionControl === undefined ?
+												config.attributionControl :
+												data.mapLeafletAttributionControl,
+											zoomControl: data.mapLeafletZoomControl === undefined ?
+												config.zoomControl :
+												data.mapLeafletZoomControl,
+											crs: data.mapLeafletCrs || MyAMS.getObject(config.crs) || globals.L.CRS.EPSG3857,
+											center: data.mapLeafletCenter || config.center,
+											zoom: data.mapLeafletZoom || config.zoom
+										};
+										settings = $.extend({}, settings, options);
+										map.trigger('map.init', [map, settings, config]);
+										var leafmap = L.map(map.attr('id'), settings);
+										if (config.layers) {
+											for (var index = 0; index < config.layers.length; index++) {
+												var layerConfig = config.layers[index];
+												map.trigger('map.layer.init', [map, layerConfig]);
+												PyAMS_GIS.getLayer(layerConfig).addTo(leafmap);
+											}
+										} else {
+											L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
+												maxZoom: 19,
+												id: 'osm',
+												attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
+											}).addTo(leafmap);
 										}
-									} else {
-										L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
-											maxZoom: 19,
-											id: 'osm',
-											attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
-										}).addTo(leafmap);
-									}
-									if (config.zoomControl && (data.mapLeafletHideZoomControl !== true)) {
-										L.control.scale().addTo(leafmap);
-									}
-									if (config.bounds) {
-										leafmap.fitBounds(config.bounds);
-									}
-									map.data('leafmap', leafmap);
-									map.data('leafmap.config', config);
-									map.trigger('map.finishing', [map, leafmap]);
-									if (callback) {
-										callback(leafmap, config);
-									}
-									map.trigger('map.finished', [map, leafmap]);
+										if (config.zoomControl && (data.mapLeafletHideZoomControl !== true)) {
+											L.control.scale().addTo(leafmap);
+										}
+										if (config.bounds) {
+											leafmap.fitBounds(config.bounds);
+										}
+										map.data('leafmap', leafmap);
+										map.data('leafmap.config', config);
+										map.trigger('map.finishing', [map, leafmap]);
+										if (callback) {
+											callback(leafmap, config);
+										}
+										map.trigger('map.finished', [map, leafmap]);
+									});
 								});
 							 });
 		},
@@ -203,8 +204,8 @@
 									longitude: parseFloat(longitude.val()),
 									latitude: parseFloat(latitude.val())
 								},
-								from_projection: projection.val() || PyAMS_GIS.WGS_SRID,
-								to_projection: PyAMS_GIS.WGS_SRID
+								from_srid: projection.val() || PyAMS_GIS.WGS_SRID,
+								to_srid: PyAMS_GIS.WGS_SRID
 							};
 							PyAMS_GIS.callJSON('transformPoint', params, function(result) {
 								if (!result.error) {
@@ -240,14 +241,15 @@
 						longitude: latlng.lng,
 						latitude: latlng.lat
 					},
-					from_projection: PyAMS_GIS.WGS_SRID,
-					to_projection: projection.val()
+					from_srid: PyAMS_GIS.WGS_SRID,
+					to_srid: projection.val()
 				};
 				PyAMS_GIS.callJSON('transformPoint', params, function(result) {
 					if (!result.error) {
 						var point = result.result.point;
 						$('input[name="' + fieldname + '.widgets.longitude"]').val(point.longitude);
 						$('input[name="' + fieldname + '.widgets.latitude"]').val(point.latitude);
+						$(map).trigger('marker.changed', [map, point]);
 					}
 				});
 			},
@@ -268,8 +270,8 @@
 							longitude: parseFloat(longitude.val()),
 							latitude: parseFloat(latitude.val())
 						},
-						from_projection: projection.val(),
-						to_projection: 4326
+						from_srid: projection.val(),
+						to_srid: PyAMS_GIS.WGS_SRID
 					};
 					PyAMS_GIS.callJSON('transformPoint', params, function (result) {
 						if (!result.error) {
@@ -300,8 +302,8 @@
 									longitude: parseFloat(longitude.val()),
 									latitude: parseFloat(latitude.val())
 								},
-								from_projection: previous,
-								to_projection: current
+								from_srid: previous,
+								to_srid: current
 							};
 							PyAMS_GIS.callJSON('transformPoint', params, function (result) {
 								if (!result.error) {
@@ -320,6 +322,33 @@
 			clear: function(event) {
 				var fieldset = $(this).parents('fieldset:first');
 				$('input', fieldset).val(null);
+			},
+
+			moveMarkerTo: function(map, position, srid) {
+				// Update fields
+				var fieldname = map.data('map-leaflet-fieldname');
+				$('input[name="' + fieldname + '.widgets.longitude"]').val(position.lon);
+				$('input[name="' + fieldname + '.widgets.latitude"]').val(position.lat);
+				// Get map coordinates
+				var params = {
+					point: {
+						longitude: position.lon,
+						latitude: position.lat
+					},
+					from_srid: srid,
+					to_srid: 4326
+				};
+				PyAMS_GIS.callJSON('transformPoint', params, function(result) {
+					if (!result.error) {
+						var point = result.result.point;
+						var marker = map.data('marker');
+						marker.setLatLng({
+							lon: point.longitude,
+							lat: point.latitude
+						});
+						map.data('leafmap').setView(position);
+					}
+				});
 			}
 		},
 
@@ -377,8 +406,8 @@
 																			x2: parseFloat(x2.val()),
 																			y2: parseFloat(y2.val())
 																		},
-																		from_projection: projection.val(),
-																		to_projection: PyAMS_GIS.WGS_SRID
+																		from_srid: projection.val(),
+																		to_srid: PyAMS_GIS.WGS_SRID
 																	};
 																	PyAMS_GIS.callJSON('transformArea', params, function(result) {
 																		if (!result.error) {
@@ -423,8 +452,8 @@
 								x2: area.getEast(),
 								y2: area.getNorth()
 							},
-							from_projection: PyAMS_GIS.WGS_SRID,
-							to_projection: projection
+							from_srid: PyAMS_GIS.WGS_SRID,
+							to_srid: projection
 						};
 						PyAMS_GIS.callJSON('transformArea', params, function(result) {
 							if (!result.error) {
@@ -459,8 +488,8 @@
 							x2: parseFloat(x2.val()),
 							y2: parseFloat(y2.val())
 						},
-						from_projection: projection.val(),
-						to_projection: 4326
+						from_srid: projection.val(),
+						to_srid: 4326
 					};
 					PyAMS_GIS.callJSON('transformArea', params, function (result) {
 						if (!result.error) {
@@ -495,8 +524,8 @@
 									x2: parseFloat(x2.val()),
 									y2: parseFloat(y2.val())
 								},
-								from_projection: previous,
-								to_projection: current
+								from_srid: previous,
+								to_srid: current
 							};
 							PyAMS_GIS.callJSON('transformArea', params, function (result) {
 								if (!result.error) {