Search code examples
javascriptmapstitaniumcoordinatesappcelerator

Titanium get moved pin coords


I am trying to get the coords after i moved the pin in the map. I used the user current location and when i open the map shows correctly. But when i move the pin to another street, won't able to get the new coords still showing the same coords.

Here is my code

var Map = require('ti.map');

if(Ti.Geolocation.locationServicesEnabled){

Ti.Geolocation.addEventListener('location', function(e) {

    if (e.error){

        alert('Error: ' + e.error);

    }else {
        longitude = e.coords.longitude;
        latitude = e.coords.latitude;



var mapView = Map.createView({
top          : 0,
left         : 0,
bottom       : 0,
right        : 0,
mapType      : Map.STANDARD_TYPE,
animate      : true,
regionFit    : true,
userLocation : false,
touchEnabled : true,
draggable    : true
});



view3.add(mapView);
view3.add(boton2);

var annotation = Map.createAnnotation({
latitude    : latitude,
longitude   : longitude,
title       : 'Selecciona Tu Ubicación',
draggable   : true
});

mapView.setAnnotations([annotation]);

var region = {
latitude       : latitude,
longitude      : longitude,
animate        : true,
latitudeDelta  : 0.005,
longitudeDelta : 0.005
};
mapView.setRegion(region);

mapView.addEventListener('pinchangedragstate', function(e) {
    Ti.API.info(longitude + ' - ' + latitude);
}); 
    }

});

}else{

alert('Por favor habilita los servicios de ubicación');
}

Thanks!


Solution

  • You're logging the variables you've set manually, that won't change automatically. You'll have to use the annotation to check the coordinates.

    If you look at the documentation for the event you'll see the annotation is also returned there.

    mapView.addEventListener('pinchangedragstate', function(e) {
        Ti.API.info(e.annotation.longitude + ' - ' + e.annotation.latitude);
    });