Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

The array always returns de last value

$
0
0

Sorry for me little English. this is the code i am working:

function distance(obj) { var R = 6371; // km var dLat = (obj.lat2 - obj.lat1) * Math.PI / 180; var dLon = (obj.lon2 - obj.lon1) * Math.PI / 180; var lat1 = obj.lat1 * Math.PI / 180; var lat2 = obj.lat2 * Math.PI / 180;

var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
var d = R * c;
var m = d * 0.621371;
return {
    km: d,
    m: m
};

}

var location = { lat: 39.42495, lon: -0.47985, title: 'Home' }; var points = [ { lat: 39.45723, lon: -0.34712, title: 'Eci' }, { lat: 53.7500, lon: -0.3600, title: 'Hull' }, { lat: 52.2100, lon: 0.1300, title: 'Cambridge' }, { lat: 51.9000, lon: -0.2000, title: 'Stevenage' }, { lat: 51.1092, lon: -0.1872, title: 'Crawley' } ]; var filtered = [], intPoint = 0, intPoints = points.length, dist = {}, point = {}; for (intPoint = 0; intPoint < intPoints; intPoint = intPoint + 1) { point = points[intPoint]; dist = distance({ lat1: location.lat, lon1: location.lon, lat2: point.lat, lon2: point.lon }); if (dist.km < 20) { point.distance = dist.km; filtered.push(point); // gpsLabel.text = point; } } var win = Ti.UI.createWindow({ backgroundColor: '#222', layout: 'vertical' });

var gpsLabel = Ti.UI.createLabel({ text:filtered[0] , color: '#fff', font: { fontSize: '20dp', fontWeight: 'normal'
}, top: '30dp' }); Ti.API.info('points'+ JSON.stringify(filtered)); win.add(gpsLabel); win.open();

the array always returns the distance from de last item, I need the array return any value that satisfies the condition. I put in the text from gpsLabel, the first filtered item from de new array. but the value is blank. I am sure that i am doing something wrong, but i don't know what is. Please Help me.


Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>