-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
275 lines (232 loc) · 10.1 KB
/
index.html
File metadata and controls
275 lines (232 loc) · 10.1 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<meta name="apple-mobile-web-app-capable" content="yes">
<title>OpenLayers WMTS Example</title>
<script src="http://maps.google.com/maps/api/js?sensor=false&v=3.6"></script>
<script src="http://openlayers.org/dev/OpenLayers.js" type="text/javascript"></script>
<style>
.olControlAttribution
{
bottom: 5px;
}
.smallmap
{
width: 600px;
height: 600px;
border: 1px solid #ccc;
float: left;
}
</style>
<script type="text/javascript">
var map;
var mosques;
var vectorlayer;
function init() {
var googleSatellite = new OpenLayers.Layer.Google(
"Google Satellite",
{
type: google.maps.MapTypeId.SATELLITE,
sphericalMercator: true,
projection: "EPSG:900913",
units: 'm'
}
);
var googleRoad = new OpenLayers.Layer.Google(
"Google Road",
{
type: google.maps.MapTypeId.ROADMAP,
sphericalMercator: true,
projection: "EPSG:900913",
units: 'm'
}
);
var googleSatellite = new OpenLayers.Layer.Google(
"Google Satellite",
{
type: google.maps.MapTypeId.SATELLITE,
sphericalMercator: true,
projection: "EPSG:900913",
units: 'm'
}
);
vectorlayer = new OpenLayers.Layer.Vector('Vectorlayer', {
isBaseLayer: false,
styleMap: new OpenLayers.StyleMap({
"default": {
pointRadius: "${distance}",
strokeColor: "fuchsia",
strokeWidth: 2,
fillColor: "lime",
fillOpacity: 1,
rotation: "${rotation}",
backgroundGraphic: "http://www.openlayers.org/dev/img/marker.png",
backgroundHeight: "${distance}",
backgroundWidth: "${distance}",
externalGraphic: "${graphic}"
},
"select":{
fillColor: "#66ccff",
strokeColor: "#3399ff",
graphicZIndex: 2,
pointRadius: 20
},
"hover": {
fillColor: "#66ccff",
strokeColor: "#3399ff",
graphicZIndex: 2,
pointRadius: 20
}})
});
var select = new OpenLayers.Control.SelectFeature(vectorlayer, { hover: false });
mosques = new OpenLayers.Layer.Vector("Mosques", {
projection: new OpenLayers.Projection("EPSG:4326"),
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol.HTTP(
{
url: "http://www.ramotswa.com/showcase/portal/kml/mosques.kml",
format: new OpenLayers.Format.KML(
{
extractStyles: true,
extractAttributes: true
})
}),
visibility: true
});
map = new OpenLayers.Map({
controls: [new OpenLayers.Control.Navigation({ documentDrag: true })],
div: "map",
allOverlays: true,
eventListeners: {
"move": createMakers
}
});
map.addLayers([googleSatellite, mosques]);
map.zoomToMaxExtent();
map.addControl(new OpenLayers.Control.MousePosition({ "numDigits": 2 }));
map.addControl(select);
select.activate();
vectorlayer.events.on(
{
"featureselected": onFeatureSelect,
"featureunselected": onFeatureUnselect
});
}
var spherical_mercator = new OpenLayers.Projection("EPSG:900913");
var wgs84 = new OpenLayers.Projection("EPSG:4326");
function onFeatureSelect(event) {
var s = event;
var featCoord = new OpenLayers.LonLat(event.feature.attributes.iX, event.feature.attributes.iY);
map.panTo(featCoord);
}
function onFeatureUnselect(event) {
}
function createMakers() {
var offset = 20;
var divSize = map.size;
vectorlayer.removeAllFeatures();
var middleScreenlonlat = map.getCenter();
var middleScreenPoint = new OpenLayers.Geometry.Point(middleScreenlonlat.lon, middleScreenlonlat.lat);
var topLeftPx = map.getLonLatFromViewPortPx(new OpenLayers.Pixel(offset, offset));
var topRightPx = map.getLonLatFromViewPortPx(new OpenLayers.Pixel(divSize.w - offset, offset));
var bottomRightPx = map.getLonLatFromViewPortPx(new OpenLayers.Pixel(divSize.w - offset, divSize.h - offset));
var bottomLeftPx = map.getLonLatFromViewPortPx(new OpenLayers.Pixel(10, divSize.h - offset));
var topLeftPoint = new OpenLayers.Geometry.Point(topLeftPx.lon, topLeftPx.lat);
var topRightPoint = new OpenLayers.Geometry.Point(topRightPx.lon, topRightPx.lat);
var bottomRightPoint = new OpenLayers.Geometry.Point(bottomRightPx.lon, bottomRightPx.lat);
var bottomLeftPoint = new OpenLayers.Geometry.Point(bottomLeftPx.lon, bottomLeftPx.lat);
var screenBoundsArray = [topLeftPoint, topRightPoint, bottomRightPoint, bottomLeftPoint, topLeftPoint];
var screenBoundsLineString = new OpenLayers.Geometry.LineString(screenBoundsArray);
//var screenPolyFeat = new OpenLayers.Feature.Vector(screenBoundsLineString, null,null);
// vectorlayer.addFeatures([screenPolyFeat]);
for (var i = 0; i < mosques.features.length; i++) {
var currentFeature = mosques.features[i];
//only show the marker when the feature is not visible
if (map.getExtent().containsBounds(currentFeature.geometry.bounds)) {
//do nothing (yet)
} else {
var pointArray = [middleScreenPoint, currentFeature.geometry];
var linear_ring = new OpenLayers.Geometry.LineString(pointArray);
var wgs84middle = middleScreenlonlat.clone();
wgs84middle.transform(spherical_mercator, wgs84);
var wgs84current = currentFeature.clone();
wgs84current.geometry.transform(spherical_mercator, wgs84);
var angleInDegrees = getBearing(wgs84current.geometry.x, wgs84current.geometry.y, wgs84middle.lon, wgs84middle.lat);
var splitArray = linear_ring.splitWith(screenBoundsLineString);
var intersectPoint = splitArray[0].components[1].clone();
intersectPoint.transform(spherical_mercator, wgs84);
//get distance from marker on edge of screen rather than distance from centerpoint.
var distance = getDistance(intersectPoint.x, intersectPoint.y, wgs84current.geometry.x, wgs84current.geometry.y);
//document.getElementById('output').innerHTML = "Distance: " + distance + "</br>Bearing: " + angleInDegrees;
var iconSize;
if (distance < 10) { iconSize = 15; } else
if (distance < 20) { iconSize = 14; } else
if (distance < 40) { iconSize = 13; } else
if (distance < 60) { iconSize = 12; } else
if (distance < 80) { iconSize = 11; } else
if (distance < 100) { iconSize = 10; } else
if (distance < 150) { iconSize = 9; } else
if (distance < 200) { iconSize = 8; } else
if (distance > 200) { iconSize = 5; }
document.getElementById('output').innerHTML = "Distance: " + distance + "</br>Bearing: " + angleInDegrees + "</br>Size: "+ iconSize;
var markerPoint = new OpenLayers.Feature.Vector(splitArray[0].components[1], { iX: currentFeature.geometry.x, iY: currentFeature.geometry.y, rotation: angleInDegrees, distance: iconSize, bggraphicsize: iconSize + 5 }, null);
vectorlayer.addFeatures([markerPoint]);
}
}
map.addLayer(vectorlayer);
}
function getDistance(x1, y1, x2, y2) {
var DEG2RAD = 0.01745329252;
var RAD2DEG = 57.29577951308;
EARTH_RADIUS = 3958.75;
var distance = 0;
x1 = x1 * DEG2RAD;
y1 = y1 * DEG2RAD;
x2 = x2 * DEG2RAD;
y2 = y2 * DEG2RAD;
var a = Math.pow(Math.sin((y2 - y1) / 2.0), 2);
var b = Math.pow(Math.sin((x2 - x1) / 2.0), 2);
var c = Math.sqrt(a + Math.cos(y2) * Math.cos(y1) * b);
distance = 2 * Math.asin(c) * EARTH_RADIUS;
return distance;
}
function getBearing(x1, y1, x2, y2) {
var bearing = 0;
var DEG2RAD = 0.01745329252;
var RAD2DEG = 57.29577951308;
x1 = x1 * DEG2RAD;
y1 = y1 * DEG2RAD;
x2 = x2 * DEG2RAD;
y2 = y2 * DEG2RAD;
a = Math.cos(y2) * Math.sin(x2 - x1);
b = Math.cos(y1) * Math.sin(y2) - Math.sin(y1) * Math.cos(y2) * Math.cos(x2 - x1);
adjust = 0;
if ((a == 0) && (b == 0)) {
bearing = 0;
} else if (b == 0) {
if (a < 0)
bearing = 3 * Math.PI / 2;
else
bearing = Math.PI / 2;
} else if (b < 0)
adjust = Math.PI;
else {
if (a < 0)
adjust = 2 * Math.PI;
else
adjust = 0;
}
bearing = (Math.atan(a / b) + adjust) * RAD2DEG;
// document.getElementById('output').innerHTML = "A: " + a + "</br>B: " + b + "</br>Degrees: " + bearing;
return bearing;
}
</script>
</head>
<body onload="init();">
<div id="map" class="smallmap">
</div>
<div id="output"></div>
</body>
</html>