The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

# Math/Code to find nearest location from current location?

• ### Question

• Hi,
I'm currently working on a wp7 app that shows you the nearest lets say mall from your current location. Is there a way to find it using math,logic? I've created pushpins with cordinates hard-coded to PhoneAppplicationPage_Loaded.

I was thinking of using my current location (watcher.position.location) to maybe subtract every pushpin cordinates I have and the least would be shown on the screen to the user as the nearest mall.

Is there such a method? lol.

Wednesday, November 16, 2011 9:13 AM

### All replies

• No method, you will need to measure distance between two GPS co-ordinates.

Just bing search for GPS distance, should find some.

Here is one example:

http://www.joshchristie.com/articles/GPSDistance/
Wednesday, November 16, 2011 9:31 AM
• No method, you will need to measure distance between two GPS co-ordinates.

Just bing search for GPS distance, should find some.

Here is one example:

http://www.joshchristie.com/articles/GPSDistance/

I'm sorry, how exactly should I do it? I have pushpins hardcoded on my page load like lets say:

Pushpin pin = new Pushpin();

pin.Location =

new GeoCoordinate(1.244141, 103.829094);

I just want to use my current location to find the nearest pushpin location and show it to the user. I have a hundred of such pushpins? Where do I begin?

Wednesday, November 16, 2011 9:54 AM
• Use the GeoCoordinate.GetDistanceTo(other) method.  Iterate through your pushpins.  Choose the one with smallest value returned by GetDistanceTo().
Wednesday, November 16, 2011 11:04 AM
• Use the GeoCoordinate.GetDistanceTo(other) method.  Iterate through your pushpins.  Choose the one with smallest value returned by GetDistanceTo().

Sorry, I'm pretty new at this and not sure how I should go about doing this..Is it something like this?

