-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
144 lines (119 loc) · 4.77 KB
/
index.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
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" debug="true">
<head>
<title>Get Lat/Long</title>
<style type="text/css">
.map {
width: 800px;
height: 400px;
border: 1px solid #ccc;
}
#head {
background-color:#aaaaFF;
padding: 10px;
line-height: 1.2em;
}
#left-col {
position:relative;
float:left;
padding-top:5px;
padding-left:5px;
}
#left-col {
position: relative;
float: right;
}
</style>
<script src="http://openlayers.org/api/OpenLayers.js"></script>
<script src="http://www.openstreetmap.org/openlayers/OpenStreetMap.js"></script>
<script type="text/javascript">
var dms = false;
var map;
function init(){
var map = new OpenLayers.Map('map',
{ maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
numZoomLevels: 19,
maxResolution: 156543.0399,
units: 'm',
projection: new OpenLayers.Projection("EPSG:900913"),
displayProjection: new OpenLayers.Projection("EPSG:4326")
});
map.addControl(new OpenLayers.Control.MousePosition());
/* OpenLayers Map Setup - I've commented out the additional layersl
only WMS is used - Brian */
var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
"http://labs.metacarta.com/wms/vmap0",
{layers: 'basic'} );
var lat = 18.32;
var lon = -72.20;
var zoom = 10;
var markers;
var layerMapnik = new OpenLayers.Layer.OSM.Mapnik("Mapnik");
var layerTah = new OpenLayers.Layer.OSM.Osmarender("Tiles@Home");
map.addLayers([layerMapnik,layerTah,wms]);
map.addControl(new OpenLayers.Control.LayerSwitcher());
var lonLat = new OpenLayers.LonLat(lon, lat).transform(map.displayProjection, map.projection);
if (!map.getCenter()) map.setCenter (lonLat, zoom);
markers = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer(markers);
if (!map.getCenter()) map.zoomToMaxExtent();
map.events.register('click', map, handleMapClick);
function handleMapClick(evt) {
var mc = map.getLonLatFromViewPortPx(new
OpenLayers.Pixel(evt.xy.x , evt.xy.y) );
OpenLayers.Util.getElement("coords").innerHTML = getFormattedCoord(mc);
addMarker(mc);
}
function getFormattedCoord(latlng){
//console.log(latlng);
latlng= latlng.transform(map.projection, map.displayProjection);
var lat = latlng.lat;
var lon = latlng.lon;
/*
var mypoint = OpenLayers.Projection.transform = function( latlng,
source,
dest )
*/
if(dms){
lat = getDMS(lat);
lon = getDMS(lon);
}
return "Latitude : " + lat + "<br/>" + "Longitude : " + lon;
}
function getDMS(dd){
var absDD = Math.abs(dd);
var deg = Math.floor(absDD);
var min = Math.floor((absDD-deg)*60);
var sec = (Math.round((((absDD - deg) - (min/60)) * 60 * 60) * 100) / 100 ) ;
if(dd < 0) deg = -deg;
//return {"deg":deg, "min":min, "sec":sec};
return deg + " " + min + "' " + sec + "''";
}
function addMarker(latlng){
markers.clearMarkers();
latlng= latlng.transform(map.displayProjection, map.projection);
var lat = latlng.lat;
var lon = latlng.lon;
var size = new OpenLayers.Size(40,40);
//var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
//var icon = new OpenLayers.Icon('cursor.png',size,offset);
var icon = new OpenLayers.Icon('cursor.png',size); //,offset);
markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(lon,lat),icon));
map.addLayer(markers);
}
}
</script>
</head>
<body onload="init()">
<div id="head">
<h2 id="title">Find your Current Lat & Long, provided by Open Street Map</h2>
</div>
<div id="left-col">
<div id="map" class="map"></div>
</div>
<div id="right-col"><h3>Click on the map to get lat/long position</h3>
<div id="coords">Latitude:<br/>Longitude:</div>
</div>
</body>
</html>