Created
August 31, 2015 18:55
-
-
Save alamsal/044b9a1480ab9b37fa7b to your computer and use it in GitHub Desktop.
Find out location with in X radius from the user's current location
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: 51.5171, | |
lon: -0.1062, | |
title: 'London' | |
}; | |
var points = [ | |
{ lat: 52.4800, lon: -1.9100, title: 'Birmingham' }, | |
{ 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 < 100) { | |
point.distance = dist.km; | |
filtered.push(point); | |
} | |
} | |
Ti.API.info('points: ' + JSON.stringify(filtered)); | |
//https://developer.appcelerator.com/question/149231/find-out-location-in-10-km-radius-from-user-current-location |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment