-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathol2_random_within_extent.html
109 lines (103 loc) · 3.35 KB
/
ol2_random_within_extent.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!DOCTYPE html>
<html>
<head>
<title>points points and points</title>
<link rel="stylesheet" href="/stylesheets/ol_default.css" type="text/css" />
<link rel="stylesheet" href="/stylesheets/ol_fullscreen.css" type="text/css" />
<style type="text/css">
html, body, #map {
margin: 0;
width: 100%;
height: 100%;
}
#text {
position: absolute;
bottom: 1em;
left: 1em;
width: 512px;
z-index: 20000;
background-color: white;
padding: 0 0.5em 0.5em 0.5em;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script src="http://openlayers.org/api/OpenLayers.js"></script>
<script>
var map;
function init() {
// create the map
map = new OpenLayers.Map('map');
var ol_wms = new OpenLayers.Layer.WMS(
'OpenLayers WMS',
'http://vmap0.tiles.osgeo.org/wms/vmap0',
{layers: 'basic'}
);
var ol_wms_nobuffer = new OpenLayers.Layer.WMS(
'OpenLayers WMS (no tile buffer)',
'http://vmap0.tiles.osgeo.org/wms/vmap0',
{layers: 'basic'}, {buffer: 0}
);
var pts = new OpenLayers.Layer.Vector('Points');
map.addLayers([ol_wms, ol_wms_nobuffer, pts]);
map.addControl(new OpenLayers.Control.LayerSwitcher());
map.setCenter(new OpenLayers.LonLat(-100, 40), 4);
pts.addFeatures([
new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(-100, 40),
null,
null
)
]);
// re-draw points
$('#draw').click(redraw);
// clear graphics
$('#clear').click(clear);
}
function redraw() {
// get new number of points, the point layer and the map extent
// also declare a new array for our new points
var num = $('#num').attr('value'),
ptlyr = map.getLayersBy('name', 'Points')[0],
ext = map.getExtent(),
newpts = [];
// calc map width and height
var width = ext.left - ext.right,
height = ext.top - ext.bottom;
console.log(width, height);
// generate "random" points within the map's extent
for (var i = 0; i < num; i++) {
var randx = Math.random(),
randy = Math.random();
var x = (randx * width) + ext.right,
y = (randy * height) + ext.bottom;
newpts[i] = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(x, y), null, null
);
}
ptlyr.removeAllFeatures();
ptlyr.addFeatures(newpts);
}
function clear() {
map.getLayersBy('name', 'Points')[0].removeAllFeatures();
}
</script>
</head>
<body onload="init()">
<div id="map"></div>
<div id="text">
<h1 id="title">How Many Points...</h1>
<div id="docs">
<p>
Number of points to place on the map:
<input id="num" type="text" value="1000" />
<button id="draw">Draw</button>
<button id="clear">Clear</button>
<p>
This page is a tweaked version the
<a href="http://openlayers.org/dev/examples/fullScreen.html">full screen</a>
sample.
</p>
</div>
</div>
</body>
</html>