Project

General

Profile

« Previous | Next » 

Revision 5c5b7504

ID5c5b75049200d1e4d97408381f1c5fd4f713ee39
Parent b2945655
Child af81ae55

Added by Jocelyn Delande about 9 years ago

Added a display of all the ref_points on the map.

Files

  • added
  • modified
  • copied
  • renamed
  • deleted

View differences

Changesets

ajax/ref_points.php
1
<?php
2

  
3

  
4
/**
5
 *  An example CORS-compliant method.  It will allow any GET, POST, or OPTIONS requests from any
6
 *  origin.
7
 *
8
 *  In a production environment, you probably want to be more restrictive, but this gives you
9
 *  the general idea of what is involved.  For the nitty-gritty low-down, read:
10
 *
11
 *  - https://developer.mozilla.org/en/HTTP_access_control
12
 *  - http://www.w3.org/TR/cors/
13
 *
14
 */
15
function cors() {
16
    // Allow from any origin
17
    if (isset($_SERVER['HTTP_ORIGIN'])) {
18
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
19
        header('Access-Control-Allow-Credentials: true');
20
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
21
    }
22

  
23
    // Access-Control headers are received during OPTIONS requests
24
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
25

  
26
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
27
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
28

  
29
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
30
            header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
31

  
32
        exit(0);
33
    }
34
}
35

  
36
cors();
37

  
38
function ref_point_to_geoJSONFeature($name, $values) {
39
	return array("type" => "Feature",
40
	             "geometry" => array(
41
	                                 "type" => "Point",
42
	                                 "coordinates" => [$values[1],$values[0]]
43
	                                 ),
44
	             "properties" => array("name" => $name)
45
	             );
46
}
47

  
48
function get_ref_points() {
49
	$ref_points_filename = '../ref_points.local.php';
50
	if (file_exists($ref_points_filename)) {
51
		include $ref_points_filename;
52
		return $ref_points;
53
	} else {
54
		return array();
55
	}
56
}
57

  
58
$json = array(
59
              "type" => "FeatureCollection",
60
              "features"=> array()
61
              );
62

  
63
foreach (get_ref_points() as $name => $vals) {
64
	$json['features'][] = ref_point_to_geoJSONFeature($name, $vals);
65
}
66

  
67
echo json_encode($json);
68

  
69
?>
js/utils_osm.js
225 225
	    alert(position.lat.toFixed(5) + ', ' + position.lon.toFixed(5));
226 226
	});
227 227
    }
228

  
229
	return map;
230
}
231

  
232
function mk_all_refpoints_layer() {
233
	var layer = new OpenLayers.Layer.Vector(
234
		"Reference points",{
235
			projection: new OpenLayers.Projection("EPSG:4326"),
236
			strategies: [new OpenLayers.Strategy.Fixed()],
237
			protocol: new OpenLayers.Protocol.HTTP({
238
				url: 'ajax/ref_points.php',
239
				format: new OpenLayers.Format.GeoJSON(),
240
			})
241
		});
242
	return layer;
228 243
}
244

  
245

  
246
function add_refpoint_control(layer, map) {
247
	var selectControl ;
248
	selectControl = new OpenLayers.Control.SelectFeature(
249
		layer,{
250
			onSelect:function(feature) {
251
				var popup = new OpenLayers.Popup.FramedCloud(
252
					feature.attributes.name,
253
					feature.geometry.getBounds().getCenterLonLat(),
254
					null,
255
					"<div>" + feature.attributes.name+"</div>",
256
					null, true, function() {selectControl.unselect(feature);});
257
				feature.popup = popup;
258
				map.addPopup(popup);},
259

  
260
			onUnselect:function(feature) {
261
				map.removePopup(feature.popup);
262
				feature.popup.destroy();
263
				feature.popup = null;
264
			}});
265

  
266
	map.addControl(selectControl);
267
	selectControl.activate();
268
}
269

  
show_capline.php
86 86
}
87 87
?>
88 88
<script>
89
window.onload = draw_cap_map;
89
window.onload = function() {
90
  var map = draw_cap_map();
91
  all_refpoints = mk_all_refpoints_layer()
92
  map.addLayer(all_refpoints);
93
  add_refpoint_control(all_refpoints, map);
94
}
90 95
</script>
91 96
</head>
92 97
<body>