Answered by:
nearest latitude and longitude

I need to have this function to calculate which latitude or longitude is nearest to the user.
how do I do that?
1002614D
Wednesday, August 15, 2012 2:03 AM
Question
Answers

Hi,
Based on your code, I don't see you call getGeopositionAsync. Your code seems to be a function calcuating the distance between two geo locations. Is there any difficulties to calculate the result? You can also try to make it easy. Ignore altitudes, and treat the earth as a sphere (ignore polar flatterning). Then you can use the great circle distance formular to calculate the distance: http://en.wikipedia.org/wiki/Greatcircle_distance. More specifically, the Haversine formula (which uses great circle distance formular under the hook) helps to calculate the distance between two points on the earth's surface. It is not needed to learn the math details. Please simply use the Haversine formula.
Below is some quick JavaScript code to calculate the approximate distance between two points
// haver sine function.
function haversine(angle) {
return Math.pow(Math.sin(angle / 2), 2);
}// Convert the value to radius.
function toRadius(value) {
return value * Math.PI / 180;
}function distance(lat1, long1, lat2, long2) {
varR = 6371; // km (change this constant to get miles)
var dLat = toRadius(lat2  lat1) ;
var dLon = toRadius(long2  long1);
var haversined = haversine(dLat) + Math.cos(toRadius(lat1)) * Math.cos(toRadius(lat2)) * haversine(dLon);
var result = Math.acos(1  2 * haversined) * varR;return result;
}Note the above code treats earth as a sphere and ignores altitudes. If you want a more presice calculation, you may want to use a cloud service, such as Bing Maps API: http://msdn.microsoft.com/enus/library/ff701717.aspx.
Best Regards,
Ming Xu.
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework Marked as answer by Dino He Friday, September 14, 2012 9:20 AM
Thursday, August 30, 2012 11:21 AMModerator
All replies

Hi,
I think you can achieve that using the Geolocator class and it's getGeopositionAsync() method. Reference http://msdn.microsoft.com/enUS/library/windows/apps/hh973537
 Proposed as answer by Jeff SandersMicrosoft employee, Moderator Wednesday, August 15, 2012 6:31 PM
Wednesday, August 15, 2012 6:08 PMModerator 
I have written this code.
but I cant get the value out.
var lon2 = 55;
var lat2 = 33;
function
distance(lat, long, lat2, lon2) {
varR = 6371; // km (change this constant to get miles)
vardLat = (lat2  lat) * Math.PI / 180;
vardLon = (lon2  long) * Math.PI / 180;
vara = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
Math.sin(dLon/2) * Math.sin(dLon/2);
varc = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1a));
vard = R * c;
if(d>1) returnMath.round(d)+"km";
elseif(d<=1) returnMath.round(d*1000)+"m";
returnd;
// var outputArea1 = document.getElementById("lat2");
//var outputArea1 = document.getElementById("lon2");
}
1002614D
Thursday, August 16, 2012 6:03 AM 
I don't know what you mean by "can't get the data out". Can you be more specific?
Jeff Sanders (MSFT)
Friday, August 17, 2012 1:25 PMModerator 
Hi,
Based on your code, I don't see you call getGeopositionAsync. Your code seems to be a function calcuating the distance between two geo locations. Is there any difficulties to calculate the result? You can also try to make it easy. Ignore altitudes, and treat the earth as a sphere (ignore polar flatterning). Then you can use the great circle distance formular to calculate the distance: http://en.wikipedia.org/wiki/Greatcircle_distance. More specifically, the Haversine formula (which uses great circle distance formular under the hook) helps to calculate the distance between two points on the earth's surface. It is not needed to learn the math details. Please simply use the Haversine formula.
Below is some quick JavaScript code to calculate the approximate distance between two points
// haver sine function.
function haversine(angle) {
return Math.pow(Math.sin(angle / 2), 2);
}// Convert the value to radius.
function toRadius(value) {
return value * Math.PI / 180;
}function distance(lat1, long1, lat2, long2) {
varR = 6371; // km (change this constant to get miles)
var dLat = toRadius(lat2  lat1) ;
var dLon = toRadius(long2  long1);
var haversined = haversine(dLat) + Math.cos(toRadius(lat1)) * Math.cos(toRadius(lat2)) * haversine(dLon);
var result = Math.acos(1  2 * haversined) * varR;return result;
}Note the above code treats earth as a sphere and ignores altitudes. If you want a more presice calculation, you may want to use a cloud service, such as Bing Maps API: http://msdn.microsoft.com/enus/library/ff701717.aspx.
Best Regards,
Ming Xu.
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework Marked as answer by Dino He Friday, September 14, 2012 9:20 AM
Thursday, August 30, 2012 11:21 AMModerator