Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need write a asp.net web application in this need to find other nearby places within 5 km distance based on the latitude and longitude of the specified place. I have latitude and longitude of some places in my database. I need code to calculate the distance between two points (specifying their latitude/longitude


regards
Posted
Updated 14-Jun-10 21:12pm
v4
Comments
Ankur\m/ 14-Jun-10 7:16am    
Your requirement is to find the places within a radius of 5 km. You also mentioned that you have found the code for this. So what's the problem?
Sandeep Mewara 14-Jun-10 8:36am    
Problem is he found 'some' code. You need to complete it! Is so?

pradeep kumar patwari wrote:
I found some code to calculate the distance between two points (specifying their latitude/longitude


You wanted something, you even got a code. Now what?

Look here of how to post a question: http://www.codeproject.com/script/Answers/Post.aspx?new=question[^]

Until unless you wont tell us what is your issue, how are we suppose to know and answer them?
 
Share this answer
 
C#
using System;

private double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
  double theta = lon1 - lon2;
  double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));
  dist = Math.Acos(dist);
  dist = rad2deg(dist);
  dist = dist * 60 * 1.1515;
  if (unit == 'K') {
    dist = dist * 1.609344;
  } else if (unit == 'N') {
    dist = dist * 0.8684;
    }
  return (dist);
}

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//::  This function converts decimal degrees to radians             :::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
private double deg2rad(double deg) {
  return (deg * Math.PI / 180.0);
}

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//::  This function converts radians to decimal degrees             :::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
private double rad2deg(double rad) {
  return (rad / Math.PI * 180.0);
}

Console.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, "M"));
Console.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, "K"));
Console.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, "N"));
 
Share this answer
 
Comments
BobJanova 31-May-11 10:42am    
Can you provide a derivation of this formula?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900