Hi, I want to find out my Current location using Titanium gps and here is my code.can any one help me plz
var win = Titanium.UI.currentWindow; //win.backgroundColor = '#fff';
Ti.include("version.js");
Ti.Geolocation.preferredProvider = "gps";
if (isIPhone3_2_Plus()) { //NOTE: starting in 3.2+, you'll need to set the applications //purpose property for using Location services on iPhone Ti.Geolocation.purpose = "GPS demo"; }
var currentLocationLabel = Titanium.UI.createLabel({ text:'Current Location (One Shot)', font:{fontSize:12, fontWeight:'bold'}, color:'#111', top:110, left:10, height:15, width:300 }); win.add(currentLocationLabel); var currentLocation = Titanium.UI.createLabel({ text:'Current Location not fired', font:{fontSize:11}, color:'#444', top:130, left:10, height:15, width:300 }); win.add(currentLocation);
Titanium.Geolocation.getCurrentPosition(function(e) { if (!e.success || e.error) { currentLocation.text = 'error: ' + JSON.stringify(e.error); Ti.API.info("Code translation: "+translateErrorCode(e.code)); alert('error ' + JSON.stringify(e.error)); return; }
var longitude = e.coords.longitude; var latitude = e.coords.latitude; var altitude = e.coords.altitude; var heading = e.coords.heading; var accuracy = e.coords.accuracy; var speed = e.coords.speed; var timestamp = e.coords.timestamp; var altitudeAccuracy = e.coords.altitudeAccuracy; Ti.API.info('speed ' + speed); currentLocation.text = 'long:' + longitude + ' lat: ' + latitude;
Titanium.API.info('geo - current location: ' + new Date(timestamp) + ' long ' + longitude + ' lat ' + latitude + ' accuracy ' + accuracy); });
4 Answers
This code works fine. This will work in device not in simulator.
var longitude; var latitude; Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST; Titanium.Geolocation.distanceFilter = 10; Titanium.Geolocation.getCurrentPosition(function(e) { if (!e.success || e.error) { alert('error ' + JSON.stringify(e.error)); return; } longitude = e.coords.longitude; latitude = e.coords.latitude; var altitude = e.coords.altitude; var heading = e.coords.heading; var accuracy = e.coords.accuracy; var speed = e.coords.speed; var timestamp = e.coords.timestamp; var altitudeAccuracy = e.coords.altitudeAccuracy; }); var locationCallback = function(e) { if (!e.success || e.error) { return; } var longitude = e.coords.longitude; var latitude = e.coords.latitude; var altitude = e.coords.altitude; var heading = e.coords.heading; var accuracy = e.coords.accuracy; var speed = e.coords.speed; var timestamp = e.coords.timestamp; var altitudeAccuracy = e.coords.altitudeAccuracy; setTimeout(function() { },100); // reverse geo Titanium.Geolocation.reverseGeocoder(latitude,longitude,function(evt) { if (evt.success) { var places = evt.places; if (places && places.length) { //reverseGeo.text = places[0].address; var place = places[0].address; alert("Current location "+place); } else { //reverseGeo.text = "No address found"; alert("No address found"); } //Ti.API.debug("reverse geolocation result = "+JSON.stringify(evt)); } else { } }); }; Titanium.Geolocation.addEventListener('location', locationCallback);This will help you !!!!!!!!!!!!!!!!!!
your code is un-readable. please format it.
try this code on using Geolocation services
please Format your Code. What is your issue?
Are you trying on emulator or device. You can find your current location only on device not on emulator/simulator.
Your code is not properly readable.
Your Answer
Think you can help? Login to answer this question!