Cannot return location annotation from Titanium.Geolocation.getCurrentPosition...why?

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

I am working on a project which i need to use my location.For that, i used the geolocation.js and i customed it to my project but i have a trouble. My code is that:

Titanium.Geolocation.getCurrentPosition(function(e)
{
 
    if (e.error)
    {
        alert('error ' + JSON.stringify(e.error))
        return;
    }
 
    var current_longitude = e.coords.longitude;
    var current_latitude = e.coords.latitude;
    var current_altitude = e.coords.altitude;
    var current_heading = e.coords.heading;
    var current_accuracy = e.coords.accuracy;
    var current_speed = e.coords.speed;
    var current_timestamp = e.coords.timestamp;
    var current_altitudeAccuracy = e.coords.altitudeAccuracy;
 
 
 
 //==================================================//
 // CREATE ANNOTATIONS
 //
  var my_location = Titanium.Map.createAnnotation({
        animate:true,
        title:"You are here!",
        pincolor:Titanium.Map.ANNOTATION_GREEN,
        latitude:parseFloat(current_latitude),
        //latitude:data[c]['gmap_lat'],
        longitude:parseFloat(current_longitude),
        //longitude:data[c]['gmap_lon'],
        myid:30
      });       
 
    return my_location;
 
});     
 
////=====================================================//
//  CREATE MAP VIEW
//
var mapview = Titanium.Map.createView({
    mapType: Titanium.Map.STANDARD_TYPE,
    animate:true,
    regionFit:true,
    annotations:[my_location]
    });
 
    mapview.selectAnnotation(my_location);
 
    win.add(mapview);

and when i run it, the application pop up an error which tell me that "my_location is not define".

1.What i am doing wrong?

2.I have to define anything else?

3.Also i have the some problem when i try to return an array o location annotations.

1 Answer

Christos

Your my_location variable is only available within the scope of the Titanium.Geolocation.getCurrentPosition() method. Try assigning the method to a variable, and using that instead:

For example:

var my_location = Titanium.Geolocation.getCurrentPosition(function(e)
{
    if (e.error)
    {
        alert('error ' + JSON.stringify(e.error))
        return;
    }
 
    var current_longitude = e.coords.longitude;
    var current_latitude = e.coords.latitude;
    var current_altitude = e.coords.altitude;
    var current_heading = e.coords.heading;
    var current_accuracy = e.coords.accuracy;
    var current_speed = e.coords.speed;
    var current_timestamp = e.coords.timestamp;
    var current_altitudeAccuracy = e.coords.altitudeAccuracy;
//==================================================//
// CREATE ANNOTATIONS
//
    var thisLocation = Titanium.Map.createAnnotation({
        animate:true,
        title:"You are here!",
        pincolor:Titanium.Map.ANNOTATION_GREEN,
        latitude:parseFloat(current_latitude),
        //latitude:data[c]['gmap_lat'],
        longitude:parseFloat(current_longitude),
        //longitude:data[c]['gmap_lon'],
        myid:30
    });       
    return thisLocation;
});     
 
////=====================================================//
//  CREATE MAP VIEW
//
var mapview = Titanium.Map.createView({
    mapType: Titanium.Map.STANDARD_TYPE,
    animate:true,
    regionFit:true,
    annotations:[my_location]
});
 
mapview.selectAnnotation(my_location);
 
win.add(mapview);

Your Answer

Think you can help? Login to answer this question!