-
Notifications
You must be signed in to change notification settings - Fork 71
/
index_foursquare_xdkAR.html
369 lines (340 loc) · 14.3 KB
/
index_foursquare_xdkAR.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<!doctype html>
<html>
<head>
<title>Nearby</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="Copyright" content="© 2013, Intel Corporation. All rights reserved." />
<meta name="Author" content="Rakshith Krishnappa" />
<!--
* Copyright (c) 2013, Intel Corporation. All rights reserved.
* Please see http://software.intel.com/html5/license/samples
* and the included README.md file for license terms and conditions.
-->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes" />
<script src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script src="lib/jquery/jquery-1.8.2.min.js"></script>
<script src="intelxdk.js"></script>
<script src="cordova.js"></script>
<script>
var FOURSQUARE_CLIENT_ID = "";
var FOURSQUARE_CLIENT_SECRET = "";
var pin=[], markersArray = [], bounds;
var myLat = 0, myLng = 0;
var bearing, distance;
var dataStatus = 0, q = "coffee";
// setup map and listen to deviceready
$( document ).ready(function() {
if(!FOURSQUARE_CLIENT_ID || !FOURSQUARE_CLIENT_SECRET){
alert("Register a foursquare app at developer.foursquare.com and enter the client_id and client_secret in this app code.");
} else {
document.addEventListener("deviceready", onDeviceReady, false);
}
});
// start device compass, accelerometer and geolocation after deviceready
function onDeviceReady() {
document.getElementById('log').innerHTML += "deciveready.";
navigator.splashscreen.hide();
setupMap();
// start cordova device sensors
startAccelerometer();
startCompass();
startGeolocation();
}
// start intel.xdk augmented reality mode, adds camera in background
function xdkStartAR(){
intel.xdk.display.startAR();
$('#arView').css('background-color','transparent');
$('body').css('background-color','transparent');
}
// stop intel.xdk augmented reality mode
function xdkStopAR(){
intel.xdk.display.stopAR();
}
// setup google maps api
function setupMap(){
$("#map").height($(window).height()-60);
var mapOptions = {
zoom: 13,
mapTypeControl: false,
streetViewControl: false,
navigationControl: true,
scrollwheel: false,
navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"), mapOptions);
}
// toggle between list view and map view
function toggleView(){
if($(".listView").is(":visible")){
$(".listView").hide();
$("#map").height($(window).height()-60);
$(".mapView").fadeIn(function(){google.maps.event.trigger(map, "resize");map.fitBounds(bounds);});
$("#viewbtn").html("List");
} else {
$(".mapView").hide();
$(".listView").fadeIn();
$("#viewbtn").html("Map");
}
}
// new search data
function searchData(){
q = $("#search").val();
getData(q);
}
// refresh new data
function refresh(){
getData(q);
}
// get data from API and store in array, add to list view and create markers on map, calculate
function getData(query){
dataStatus = "loading";
$(".listItems").html("<div class='item'>Loading...</div>");
var url = "https://api.foursquare.com/v2/venues/search?query="+query+"&ll="+myLat+","+myLng+"&intent=checkin&limit=20&v=20130203&client_id="+FOURSQUARE_CLIENT_ID+"&client_secret="+FOURSQUARE_CLIENT_SECRET+"&callback=?";
$.getJSON(url, function(data){
if(data.response.venues){
// clear list view and map markers and arrays
$(".listItems").html("<div class='query'>↓ Showing '"+q+"'</div>");
pin=[];
clearMarkers();
markersArray = [];
bounds = new google.maps.LatLngBounds();
// add blue gps marker
var icon = new google.maps.MarkerImage('http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png',new google.maps.Size(30, 28),new google.maps.Point(0,0),new google.maps.Point(9, 28));
var gpsMarker = new google.maps.Marker({position: new google.maps.LatLng(myLat, myLng), map: map, title: "My Position", icon:icon});
bounds.extend(new google.maps.LatLng(myLat, myLng));
markersArray.push(gpsMarker);
// add all location markers to map and list view and array
for(var i=0; i< data.response.venues.length; i++){
var point = {};
point['name'] = data.response.venues[i].name;
point['lat'] = data.response.venues[i].location.lat;
point['lng'] = data.response.venues[i].location.lng;
pin.push(point);
$(".listItems").append("<div class='item'>"+pin[i].name+"</div>");
addMarker(i);
relativePosition(i);
}
map.fitBounds(bounds);
google.maps.event.trigger(map, "resize");
dataStatus = "loaded";
} else if (data.meta && data.meta.errorDetail){
alert(data.meta.errorDetail);
$(".listItems").html("<div class='item'>"+data.meta.errorDetail+"</div>");
} else {
$(".listItems").html("<div class='item'>Error getting data</div>");
}
});
}
// add marker to map and in array
function addMarker(i){
var marker = new google.maps.Marker({position: new google.maps.LatLng(pin[i].lat, pin[i].lng), map: map, title: pin[i].name});
bounds.extend(new google.maps.LatLng(pin[i].lat, pin[i].lng));
markersArray.push(marker);
}
// clear all markers from map and array
function clearMarkers() {
while (markersArray.length) {
markersArray.pop().setMap(null);
}
}
// calulate distance and bearing value for each of the points wrt gps lat/lng
function relativePosition(i){
var pinLat = pin[i].lat;
var pinLng = pin[i].lng;
var dLat = (myLat-pinLat)* Math.PI / 180;
var dLon = (myLng-pinLng)* Math.PI / 180;
var lat1 = pinLat * Math.PI / 180;
var lat2 = myLat * Math.PI / 180;
var y = Math.sin(dLon) * Math.cos(lat2);
var x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
bearing = Math.atan2(y, x) * 180 / Math.PI;
bearing = bearing + 180;
pin[i]['bearing'] = bearing;
var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
distance = 3958.76 * c;
pin[i]['distance'] = distance;
}
// calculate direction of points and display
function calculateDirection(degree){
var detected = 0;
$("#spot").html("");
for(var i=0;i<pin.length;i++){
if(Math.abs(pin[i].bearing - degree) <= 20){
var away, fontSize, fontColor;
// varry font size based on distance from gps location
if(pin[i].distance>5){
away = Math.round(pin[i].distance);
fontSize = "16";
fontColor = "#ccc";
} else if(pin[i].distance>2){
away = Math.round(pin[i].distance);
fontSize = "24";
fontColor = "#ddd";
} else {
away = pin[i].distance.toFixed(2);
fontSize = "30";
fontColor = "#eee";
}
$("#spot").append('<div class="name" data-id="'+i+'" style="margin-left:'+(((pin[i].bearing - degree) * 5)+50)+'px;width:'+($(window).width()-100)+'px;font-size:'+fontSize+'px;color:'+fontColor+'">'+pin[i].name+'<div class="distance">'+ away +' miles away</div></div>');
detected = 1;
} else {
if(!detected){
$("#spot").html("");
}
}
}
}
// Start watching the geolocation
function startGeolocation(){
var options = { timeout: 30000 };
watchGeoID = navigator.geolocation.watchPosition(onGeoSuccess, onGeoError, options);
}
// Stop watching the geolocation
function stopGeolocation() {
if (watchGeoID) {
navigator.geolocation.clearWatch(watchGeoID);
watchGeoID = null;
}
}
// onSuccess: Get the current location
function onGeoSuccess(position) {
document.getElementById('geolocation').innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' + 'Longitude: ' + position.coords.longitude;
myLat = position.coords.latitude;
myLng = position.coords.longitude;
if(!dataStatus){
getData(q);
}
}
// onError: Failed to get the location
function onGeoError() {
document.getElementById('log').innerHTML += "onError=.";
}
// Start watching the compass
function startCompass() {
var options = { frequency: 100 };
watchCompassID = navigator.compass.watchHeading(onCompassSuccess, onCompassError, options);
}
// Stop watching the compass
function stopCompass() {
if (watchCompassID) {
navigator.compass.clearWatch(watchCompassID);
watchCompassID = null;
}
}
// onSuccess: Get the current heading
function onCompassSuccess(heading) {
var directions = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW', 'N'];
var direction = directions[Math.abs(parseInt((heading.magneticHeading) / 45) + 1)];
document.getElementById('compass').innerHTML = heading.magneticHeading + "<br>" + direction;
document.getElementById('direction').innerHTML = direction;
var degree = heading.magneticHeading;
if($("#arView").is(":visible") && dataStatus != "loading"){
calculateDirection(degree);
}
}
// onError: Failed to get the heading
function onCompassError(compassError) {
document.getElementById('log').innerHTML += "onError=."+compassError.code;
}
// Start checking the accelerometer
function startAccelerometer() {
var options = { frequency: 100 };
watchAccelerometerID = navigator.accelerometer.watchAcceleration(onAccelerometerSuccess, onAccelerometerError, options);
}
// Stop checking the accelerometer
function stopAccelerometer() {
if (watchAccelerometerID) {
navigator.accelerometer.clearWatch(watchAccelerometerID);
watchAccelerometerID = null;
}
}
// onSuccess: Get current accelerometer values
function onAccelerometerSuccess(acceleration) {
// for debug purpose to print out accelerometer values
var element = document.getElementById('accelerometer');
element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />' +
'Acceleration Y: ' + acceleration.y + '<br />' +
'Acceleration Z: ' + acceleration.z ;
if(acceleration.y > 7){
$("#arView").fadeIn();
$("#topView").hide();
document.getElementById('body').style.background = "#d22";
xdkStartAR();
} else {
$("#arView").hide();
$("#topView").fadeIn();
document.getElementById('body').style.background = "#fff";
xdkStopAR();
}
}
// onError: Failed to get the acceleration
function onAccelerometerError() {
document.getElementById('log').innerHTML += "onError.";
}
</script>
<style>
body {background-color:#fff;font-family:Arial;margin:0;overflow-x:hidden;-webkit-user-select: none;}
.navbar {background-color:#222;height:40px;padding:10px;text-align:center;color:#fff;font-size:20px;font-weight:bold;line-height:40px;}
.navtitle {text-align:center;margin:auto}
.navbtn {background-color:#333;padding:5px 10px;height:30px;color:#fff;font-size:18px;font-weight:bold;line-height:30px;border-radius:4px;border:1px solid #666}
#actionbtn {float:right;}
#viewbtn {float:left;}
.query {padding:10px;background-color:#aaa;border-bottom:1px solid #fff;font-size:14px;font-weight:bold;color:#222;}
.item {padding:20px 10px; background-color:#eee;border-bottom:1px solid #fff;font-size:18px;color:#333;text-shadow:0 1px #fff}
.searchbox {padding:5px;background-color:#eee;border-bottom:1px solid #fff;}
#search {box-sizing: border-box;width:100%;height:40px;font-size:16px;border-radius:20px;border:1px solid #bbb}
.mapView {display:none}
#map {height:200px;}
#arView, #topView {display:none;}
#arView{padding:30px 0; height:70px;text-align:center}
.arMessage {color:#ddd;font-size:14px;text-shadow: 0 0 0.2em #000, 0 0 0.2em #000,0 0 0.2em #000}
#spot {text-align:center}
.name, .distance {text-shadow:0 1px #666}
.name {padding:15px;font-weight:bold;;background-color:#c22;border-radius:40px;margin-bottom:10px}
#direction {color:#d55;font-size:20px;padding:15px;font-weight:bold;;background-color:#a22;border-radius:40px;display:inline-block;margin-bottom:10px;width:40px;line-height:40px}
.distance {font-size:14px;font-weight:normal;}
#debug {border:1px solid #999;display:none}
.heading {background-color:#999;color:#eee;padding:5px;}
#compass, #accelerometer, #geolocation {padding:5px}
</style>
</head>
<body id="body">
<div id="arView">
<div class="arMessage">↑<br>Tilt down to see all places</div>
<br>
<div class="arMessage">← Move the device around to find spots →</div>
<br>
<div id="direction"></div>
<br>
<div id="spot"></div>
</div>
<div id="topView">
<div class="navbar">
<div id="actionbtn" class="navbtn" onclick="refresh()"> ↵ </div>
<div id="viewbtn" class="navbtn" onclick="toggleView()">Map</div>
<div class="navtitle">Nearby</div>
</div>
<div class="listView">
<div class="searchbox"><input id="search" type="text" placeholder="Search Anything" onKeyPress='if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {searchData()} else return true;'></div>
<div class="listItems"></div>
</div>
<div class="mapView">
<div id="map"></div>
</div>
</div>
<div id="debug">
<div class="heading">Geolocation</div>
<div id="geolocation"></div>
<div class="heading">Compass</div>
<div id="compass"></div>
<div class="heading">Accelerometer</div>
<div id="accelerometer"></div>
<div class="heading">Log</div>
<div id="log"></div>
</div>
<!-- <script src="http://debug-software.intel.com/target/target-script-min.js#9ddff4f5-bc93-4e9f-9b7c-d38510fe6e79"/> -->
</body>
</html>