public double GetDistanceTo(

var myLocation = watcher.position.location;
var yourLocation = pin1.location to pin100.location; (????)
double distance = myLocation.GetDistanceTo(yourLocation);

I know this is pretty screwed up, please do help!

Thursday, November 17, 2011 1:08 AM
• That's pretty much what you want.

GeoCoordinate closest = null;
double closestDistance = double.MaxValue;
for (int i = 0; i < 100; ++i)
{
double dist = myLocation.GetDistanceTo(pin[i].Location);
if (dist < closestDistance)
{
closest = pin[i].Location;
closestDistance = dist;
}
}

Thursday, November 17, 2011 1:17 AM
• That's pretty much what you want.

GeoCoordinate closest = null;
double closestDistance = double.MaxValue;
for (int i = 0; i < 100; ++i)
{
double dist = myLocation.GetDistanceTo(pin[i].Location);
if (dist < closestDistance)
{
closest = pin[i].Location;
closestDistance = dist;
}
}

Hi KromeStudios, This is what I did..

private

{

GeoCoordinate currentlocation = watcher.Position.Location;

GeoCoordinate closest = null;

double closestDistance = double.MaxValue;

for (int i = 0; i < 50; ++i)

{

double dist = currentlocation.GetDistanceTo(pin[i].Location);

if (dist < closestDistance)

{

closest = pin[i].Location;

closestDistance = dist;

}
}

}

And it shows an error(Error 1 Cannot apply indexing with [] to an expression of type 'Microsoft.Phone.Controls.Maps.Pushpin' )? What should i do?

Thursday, November 17, 2011 3:37 AM
• It was sample code.  Intended to guide you towards your own solution.  It was shown as an example of how to iterate through 100 pins, but you need to have the pins in a List or array or some sort.  I do not know how you are storing your 100 pins.
Thursday, November 17, 2011 3:43 AM
• How do I store it in a list or array if I'm storing it like this...

Pushpin pin = new Pushpin();

pin.Location = new GeoCoordinate(1.244141, 103.829094);

Pushpin pin1 = new Pushpin();

pin1.Location = new GeoCoordinate(1.26999, 103.829824);

Thursday, November 17, 2011 3:50 AM
• Map.Children is a UIElementCollection, so you can that instead of pin in the loop, e.g. Map.Children[i].

GeoCoordinate closest = null;
double closestDistance = double.MaxValue;
foreach (UIElement element in Map.Children)
{
if (element is Pushpin)
{
Pushpin pin = (Pushpin)element;
double dist = myLocation.GetDistanceTo(pin.Location);
if (dist < closestDistance)
{
closest = pin.Location;
closestDistance = dist;
}
}
}

Thursday, November 17, 2011 4:14 AM
• Map.Children is a UIElementCollection, so you can that instead of pin in the loop, e.g. Map.Children[i].

GeoCoordinate closest = null;
double closestDistance = double.MaxValue;
foreach (UIElement element in Map.Children)
{
if (element is Pushpin)
{
Pushpin pin = (Pushpin)element;
double dist = myLocation.GetDistanceTo(pin.Location);
if (dist < closestDistance)
{
closest = pin.Location;
closestDistance = dist;
}
}
}

I did this...

private void button1_Click(object sender, RoutedEventArgs e)

{

GeoCoordinate currentLocation = watcher.Position.Location;

GeoCoordinate closest = null;

double closestDistance = double.MaxValue;

foreach (UIElement element in Map.Children)

{

if (element is Pushpin)

{

Pushpin pin = (Pushpin)element;

double dist = currentLocation.GetDistanceTo(pin.Location);

if (dist < closestDistance)

{

closest = pin.Location;

closestDistance = dist;

loc.StatusChanged += loc_StatusChanged;

}

else

{

new Uri("/MainPage.xaml"));

}

}

}

Doesn't seem to do what I was trying to achieve. Sorry I'm pretty new at this. I want it zoom to the location nearest to my current location after i click button1.

Thursday, November 17, 2011 5:31 AM
• Why is the Navigate call inside the foreach loop?  That will just make the app navigate back to the main page on the first pushpin that is further away than the previous pushpin.

After the foreach loop, closest will contain the coordinates of the Pushpin closest to the user's current location.  Then you use those coordinates to zoom the map.
Thursday, November 17, 2011 5:49 AM
• Why is the Navigate call inside the foreach loop?  That will just make the app navigate back to the main page on the first pushpin that is further away than the previous pushpin.

After the foreach loop, closest will contain the coordinates of the Pushpin closest to the user's current location.  Then you use those coordinates to zoom the map.

private

void button1_Click(object sender, RoutedEventArgs e)

{

GeoCoordinate currentLocation = watcher.Position.Location;

GeoCoordinate closest = null;

double closestDistance = double.MaxValue;

foreach (UIElement element in Map.Children)

{

if (element is Pushpin)

{

Pushpin pin = (Pushpin)element;

double dist = currentLocation.GetDistanceTo(pin.Location);

if (dist < closestDistance)

{

closest = pin.Location;

closestDistance = dist;

loc.StatusChanged += loc_StatusChanged;

}

Map.Center= closest;

}

}

}

Am I right with this?

Thursday, November 17, 2011 9:09 AM
• Try:
 private void button1_Click(object sender, RoutedEventArgs e) { GeoCoordinate currentLocation = watcher.Position.Location; GeoCoordinate closest = null; double closestDistance = double.MaxValue; foreach (UIElement element in Map.Children) { if (element is Pushpin) { Pushpin pin = (Pushpin)element; double dist = currentLocation.GetDistanceTo(pin.Location); if (dist < closestDistance) { closest = pin.Location; closestDistance = dist; //loc.StatusChanged += loc_StatusChanged; } } } Map.Center= closest; }

I am not sure what "loc.StatusChanged" refers to, but it is unlikely you would want to do this each time you find a closer point.

You only want to change the map center one time to the closest point.  Your code would have had you centering the map every time you looked at a point.

Thursday, November 17, 2011 10:15 AM