-
-
Save michelmelo/03a4c2c65a64580383e0 to your computer and use it in GitHub Desktop.
Ti.Geolocation Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (Ti.Geolocation.locationServicesEnabled) { | |
Titanium.Geolocation.purpose = 'Get Current Location'; | |
Titanium.Geolocation.getCurrentPosition(function(e) { | |
if (e.error) { | |
Ti.API.error('Error: ' + e.error); | |
} else { | |
Ti.API.info(e.coords); | |
} | |
}); | |
} else { | |
alert('Please enable location services'); | |
} | |
/* | |
Returns something similar to: | |
{ | |
"accuracy": 100, | |
"altitude": 0, | |
"altitudeAccuracy": null, | |
"heading": 0, | |
"latitude": 40.493781233333333, | |
"longitude": -80.056671 | |
"speed": 0, | |
"timestamp": 1318426498331 | |
} | |
*/ | |
// Use the compass | |
if (Ti.Geolocation.locationServicesEnabled) { | |
Ti.Geolocation.purpose = 'Get Current Heading'; | |
// make a single request for the current heading | |
Ti.Geolocation.getCurrentHeading(function(e) { | |
Ti.API.info(e.heading); | |
}); | |
// Set 'heading' event for continual monitoring | |
Ti.Geolocation.addEventListener('heading', function(e) { | |
if (e.error) { | |
alert('Error: ' + e.error); | |
} else { | |
Ti.API.info(e.heading); | |
} | |
}); | |
} else { | |
alert('Please enable location services'); | |
} | |
/* | |
The console output of your program will contain the heading information, which will be sent continuously from the heading event. The data for each heading entry will be structured in the following manner. | |
{ | |
"accuracy": 3, | |
"magneticHeading": 34.421875, // degrees east of magnetic north | |
"timestamp": 1318447443692, | |
"trueHeading": 43.595027923583984, // degrees east of true north | |
"type": "heading", | |
"x": 34.421875, | |
"y": -69.296875, | |
"z": -1.140625 | |
} | |
*/ | |
// Configure Location Service Properties | |
if (Ti.Geolocation.locationServicesEnabled) { | |
Ti.Geolocation.purpose = 'Get Current Location'; | |
Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST; | |
Ti.Geolocation.distanceFilter = 10; | |
Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS; | |
Ti.Geolocation.addEventListener('location', function(e) { | |
if (e.error) { | |
alert('Error: ' + e.error); | |
} else { | |
Ti.API.info(e.coords); | |
} | |
}); | |
} else { | |
alert('Please enable location services'); | |
} | |
// Forward and Reverse Geocoding | |
Ti.Geolocation.forwardGeocoder('440 Bernardo Ave Mountain View CA', function(e) { | |
Ti.API.info(e); | |
}); | |
/* | |
And here is the output of a forward geocoding of Appcelerator HQ. As you can see, it delivers the geographic location of the given address in latitude and longitude. | |
{ | |
"accuracy": 1, | |
"latitude": 37.389071, | |
"longitude": -122.050156, | |
"success": 1 | |
} | |
*/ | |
Ti.Geolocation.reverseGeocoder(50, 50, function(e) { | |
Ti.API.info(e); | |
}); | |
/* | |
Here's the output: | |
{ | |
"places": [ | |
{ | |
"address": ", 418020 Dzhany-Kuduk, , Kazakhstan", | |
"city": "Oral", | |
"country": "Kazakhstan", | |
"country_code": "KZ", | |
"latitude": 50.0, | |
"longitude": 50.0, | |
"street": "", | |
"zipcode": 418020 | |
} | |
], | |
"success": 1 | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment