-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
176 lines (156 loc) · 6.34 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Directions and POI Buffer</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox.js/v2.1.5/mapbox.js'></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="//api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.0/turf.min.js"></script>
<script src='osmtogeojson.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox.js/v2.1.5/mapbox.css' rel='stylesheet' />
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: fixed;
top: 0;
bottom: 0;
width: 100%;
z-index: 0;
}
</style>
</head>
<body>
<div id='map'></div>
<script>
L.mapbox.accessToken = 'pk.eyJ1Ijoic3JpdmlkeWEiLCJhIjoicXo2NTJKayJ9.m1P57SCwkp8_7N3TgYsVRg';
var map = L.mapbox.map('map', 'mapbox.high-contrast')
// .addControl(L.mapbox.geocoderControl('mapbox.places'));
.setView([12.9861, 77.5930], 13);
map.on('click', function (e) {
makeMarker(e, drawRoute);
});
var waypoints = [];
var polyline = L.polyline([]).addTo(map);
function makeMarker(e, done) {
var marker = L.marker(e.latlng, {
draggable: true
}).addTo(map);
marker.on('dragend', drawRoute);
waypoints.push(marker);
return done();
}
var routeLayer = L.mapbox.featureLayer().addTo(map);
var poiLayer = L.mapbox.featureLayer().addTo(map);
function drawRoute() {
if (waypoints.length < 2) return;
// Directions API request looks like
// http://api.tiles.mapbox.com/v4/directions/mapbox.driving/
// latlong.json?access_token={access_token}
var points = waypoints.map(function (marker) {
var latlng = marker._latlng;
return [latlng.lng, latlng.lat].join(',');
}).join(';');
var directionsUrl = 'https://api.tiles.mapbox.com/v4/directions/mapbox.driving/' +
points + '.json?access_token=' + L.mapbox.accessToken;
$.get(directionsUrl, function (data) {
//console.log(data)
routeLayer.setGeoJSON(data.routes[0].geometry);
//console.log(data)
routeLayer.setStyle({
color: 'black',
//weight: 3
});
map.fitBounds(routeLayer.getBounds());
calculateBuffer();
});
}
function calculateBuffer() {
var routes = turf.featurecollection(routeLayer.getLayers().map(function (f) {
return f.toGeoJSON();
}));
var buffer = turf.buffer(routes, 200, 'meters');
// console.log(buffer);
routeLayer.setGeoJSON(buffer);
routeLayer
.setGeoJSON(buffer)
.setStyle({
stroke: true,
color: "#B2B2B2",
fillColor: '#400000',
fillOpacity: 0.4
})
var bbox = routeLayer.getBounds().toBBoxString().split(',');
var overpassBbox = bbox[1] + ',' + bbox[0] + ',' + bbox[3] + ',' + bbox[2];
//console.log(overpassBbox);
var url = "http://www.overpass-api.de/api/interpreter?data=[out:json];node['amenity'](" + overpassBbox + ");out;";
$.ajax(url)
.done(function (data) {
//console.log(data);
var pointsGeoJSON = overpassToGeoJSON(data);
var amenityMap = {
'hospital': 'hospital.svg',
'bank': 'bank.svg',
'pharmacy': 'pharmacy.svg',
'restaurant': 'restaurant.svg',
'school': 'school.svg',
'college': 'college.svg',
'bar': 'bar.svg'
};
// console.log('pointsGeoJSON', pointsGeoJSON);
poiLayer.on('layeradd', function (e) {
var layer = e.layer;
// console.log('layer', layer);
var amenityName = layer.feature.properties.amenity;
if (amenityMap.hasOwnProperty(amenityName)) {
var icon = {
'iconUrl': amenityMap[amenityName]
};
layer.setIcon(L.icon(icon));
}
});
poiLayer.setGeoJSON(pointsGeoJSON);
poiWithin();
function poiWithin() {
var within = turf.within(poiLayer.getGeoJSON(), buffer);
// console.log(within);
poiLayer.setGeoJSON(within);
}
})
.fail(function (err) {
alert("overpass query failed!");
});
}
function overpassToGeoJSON(data) {
var pointsGeoJSON = {
'type': 'FeatureCollection',
'features': []
};
for (var i = 0; i <= data.elements.length; i++) {
var point = data.elements[i];
if (point && point.lat && point.lon) {
var props = point.tags;
props.title = point.tags.name;
props['marker-color'] = "#CCCC00";
props['marker-size'] = 'medium';
props['marker-symbol'] = 'circle';
var geojson = {
'type': 'Feature',
"properties": props,
'geometry': {
'type': 'Point',
'coordinates': [point.lon, point.lat]
}
};
pointsGeoJSON.features.push(geojson);
// console.log(geojson);
}
}
return pointsGeoJSON;
}
</script>
</body>
</html>