Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
hi every one
i want to find Distance in Km between two address
i want to do this using c# application i have two variables that contain the addresses,
i.e first variable "Fromaddress" contain 1st address abd second variable "Toaddress" contain the second address now i want to calculate the disstance from "
Fromaddress
" to
Toaddress
using c# winform application.

What I have tried:

i have no idea how can i do this but i know little bit about this, this is done to sending both address to google map from c# application and google map returns the Distance
Posted
Updated 16-Jul-19 11:50am
Comments
Patrice T 16-Jul-19 17:38pm    
Ans asking Google was too much effort ?

You are obviously aware of Google, so why didn't you use it?
A trivial google using part of your question as the search string took me straight here: https://www.aspforums.net/Threads/515185/Calculate-Distance-Duration-between-two-locations-in-Google-Maps-API-using-C-and-VBNet-in-ASPNet/[^]

Please, in future at least do a basic google before posting here rather than wasting your time and ours ...
 
Share this answer
 
Comments
ReturnVoid 8-Apr-20 16:16pm    
I had been googling for at least the last hour or so. And so far this link was the most useful, but the condescending tone isn't. If it was such a waste of time, why bother posting at all, clearly you have to much time on your hands. Some times using google & knowing what key words to search for isn't always so obvious nor trivial, especially for users new to a field.
Solution 2 below is helpful even more, as it was done without a condescending tone & actually provides some insight i.e. the need to convert addresses to coordinates, which I hadn't thought of.
Next time think about why you're actually (actively?) on this site.... so please, in future at least do a basic google about post etiquette before posting here rather than wasting your time and....oops :P.
OriginalGriff 9-Apr-20 2:15am    
I really don't care if you find my communications with a different member condescending: it was not addressed to you.
As it happens - not that it matters - the OP is a Help Vampire: he is stubbornly against learning anything if he can get others to do it for him. And when you go to the "effort" of posting a question that can be simply answered by googling the text you put in the question, you do perhaps need a reminder that wasting the time of busy volunteers is not a good idea ...
There are 2 portions to this; the conversion of addresses into coordinates and then calculating the distance between coordinate sets.

The conversion of addresses to coordinates is called Geocoding, and this is where Google comes in; as they have an API. There is plenty of existing documentation on this; here's the crux of what you need to know:
Get Started | Geocoding API | Google Developers[^]

The second portion is determining the distance between the points; and where all them algebra, geometry, and trigonometry skills you forgot get relearned.
For the greatest precision especially when longer distances are involved, you will want to use the Haversine formula - Wikipedia[^]

If absolute precision is not required and you are within 100 km or so, you could implement some simpler math to get the differences in latitude and longitude, and then pump this through the Pythagorean Theorem to get a pretty good distance estimate

Last portion is conversions between (decimal) degrees and kilometers, which can be helpful
How big is a degree of Latitude Longitude[^]
 
Share this answer
 
Comments
BillWoodruff 17-Jul-19 2:39am    
does someone who has not tried anything deserve this ?
F-ES Sitecore 17-Jul-19 3:57am    
Just to add, there is now a class that does all the maths for you

System.Device.Location.GeoCoordinate.GetDistanceTo()
ReturnVoid 8-Apr-20 16:16pm    
Thanks for the additional comments & solution!
MadMyche 8-Apr-20 18:35pm    
You are welcome. If this helped you out, please consider using the stars at the top of the solution to give a rating to this answer

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