-
Notifications
You must be signed in to change notification settings - Fork 15
/
rackmap.js
127 lines (114 loc) · 3.87 KB
/
rackmap.js
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
var map, layer;
var options = {
projection: new OpenLayers.Projection("EPSG:900913"),
// EPSG 900913 = Spheroid Mercator (used by Google)
displayProjection: new OpenLayers.Projection("EPSG:4326"),
// EPSG 4326 = lat,lon
units: "m",
numZoomLevels: 20,
maxResolution: 156543.03390625,
maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34, 20037508.34, 20037508.34)
};
format = "image/png";
function loadMap(draggable) {
var draggable = (draggable == null) ? true : draggable;
map = new OpenLayers.Map('request-map', options);
var osm = new OpenLayers.Layer.WMS("OpenStreetMap", "http://maps.opengeo.org/geowebcache/service/wms", {
layers: "openstreetmap",
format: "image/png",
bgcolor: '#A1BDC4'
},
{
wrapDateLine: true
});
var address_options = {
projection: 'EPSG:4326',
styleMap: new OpenLayers.StyleMap({
"default": {
pointRadius: pointRadius,
externalGraphic: externalGraphic
}
})
};
var address_point = new OpenLayers.Layer.Vector("Location Marker", address_options);
var geometry = new OpenLayers.Geometry.fromWKT(WKT);
$("#location").val(WKT);
geometry.transform(map.displayProjection, map.projection);
var location = new OpenLayers.Feature.Vector(geometry);
address_point.addFeatures([location]);
map.addLayer(address_point);
var dropHandler = function (address_point, pixel) {
var xy = address_point.geometry.getBounds().getCenterLonLat();
xy.transform(map.projection, map.displayProjection);
getAddress(xy);
var location_wkt = "POINT(" + xy.lon + " " + xy.lat + ")";
$("#location").val(location_wkt);
xy.transform(map.displayProjection, map.projection);
map.setCenter(xy);
};
var point_control = new OpenLayers.Control.DragFeature(
address_point, {
onComplete: dropHandler
});
if(draggable) {
map.addControl(point_control);
point_control.activate();
}
function getAddress(lonlat) {
var lat = lonlat.lat;
var lon = lonlat.lon;
// Geocoding is asynchronous, so it might not complete
// before the form submits. Set flags in the form in case
// that happens, so the server can do the work after form submits.
$("#geocoded").val(0);
$.get("/reverse/", {
lat: lat,
lon: lon
},
function (data) {
$("#address").val(data).change();
$("#geocoded").val(1);
});
}
function getPointsFromAddress(address) {
$("#geocoded").val(0);
$.get("/geocode/", {
geocode_text: address
},
function (results) {
// FIXME: handle multiple (or zero) results.
var lon = results[0][1][1];
var lat = results[0][1][0];
var xy = new OpenLayers.LonLat(lon, lat);
var location_wkt = "POINT(" + lon.toString() + " " + lat.toString() + ")";
$("#location").val(location_wkt);
$("#geocoded").val(1);
address_point.destroyFeatures();
var geometry = new OpenLayers.Geometry.fromWKT(location_wkt);
geometry.transform(map.displayProjection, map.projection);
var location = new OpenLayers.Feature.Vector(geometry);
address_point.addFeatures([location]);
address_point.refresh();
xy.transform(map.displayProjection, map.projection);
map.setCenter(xy, 16);
},
'json');
}
// For users with JS, we only want to be forced to check on the back end if there's an unprocessed change
$("#geocoded").val(1);
$("#address").bind("blur", function(event) {
getPointsFromAddress($("#address").val());
});
$("#address").bind("focus change", function(event) {
// Be paranoid and assume we're going to reverse-geocode...
// For some reason, doing this on focus doesn't seem
// to be enough.
$("#geocoded").val(0);
});
var navControl = map.getControlsByClass('OpenLayers.Control.Navigation')[0];
if (navControl) {
navControl.disableZoomWheel();
}
map.addLayers([osm]);
post_loadmap(map, geometry);
}