From 6b6738532deee6bb8cc88af735a774884fe443e0 Mon Sep 17 00:00:00 2001 From: Harry Cutts Date: Sat, 2 Feb 2013 11:00:06 +0000 Subject: Tidy up map.js and op2geojson.js --- resources/map.js | 64 +++++------------ resources/op2geojson.js | 178 ++++++++++++++++++++++++------------------------ 2 files changed, 105 insertions(+), 137 deletions(-) diff --git a/resources/map.js b/resources/map.js index a2c16f5..2dbf8e4 100644 --- a/resources/map.js +++ b/resources/map.js @@ -1,13 +1,11 @@ $(document).ready(function() { - var self = this; + var self = this; // the HTMLDocument - self.hospitalTemplate = _.template('\ -\ -\ + self.popupTemplate = _.template('\ +\ \ -\ -\ +\

Hospital

\ \ \ @@ -20,31 +18,14 @@ $(document).ready(function() { \ \ \ -
Name<%= properties["name"] %>
Population
Furthest Village from health structure
Average distance of all villages from health structure
'); - - self.catchmentTemplate = _.template('\ -\ -<% _.each(properties, function(val, key) { %> \ -<% if (/\:/.exec(key)) { %> \ - \ - \ - \ - \ -<% } else {%> \ -\ - \ - \ -\ -<% } %> \ -<% }); %> \
KeyValue
<%= key.split(":")[1] %> <%= val %>
<%= key %> <%= val %>
'); var hospitalIcon = L.icon({ iconUrl: 'resources/img/hospital.png', - iconSize: [18, 18], // size of the icon - iconAnchor: [9, 9], // point of the icon which will correspond to marker's location - popupAnchor: [2, -9] // point from which the popup should open relative to the iconAnchor + iconSize: [18, 18], + iconAnchor: [9, 9], // point on the icon corresponding to marker's location + popupAnchor: [2, -9] // point to open the popup from relative to iconAnchor }); self.tileLayer = L.tileLayer('http://{s}.www.toolserver.org/tiles/osm-no-labels/{z}/{x}/{y}.png', { @@ -57,7 +38,6 @@ $(document).ready(function() { layers: [self.tileLayer], }).setView([12.4822, -11.9463], 11); // TODO: Not sure why the above call to setView is needed - GlobalMap = map; self.amenities = ["hospital", "doctors", "dentist"]; // This contains the layers for each of the above amenities @@ -79,9 +59,7 @@ $(document).ready(function() { var legend = L.control({position: 'bottomright'}); legend.onAdd = function (map) { var div = L.DomUtil.create('div', 'info legend'); - div.innerHTML += ' Hospital
'; - return div; }; legend.addTo(map); @@ -92,14 +70,18 @@ $(document).ready(function() { return; } + // Make the bounding box string var bounds = map.getBounds(); var sw = bounds.getSouthWest(); var ne = bounds.getNorthEast(); bbox = [sw.lat, sw.lng, ne.lat, ne.lng].join(','); + + // Get the data var data = createQueryData(bbox); + + // Convert the data to GeoJSON converter = new op2geojson(); converter.fetch("http://overpass-api.de/api/interpreter", data, zoom, function(data) { - if (jQuery.isEmptyObject(self.amenityLayers)) { var markers = {}; @@ -120,7 +102,7 @@ $(document).ready(function() { center = feature.geometry.coordinates[0]; } - layer.bindPopup(self.hospitalTemplate({ properties: feature.properties, coordinate: center })); + layer.bindPopup(self.popupTemplate({ properties: feature.properties, coordinate: center })); markers[feature.id] = layer; }, @@ -130,7 +112,8 @@ $(document).ready(function() { }); map.addLayer(self.amenityLayers[amenity]); - self.layersControl.addOverlay(self.amenityLayers[amenity], self.amenities[i].charAt(0).toUpperCase() + self.amenities[i].slice(1)); + self.layersControl.addOverlay(self.amenityLayers[amenity], + self.amenities[i].charAt(0).toUpperCase() + self.amenities[i].slice(1)); }); // Now deal with the catchment areas @@ -144,8 +127,6 @@ $(document).ready(function() { fillOpacity: 0.1}; }, onEachFeature: function(feature, layer) { - var marker_layer = markers[feature.properties.subject]; - var popup = markers[feature.properties.subject].openPopup; layer.on({ mouseover: highlightFeature, mouseout: resetHighlight, @@ -190,7 +171,6 @@ $(document).ready(function() { } function resetHighlight(e) { - var geojsonLayer = self.catchmentAreaLayer; self.catchmentAreaLayer.resetStyle(e.target); } @@ -198,25 +178,13 @@ $(document).ready(function() { self.currentLocation = e.latlng; var radius = e.accuracy / 2; L.marker(e.latlng).addTo(map) - .bindPopup("You are within " + radius + " meters from this point").openPopup(); + .bindPopup("You are within " + radius + " meters of this point.").openPopup(); } function onLocationError(e) { alert(e.message); } - function polygonArea(X, Y, numPoints) - { - area = 0; // Accumulates area in the loop - j = numPoints-1; // The last vertex is the 'previous' one to the first - - for (i=0; i