PageRenderTime 79ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/demos/KitchenSink-Nook/Resources/examples/geolocation.js

https://github.com/varju/titanium_mobile
JavaScript | 509 lines | 416 code | 50 blank | 43 comment | 35 complexity | 15e90fed8c5718b83b09d7426fb4631e MD5 | raw file
  1. var win = Titanium.UI.currentWindow;
  2. win.backgroundColor = '#fff';
  3. Ti.include("version.js");
  4. Ti.Geolocation.preferredProvider = "gps";
  5. if (isIPhone3_2_Plus())
  6. {
  7. //NOTE: starting in 3.2+, you'll need to set the applications
  8. //purpose property for using Location services on iPhone
  9. Ti.Geolocation.purpose = "GPS demo";
  10. }
  11. function translateErrorCode(code) {
  12. if (code == null) {
  13. return null;
  14. }
  15. switch (code) {
  16. case Ti.Geolocation.ERROR_LOCATION_UNKNOWN:
  17. return "Location unknown";
  18. case Ti.Geolocation.ERROR_DENIED:
  19. return "Access denied";
  20. case Ti.Geolocation.ERROR_NETWORK:
  21. return "Network error";
  22. case Ti.Geolocation.ERROR_HEADING_FAILURE:
  23. return "Failure to detect heading";
  24. case Ti.Geolocation.ERROR_REGION_MONITORING_DENIED:
  25. return "Region monitoring access denied";
  26. case Ti.Geolocation.ERROR_REGION_MONITORING_FAILURE:
  27. return "Region monitoring access failure";
  28. case Ti.Geolocation.ERROR_REGION_MONITORING_DELAYED:
  29. return "Region monitoring setup delayed";
  30. }
  31. }
  32. var currentHeadingLabel = Titanium.UI.createLabel({
  33. text:'Current Heading (One Shot)',
  34. font:{fontSize:12, fontWeight:'bold'},
  35. color:'#111',
  36. top:10,
  37. left:10,
  38. height:15,
  39. width:300
  40. });
  41. win.add(currentHeadingLabel);
  42. var currentHeading = Titanium.UI.createLabel({
  43. text:'Updated Heading not fired',
  44. font:{fontSize:12},
  45. color:'#444',
  46. top:30,
  47. left:10,
  48. height:15,
  49. width:300
  50. });
  51. win.add(currentHeading);
  52. var updatedHeadingLabel = Titanium.UI.createLabel({
  53. text:'Updated Heading',
  54. font:{fontSize:12, fontWeight:'bold'},
  55. color:'#111',
  56. top:50,
  57. left:10,
  58. height:15,
  59. width:300
  60. });
  61. win.add(updatedHeadingLabel);
  62. var updatedHeading = Titanium.UI.createLabel({
  63. text:'Updated Heading not fired',
  64. font:{fontSize:12},
  65. color:'#444',
  66. top:70,
  67. left:10,
  68. height:15,
  69. width:300
  70. });
  71. win.add(updatedHeading);
  72. var updatedHeadingTime = Titanium.UI.createLabel({
  73. text:'',
  74. font:{fontSize:11},
  75. color:'#444',
  76. top:90,
  77. left:10,
  78. height:15,
  79. width:300
  80. });
  81. win.add(updatedHeadingTime);
  82. var currentLocationLabel = Titanium.UI.createLabel({
  83. text:'Current Location (One Shot)',
  84. font:{fontSize:12, fontWeight:'bold'},
  85. color:'#111',
  86. top:110,
  87. left:10,
  88. height:15,
  89. width:300
  90. });
  91. win.add(currentLocationLabel);
  92. var currentLocation = Titanium.UI.createLabel({
  93. text:'Current Location not fired',
  94. font:{fontSize:11},
  95. color:'#444',
  96. top:130,
  97. left:10,
  98. height:15,
  99. width:300
  100. });
  101. win.add(currentLocation);
  102. var updatedLocationLabel = Titanium.UI.createLabel({
  103. text:'Updated Location',
  104. font:{fontSize:12, fontWeight:'bold'},
  105. color:'#111',
  106. top:150,
  107. left:10,
  108. height:15,
  109. width:300
  110. });
  111. win.add(updatedLocationLabel);
  112. var updatedLocation = Titanium.UI.createLabel({
  113. text:'Updated Location not fired',
  114. font:{fontSize:11},
  115. color:'#444',
  116. top:170,
  117. left:10,
  118. height:15,
  119. width:300
  120. });
  121. win.add(updatedLocation);
  122. var updatedLatitude = Titanium.UI.createLabel({
  123. text:'',
  124. font:{fontSize:11},
  125. color:'#444',
  126. top:190,
  127. left:10,
  128. height:15,
  129. width:300
  130. });
  131. win.add(updatedLatitude);
  132. var updatedLocationAccuracy = Titanium.UI.createLabel({
  133. text:'',
  134. font:{fontSize:11},
  135. color:'#444',
  136. top:210,
  137. left:10,
  138. height:15,
  139. width:300
  140. });
  141. win.add(updatedLocationAccuracy);
  142. var updatedLocationTime = Titanium.UI.createLabel({
  143. text:'',
  144. font:{fontSize:11},
  145. color:'#444',
  146. top:230,
  147. left:10,
  148. height:15,
  149. width:300
  150. });
  151. win.add(updatedLocationTime);
  152. var forwardGeoLabel = Titanium.UI.createLabel({
  153. text:'Forward Geo (Addr->Coords)',
  154. font:{fontSize:12, fontWeight:'bold'},
  155. color:'#111',
  156. top:250,
  157. left:10,
  158. height:15,
  159. width:300
  160. });
  161. win.add(forwardGeoLabel);
  162. var forwardGeo = Titanium.UI.createLabel({
  163. text:'',
  164. font:{fontSize:11},
  165. color:'#444',
  166. top:270,
  167. left:10,
  168. height:15,
  169. width:300
  170. });
  171. win.add(forwardGeo);
  172. var reverseGeoLabel = Titanium.UI.createLabel({
  173. text:'Reverse Geo (Coords->Addr)',
  174. font:{fontSize:12, fontWeight:'bold'},
  175. color:'#111',
  176. top:290,
  177. left:10,
  178. height:15,
  179. width:300
  180. });
  181. win.add(reverseGeoLabel);
  182. var reverseGeo = Titanium.UI.createLabel({
  183. text:'',
  184. font:{fontSize:11},
  185. color:'#444',
  186. top:310,
  187. left:10,
  188. height:15,
  189. width:300
  190. });
  191. win.add(reverseGeo);
  192. // state vars used by resume/pause
  193. var headingAdded = false;
  194. var locationAdded = false;
  195. //
  196. // SHOW CUSTOM ALERT IF DEVICE HAS GEO TURNED OFF
  197. //
  198. if (Titanium.Geolocation.locationServicesEnabled === false)
  199. {
  200. Titanium.UI.createAlertDialog({title:'Kitchen Sink', message:'Your device has geo turned off - turn it on.'}).show();
  201. }
  202. else
  203. {
  204. if (Titanium.Platform.name != 'android') {
  205. var authorization = Titanium.Geolocation.locationServicesAuthorization;
  206. Ti.API.info('Authorization: '+authorization);
  207. if (authorization == Titanium.Geolocation.AUTHORIZATION_DENIED) {
  208. Ti.UI.createAlertDialog({
  209. title:'Kitchen Sink',
  210. message:'You have disallowed Titanium from running geolocation services.'
  211. }).show();
  212. }
  213. else if (authorization == Titanium.Geolocation.AUTHORIZATION_RESTRICTED) {
  214. Ti.UI.createAlertDialog({
  215. title:'Kitchen Sink',
  216. message:'Your system has disallowed Titanium from running geolocation services.'
  217. }).show();
  218. }
  219. }
  220. //
  221. // IF WE HAVE COMPASS GET THE HEADING
  222. //
  223. if (Titanium.Geolocation.hasCompass)
  224. {
  225. //
  226. // TURN OFF ANNOYING COMPASS INTERFERENCE MESSAGE
  227. //
  228. Titanium.Geolocation.showCalibration = false;
  229. //
  230. // SET THE HEADING FILTER (THIS IS IN DEGREES OF ANGLE CHANGE)
  231. // EVENT WON'T FIRE UNLESS ANGLE CHANGE EXCEEDS THIS VALUE
  232. Titanium.Geolocation.headingFilter = 90;
  233. //
  234. // GET CURRENT HEADING - THIS FIRES ONCE
  235. //
  236. Ti.Geolocation.getCurrentHeading(function(e)
  237. {
  238. if (e.error)
  239. {
  240. currentHeading.text = 'error: ' + e.error;
  241. Ti.API.info("Code translation: "+translateErrorCode(e.code));
  242. return;
  243. }
  244. var x = e.heading.x;
  245. var y = e.heading.y;
  246. var z = e.heading.z;
  247. var magneticHeading = e.heading.magneticHeading;
  248. var accuracy = e.heading.accuracy;
  249. var trueHeading = e.heading.trueHeading;
  250. var timestamp = e.heading.timestamp;
  251. currentHeading.text = 'x:' + x + ' y: ' + y + ' z:' + z;
  252. Titanium.API.info('geo - current heading: ' + new Date(timestamp) + ' x ' + x + ' y ' + y + ' z ' + z);
  253. });
  254. //
  255. // EVENT LISTENER FOR COMPASS EVENTS - THIS WILL FIRE REPEATEDLY (BASED ON HEADING FILTER)
  256. //
  257. var headingCallback = function(e)
  258. {
  259. if (e.error)
  260. {
  261. updatedHeading.text = 'error: ' + e.error;
  262. Ti.API.info("Code translation: "+translateErrorCode(e.code));
  263. return;
  264. }
  265. var x = e.heading.x;
  266. var y = e.heading.y;
  267. var z = e.heading.z;
  268. var magneticHeading = e.heading.magneticHeading;
  269. var accuracy = e.heading.accuracy;
  270. var trueHeading = e.heading.trueHeading;
  271. var timestamp = e.heading.timestamp;
  272. updatedHeading.text = 'x:' + x + ' y: ' + y + ' z:' + z;
  273. updatedHeadingTime.text = 'timestamp:' + new Date(timestamp);
  274. updatedHeading.color = 'red';
  275. updatedHeadingTime.color = 'red';
  276. setTimeout(function()
  277. {
  278. updatedHeading.color = '#444';
  279. updatedHeadingTime.color = '#444';
  280. },100);
  281. Titanium.API.info('geo - heading updated: ' + new Date(timestamp) + ' x ' + x + ' y ' + y + ' z ' + z);
  282. };
  283. Titanium.Geolocation.addEventListener('heading', headingCallback);
  284. headingAdded = true;
  285. }
  286. else
  287. {
  288. Titanium.API.info("No Compass on device");
  289. currentHeading.text = 'No compass available';
  290. updatedHeading.text = 'No compass available';
  291. }
  292. //
  293. // SET ACCURACY - THE FOLLOWING VALUES ARE SUPPORTED
  294. //
  295. // Titanium.Geolocation.ACCURACY_BEST
  296. // Titanium.Geolocation.ACCURACY_NEAREST_TEN_METERS
  297. // Titanium.Geolocation.ACCURACY_HUNDRED_METERS
  298. // Titanium.Geolocation.ACCURACY_KILOMETER
  299. // Titanium.Geolocation.ACCURACY_THREE_KILOMETERS
  300. //
  301. Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST;
  302. //
  303. // SET DISTANCE FILTER. THIS DICTATES HOW OFTEN AN EVENT FIRES BASED ON THE DISTANCE THE DEVICE MOVES
  304. // THIS VALUE IS IN METERS
  305. //
  306. Titanium.Geolocation.distanceFilter = 10;
  307. //
  308. // GET CURRENT POSITION - THIS FIRES ONCE
  309. //
  310. Titanium.Geolocation.getCurrentPosition(function(e)
  311. {
  312. if (!e.success || e.error)
  313. {
  314. currentLocation.text = 'error: ' + JSON.stringify(e.error);
  315. Ti.API.info("Code translation: "+translateErrorCode(e.code));
  316. alert('error ' + JSON.stringify(e.error));
  317. return;
  318. }
  319. var longitude = e.coords.longitude;
  320. var latitude = e.coords.latitude;
  321. var altitude = e.coords.altitude;
  322. var heading = e.coords.heading;
  323. var accuracy = e.coords.accuracy;
  324. var speed = e.coords.speed;
  325. var timestamp = e.coords.timestamp;
  326. var altitudeAccuracy = e.coords.altitudeAccuracy;
  327. Ti.API.info('speed ' + speed);
  328. currentLocation.text = 'long:' + longitude + ' lat: ' + latitude;
  329. Titanium.API.info('geo - current location: ' + new Date(timestamp) + ' long ' + longitude + ' lat ' + latitude + ' accuracy ' + accuracy);
  330. });
  331. //
  332. // EVENT LISTENER FOR GEO EVENTS - THIS WILL FIRE REPEATEDLY (BASED ON DISTANCE FILTER)
  333. //
  334. var locationCallback = function(e)
  335. {
  336. if (!e.success || e.error)
  337. {
  338. updatedLocation.text = 'error:' + JSON.stringify(e.error);
  339. updatedLatitude.text = '';
  340. updatedLocationAccuracy.text = '';
  341. updatedLocationTime.text = '';
  342. Ti.API.info("Code translation: "+translateErrorCode(e.code));
  343. return;
  344. }
  345. var longitude = e.coords.longitude;
  346. var latitude = e.coords.latitude;
  347. var altitude = e.coords.altitude;
  348. var heading = e.coords.heading;
  349. var accuracy = e.coords.accuracy;
  350. var speed = e.coords.speed;
  351. var timestamp = e.coords.timestamp;
  352. var altitudeAccuracy = e.coords.altitudeAccuracy;
  353. //Titanium.Geolocation.distanceFilter = 100; //changed after first location event
  354. updatedLocation.text = 'long:' + longitude;
  355. updatedLatitude.text = 'lat: '+ latitude;
  356. updatedLocationAccuracy.text = 'accuracy:' + accuracy;
  357. updatedLocationTime.text = 'timestamp:' +new Date(timestamp);
  358. updatedLatitude.color = 'red';
  359. updatedLocation.color = 'red';
  360. updatedLocationAccuracy.color = 'red';
  361. updatedLocationTime.color = 'red';
  362. setTimeout(function()
  363. {
  364. updatedLatitude.color = '#444';
  365. updatedLocation.color = '#444';
  366. updatedLocationAccuracy.color = '#444';
  367. updatedLocationTime.color = '#444';
  368. },100);
  369. // reverse geo
  370. Titanium.Geolocation.reverseGeocoder(latitude,longitude,function(evt)
  371. {
  372. if (evt.success) {
  373. var places = evt.places;
  374. if (places && places.length) {
  375. reverseGeo.text = places[0].address;
  376. } else {
  377. reverseGeo.text = "No address found";
  378. }
  379. Ti.API.debug("reverse geolocation result = "+JSON.stringify(evt));
  380. }
  381. else {
  382. Ti.UI.createAlertDialog({
  383. title:'Reverse geo error',
  384. message:evt.error
  385. }).show();
  386. Ti.API.info("Code translation: "+translateErrorCode(e.code));
  387. }
  388. });
  389. Titanium.API.info('geo - location updated: ' + new Date(timestamp) + ' long ' + longitude + ' lat ' + latitude + ' accuracy ' + accuracy);
  390. };
  391. Titanium.Geolocation.addEventListener('location', locationCallback);
  392. locationAdded = true;
  393. }
  394. var addr = "2065 Hamilton Avenue San Jose California 95125";
  395. Titanium.Geolocation.forwardGeocoder(addr,function(evt)
  396. {
  397. Ti.API.info('in forward ');
  398. forwardGeo.text = "lat:"+evt.latitude+", long:"+evt.longitude;
  399. Titanium.Geolocation.reverseGeocoder(evt.latitude,evt.longitude,function(evt)
  400. {
  401. if (evt.success) {
  402. var text = "";
  403. for (var i = 0; i < evt.places.length; i++) {
  404. text += "" + i + ") " + evt.places[i].address + "\n";
  405. }
  406. Ti.API.info('Reversed forward: '+text);
  407. }
  408. else {
  409. Ti.UI.createAlertDialog({
  410. title:'Forward geo error',
  411. message:evt.error
  412. }).show();
  413. Ti.API.info("Code translation: "+translateErrorCode(e.code));
  414. }
  415. });
  416. });
  417. if (Titanium.Platform.name == 'android')
  418. {
  419. // as the destroy handler will remove the listener, only set the pause handler to remove if you need battery savings
  420. Ti.Android.currentActivity.addEventListener('pause', function(e) {
  421. Ti.API.info("pause event received");
  422. if (headingAdded) {
  423. Ti.API.info("removing heading callback on pause");
  424. Titanium.Geolocation.removeEventListener('heading', headingCallback);
  425. headingAdded = false;
  426. }
  427. if (locationAdded) {
  428. Ti.API.info("removing location callback on pause");
  429. Titanium.Geolocation.removeEventListener('location', locationCallback);
  430. locationAdded = false;
  431. }
  432. });
  433. Ti.Android.currentActivity.addEventListener('destroy', function(e) {
  434. Ti.API.info("destroy event received");
  435. if (headingAdded) {
  436. Ti.API.info("removing heading callback on destroy");
  437. Titanium.Geolocation.removeEventListener('heading', headingCallback);
  438. headingAdded = false;
  439. }
  440. if (locationAdded) {
  441. Ti.API.info("removing location callback on destroy");
  442. Titanium.Geolocation.removeEventListener('location', locationCallback);
  443. locationAdded = false;
  444. }
  445. });
  446. Ti.Android.currentActivity.addEventListener('resume', function(e) {
  447. Ti.API.info("resume event received");
  448. if (!headingAdded) {
  449. Ti.API.info("adding heading callback on resume");
  450. Titanium.Geolocation.addEventListener('heading', headingCallback);
  451. headingAdded = true;
  452. }
  453. if (!locationAdded) {
  454. Ti.API.info("adding location callback on resume");
  455. Titanium.Geolocation.addEventListener('location', locationCallback);
  456. locationAdded = true;
  457. }
  458. });
  459. }