# distance between two coordinates using geocoder

SDMember ✭✭

I get coordinates for to address using geocoder and i want to calculate the distance between the two address in kilometers

• SDMember ✭✭

thank you too much for help, Unfortunately I dont know how do to that because I am still new in xamarin, If anyone help me to edit my code correctly its a great help, thank again

• USMember ✭✭
edited September 2017

Google API can provide it. Or you can compute it app side with less precision. With a such library or another one PCL compatible if it's not : https://github.com/karl-sjogren/Shorthand.Geodesy

• INMember ✭✭✭✭✭

Follow the MSDN for C# code

Sample Code can be

```var FromLocA = new GeoCoordinate(sLatitude, sLongitude);
var ToLocB = new GeoCoordinate(eLatitude, eLongitude);
return FromLocA.GetDistanceTo(ToLocB);
```

---- Note: This returns distance in meters, so Km is a easy conversion.

Hope it helps.

N Baua

• SDMember ✭✭

thank you for help, I using this code to get the coordinates in very simple way, how to put your code correctly in my code to get dustance

async void OnGeocodeButtonClicked(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(inputEntry.Text))
{
foreach (var position in approximateLocations)
{
geocodedOutputLabel.Text += position.Latitude + ", " + position.Longitude + "\n";
}
if (!string.IsNullOrWhiteSpace(inputEntry2.Text))
{
foreach (var position in approximateLocations2)
{
geocodedOutputLabel2.Text += position.Latitude + ", " + position.Longitude + "\n";
}
I am new in xamarin
thanks

• INMember ✭✭✭✭✭
edited September 2017

You can not just relate `approximateLocations` and `approximateLocations2` coordinates to one-on-one basis.

There can be multiple or No values for either of this locations. so this would not be a right way to find out the distance in my opinion.

What you can do is.
Get the `approximateLocations` and `approximateLocations2` as you are doing now, but list them in 2 different picker controls or something. (also handle the case, if there are no suggested locations).

Now if user selects the location1 from `approximateLocations` and location2 from `approximateLocations2`, your code is simple as pie

var location1 = new GeoCoordinate(sLatitude, sLongitude);
var location2 = new GeoCoordinate(eLatitude, eLongitude);
return location1.GetDistanceTo(ToLocB);

• USMember ✭✭
edited September 2017

GetPositionsForAddressAsync returns a Position struct with a Longitude and Latitude property. So convert it into GeoCoordinate object, passing long and lat to constructor :
https://developer.xamarin.com/api/type/Xamarin.Forms.Maps.Position/
https://msdn.microsoft.com/en-us/library/ee426026.aspx

• SDMember ✭✭

thank you too much for help, Unfortunately I dont know how do to that because I am still new in xamarin, If anyone help me to edit my code correctly its a great help, thank again

• USMember ✭✭

It's not a Xamarin question, but a C# one, and object oriented programming.
The anwser is something like

```var location1 = new GeoCoordinate(approximateLocations.Latitude, approximateLocations.Longitude);
var location2 = new GeoCoordinate(approximateLocations2.Latitude, approximateLocations2.Longitude);
return location1.GetDistanceTo(location2);
```
• SDMember ✭✭

thank you @Charlélie A and I am sorry about repeated question, I am trying your code but not working or I dont know how. thank for your patient

• USMember ✭✭

Paste the code you have and give details about what isn't working, the error you have. I can't guess

• Member

why cannot I import GeoCoordinate class?