-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
244 lines (213 loc) · 8.55 KB
/
scripts.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
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
let map;
//track markers to delete
let markers = [];
//set infowindow to be accessible when creating marker
//let infowindow;
//===============================================
//INIT MAP
//===============================================
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 34.0522, lng: -118.2437},
zoom: 11
});
//click to change data ===============================
map.addListener("click", function(event){
let lat = event.latLng.lat()
let lng = event.latLng.lng()
let userCenter = new google.maps.LatLng(lat, lng)
//convert radius from meters to miles
let userRadius = Number(radiusSelect.value) * 1609;
deleteMarkers()
addNearestMarkers(fpactData, userCenter, userRadius);
})
//click to change data end===============================
//autocomplete function ===============================
var autocomplete = new google.maps.places.Autocomplete(searchInput);
// Bind the map's bounds (viewport) property to the autocomplete object,
// so that the autocomplete requests use the current map bounds for the
// bounds option in the request.
autocomplete.bindTo('bounds', map);
// Set the data fields to return when the user selects a place.
autocomplete.setFields(
['address_components', 'geometry', 'icon', 'name']);
autocomplete.addListener('place_changed', function(){
let place = autocomplete.getPlace();
if (!place.geometry) {
// User entered the name of a Place that was not suggested and
// pressed the Enter key, or the Place Details request failed.
window.alert("No details available for input: '" + place.name + "'." + ' Try clicking a point on the map or clicking one of the Search suggestions if problems persist.');
return;
}
let lat = place.geometry.location.lat()
let lng = place.geometry.location.lng()
let autoCompleteCenter = new google.maps.LatLng(lat, lng);
map.setCenter(place.geometry.location);
let autoCompleteRadius = Number(radiusSelect.value) * 1609;
deleteMarkers();
addNearestMarkers(fpactData, place.geometry.location, autoCompleteRadius)
})
//end autocomplete function================================================
}
//===============================================
//END INIT MAP
//===============================================
//===============================================
//FETCH DATA
//===============================================
//FPACT Data
//cors proxy -- You can easily run your own proxy using code from https://github.com/Rob--W/cors-anywhere/.
const proxyurl = "https://cors-anywhere.herokuapp.com/";
let url1 = 'http://dhcs-chhsagency.opendata.arcgis.com/datasets/a2742f60dd944a1fa49377bd0e8a7772_0.geojson';
let fpactData;
//fetch(proxyurl + url1)
fetch(url1)
.then(function(response) {
return response.json();
})
.then(function(data) {
fpactData = data;
let center = new google.maps.LatLng(34.0522, -118.2437)
let radius = 5 * 1609; // convert meters to miles
addNearestMarkers(data, center, radius);
});
//===============================================
//END FETCH DATA
//===============================================
//===============================================
// ADD NEAREST MARKERS (geojson, center, radius)
//geojson - a geojson object
//center - a google maps latLng object (eg ...let center = new google.maps.LatLng(34.0522, -118.2437)...)
//radius - number (in miles) function converts radius from meters to miles
//===============================================
function addNearestMarkers(geojson, center, radius){
let infoWindowBase = new google.maps.InfoWindow({
content: 'YOLO'
});
let contentString = '';
console.log(geojson);
geojson.features.forEach(feature=>{
let newPoint = new google.maps.LatLng(feature.geometry.coordinates[1], feature.geometry.coordinates[0])
//computeDistanceBetween is part of google maps geometry library loaded in html
if(google.maps.geometry.spherical.computeDistanceBetween(newPoint, center) < radius){
//create infowindow content
infoContent = feature.properties;
//add marker
addMarker(feature.geometry.coordinates[1], feature.geometry.coordinates[0], infoWindowBase, infoContent)
}
})
}
//===============================================
// ADD MARKER
//addMarker(lat, lng, contentString, infowindow, infocontent)
//Notes:
// - infowindow is set in parent scope of add marker to avoid having multiple
// infowindows and cluttering the UI
// - infocontent must be in the form that the data is retrieved from the FPACT data set
// in order for createInfoWindowObject() to work correctly and dynamically display
// the content of the info window
//===============================================
function addMarker(lat, lng, infowindow, infocontent){
let marker = new google.maps.Marker({
position: {
lat: lat,
lng: lng
},
map: map,
})
let windowObj = createInfoWindowObject(infocontent);
renderInfoWindow(windowObj)
let address = infocontent.Provider_Businness_Legal_Name + ' ' + infocontent.Provider_Address_City + ' ' + infocontent.Provider_Address_Zip
marker.addListener('click', function(){
infowindow.setContent(renderInfoWindow(windowObj))
infowindow.open(map, marker)
getPlacesData(address, infowindow, windowObj)
})
markers.push(marker)
}
//===============================================
//getPlacesData(address, infowindow)
//Uses address built on the FPACT geojson feature data from a marker
//in order to:
// fetch the place ID
// fetch the google Places API placeDetails
// set/update infoWindow content with the correct phone number
//===============================================
function getPlacesData(address, infowindow, windowObj){
let request = {
query: address,
fields: ['name', 'geometry', 'place_id']
};
let service = new google.maps.places.PlacesService(map);
//async func 1 - findPlaceFromQuery() retrieves the place_id
let details = service.findPlaceFromQuery(request, function(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
let requestDetails = {
placeId: results[0].place_id,
fields: ['name', 'formatted_address', 'formatted_phone_number', 'place_id', 'geometry']
}
//async func 2 - getDetails()
let level2Data = service.getDetails(requestDetails, function(place, status){
if (status === google.maps.places.PlacesServiceStatus.OK) {
//update infowindow content here to add phone number
windowObj.phone = place.formatted_phone_number;
infowindow.setContent(renderInfoWindow(windowObj) )
}
})
} else {
console.log("sorry we can't seem to find that location")
}
});
}
//===============================================
// CLEAR MAP
//===============================================
// Sets the map on all markers in the array.
function setMapOnAll(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}
// Removes the markers from the map, but keeps them in the array.
// The function can be used to toggle markers.
function clearMarkers() {
setMapOnAll(null);
}
// Deletes all markers in the array by removing references to them.
function deleteMarkers() {
clearMarkers();
markers = [];
}
//===============================================
// createInfoWindowObject(featureData)
// creates an object to be passed into createInfoWindowContent()
//===============================================
function createInfoWindowObject(featureData){
let infoWindowData = {}
infoWindowData.title = featureData.Provider_Businness_Legal_Name;
infoWindowData.address = featureData.Provider_Address_Line_1;
infoWindowData.city = featureData.Provider_Address_City;
infoWindowData.zip = featureData.Provider_Address_Zip;
infoWindowData.phone = 'Retrieving phone number ...';
//category anti pattern
infoWindowData.category = featureData.Provider_Type_Code_Desc;
return infoWindowData;
}
//===============================================
// renderInfoWindow(featureData)
// uses the feature data from the FPACT data set
// to create an infowindow with all relevant data
//===============================================
function renderInfoWindow(dataObj){
let contentString = ''
Object.keys(dataObj).forEach(key=>{
if(key === 'title'){
contentString += '<h3>' + dataObj[key] + '</h3>'
} else if (key === 'category') {
contentString += '<p>' + key + ':' + dataObj[key] + '</p>'
} else {
contentString += '<p>' + dataObj[key] + '</p>'
}
})
return contentString;
}