forked from CivicTechAtlanta/asset-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instagram.js
155 lines (121 loc) · 4.61 KB
/
instagram.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
var target = document.getElementById('map');
var spinner = new Spinner().spin(target);
// extend L.Map so popups don't close automatically (so they can all be open on load)
L.Map = L.Map.extend({
openPopup: function(popup) {
// this.closePopup();
this._popup = popup;
return this.addLayer(popup).fire('popupopen', {
popup: this._popup
});
}
});
var southDowntownLayer = L.geoJson(southDowntown, {
onEachFeature: function(feature, layer) {
layer.bindPopup(feature.properties.name);
},
style: {color: '#007df0',
opacity: 1},
});
// instagram integration
var CLIENT_ID = '426ca0b85f5243a99098fc6ea703d53b';
// var API_ENDPOINT = 'https://api.instagram.com/v1/media/search' +
// '?client_id=CLIENT_ID' +
// '&lat=33.74986&lng=-84.39223' +
// '&distance=889' +
// '&callback=?';
var API_ENDPOINT = 'https://api.instagram.com/v1/tags/weloveatl/media/recent' +
'?client_id=CLIENT_ID';
var instagramLayer = L.layerGroup();
var nextEndpoint = null;
var notInSouthDowntown = [
"Atlanta, Georgia",
"Edgewood Ave",
"Peachtree Center (MARTA station)",
"Jungle",
"Atlanta,Ga" // a lot of these seem to actually be downtown
];
// change background color of spinner
function queryInstagram(next_url, geoItems, callback) {
var nextURL = next_url ? next_url : API_ENDPOINT;
nextURL += '&callback=?';
$.getJSON(nextURL
.replace('CLIENT_ID', CLIENT_ID), function(result, status) {
if (status !== 'success') return alert('Request to Instagram failed');
// only add items to map if they are inside the south downtown polygon
for (var i = 0; i < result.data.length; i++) {
var item = result.data[i];
if (item.location) {
// check if they are from a location not really in south downtown
if ($.inArray(item.location.name, notInSouthDowntown) >= 0) {
console.log("excluding: ", item.link);
break;
};
var inPolygon = leafletPip.pointInLayer([item.location.longitude, item.location.latitude], southDowntownLayer);
if (inPolygon.length) {
console.log(item);
geoItems.push(item);
};
};
}
if (geoItems.length >= 5) {
callback(geoItems, result.pagination.next_url);
} else {
queryInstagram(result.pagination.next_url, geoItems, callback);
};
});
}
function getPhotosAndAddToMap(next_url) {
// start spinner
spinner.spin(target);
document.getElementById("refresh").style.display = 'none';
queryInstagram(next_url, [], function (geoItems, next_url) {
// add photos to map
for (var i = 0; i < geoItems.length; i++) {
var media = geoItems[i];
var popupContents = '<a href="' + media.link + '" target="_blank"><img src="' + media.images.thumbnail.url + '" width = "' + media.images.thumbnail.width + '"></a>';
if (media.type === "video") {
popupContents = '<video width="150" height="150" controls="controls"><source src="' + media.videos.low_bandwidth.url + '" type="video/mp4"></video><br><a href="' + media.link + '" target="_blank">Link</a>';
}
var latlng = L.latLng(media.location.latitude, media.location.longitude);
var marker = L.marker(latlng, {
icon: L.AwesomeMarkers.icon({
icon: 'instagram',
prefix: 'fa',
markerColor: 'cadetblue'
})
})
.bindPopup(popupContents)
.addTo(instagramLayer)
.togglePopup();
};
nextEndpoint = next_url;
// end spinner
spinner.stop();
document.getElementById("refresh").style.display = 'block';
// show button to add more
// button runs getPhotosAndAddToMap(nextEndpoint)
});
};
getPhotosAndAddToMap(null);
// colors: 'red', 'darkred', 'orange', 'green', 'darkgreen', 'blue', 'purple', 'darkpuple', 'cadetblue'
// map
var map = L.map('map', {
attributionControl: false,
center: new L.LatLng(33.75, -84.392),
zoom: 15,
layers: [instagramLayer, southDowntownLayer]
});
L.control.attribution({position: 'bottomleft'}).addTo(map);
L.tileLayer('http://otile4.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png', {
attribution:
'Made at <a href="http://www.codeforamerica.org/events/codeacross-2015/">CodeAcross</a> for <a href="http://www.codeforatlanta.org/"><img src="images/code-for-atlanta.png" height=70></a>' +
'<br>Tiles © <a href="http://www.mapquest.com/" target="_blank">MapQuest</a> <img src="http://developer.mapquest.com/content/osm/mq_logo.png" />',
maxZoom: 18
}).addTo(map);
var overlayMaps = {
"<i class='fa fa-instagram' style='color:#426776'></i> Instagram": instagramLayer
};
L.control.layers(null, overlayMaps, {
collapsed: false
}).addTo(map);