I want to find out my Current location using Titanium gps

You must Login before you can answer or comment on any questions.

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

— answered 1 year ago by mo joe
answer permalink
5 Comments
  • var win = Titanium.UI.currentWindow; //win.backgroundColor = '#fff';

    Ti.include("version.js");

    Ti.Geolocation.preferredProvider = "gps";

    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.distanceFilter = 10; // set the granularity of the location event

    Titanium.Geolocation.getCurrentPosition(function(e)
    {
        if (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;
        currentLocation.text = 'long:' + longitude + ' lat: ' + latitude;
        Titanium.API.info('geo - current location: ' + new Date(timestamp) + ' long ' + longitude + ' lat ' + latitude + ' accuracy ' + accuracy);
    
                // we use the above data the way we need it
    });
    
    Titanium.Geolocation.addEventListener('location',function(e)
    {
        if (e.error)
        {
                // manage the 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;
        currentLocation.text = 'long:' + longitude + ' lat: ' + latitude;
        Titanium.API.info('geo - current location: ' + new Date(timestamp) + ' long ' + longitude + ' lat ' + latitude + ' accuracy ' + accuracy);
    
               // again we use the gathered data
      });
    

    here is my code

    — commented 1 year ago by chaitanya reddy

  • Hi Chaitanya.

    use the CODE styling methods in the top of each post box.

    what are you trying to do?

    — commented 1 year ago by mo joe

  • I want to find the location where I;m using gps

    — commented 1 year ago by chaitanya reddy

  • Show 2 more comments

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!