-2

First of all, I'm not even sure if this is the best way to do it, but...

I have amassed a table of longitude and lattitude points for all zip codes in the US. What I want to be able to do is allow a user to choose a zip code, select a radius in Miles (5, 10, 20, 40, etc...) and the app will list all the users in that radius.

It obviously doesn't need to be incredibly accurate, but it's got to be close. I've been poking around looking for other ways to do this but I'm stumped, and I can't find a good example of using long/lat to do it.

If I could get something in C# that would work best. I'm not proficient at Java but I might be able to muddle through it if absolutely necessary.

EDIT:

My coordinates look like this:

CountryCode Zipcode Place   StateCode   Latitude    Longitude
US          95219   Stockton     CA        38.01    -121.3698
US          95220   Acampo       CA      38.2004    -121.2186
US          95227   Clements     CA      38.1929    -121.0811
US          95230   Farmington   CA      37.9945    -120.7926
US          95231   French Camp  CA       37.878    -121.2827
US          95234   Holt         CA      37.9344    -121.4261
US          95236   Linden       CA       38.032    -121.0493

This question is not a duplicate, the linked question is for a phone.

Johnny Bones
  • 8,271
  • 6
  • 39
  • 99
  • Be aware that this is verging on trivial if you can convert your lat/long into eastings / northings. – Adrian Wragg Jul 13 '15 at 13:34
  • @oppassum - That's for a phone, which I assume has the GeoCoordinates function built in? – Johnny Bones Jul 13 '15 at 13:38
  • Maybe I not understand correctly, but Your question is finding, let's say, all coordinates, in relation to the selected location, by some radius? – nelek Jul 13 '15 at 13:58
  • Somehow, suggested thread, is calculating distance between two lang/long but not finding all of them in selected radius. Maybe this link can help You : http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates – nelek Jul 13 '15 at 14:06
  • Well, my desired result is to find all users who live within a radius of you, given your zip code. Still not sure how finding distance is going to be efficient, I'm going to have to convert each user to find which are c within a given range, which sounds horribly inefficient. I'm thinking I have to do this in SQl Server. – Johnny Bones Jul 13 '15 at 14:42

1 Answers1

2

Following code generates distance between this instance of Wgs84Point and another. Distance is given assuming a perfectly spherical Earth and does not take into account Earth's irregular shape.

public class Wgs84Point
{
    const double MaxDegreesLongitude = 180;
    const double MinDegreesLongitude = -180;
    const double MaxDegreesLatitude = 90;
    const double MinDegreesLatitude = -90;

    readonly double _longitude;
    readonly double _latitude;

    public double Latitude { get { return _latitude; } }

    public double Longitude { get { return _longitude; } }

    public Wgs84Point(double longitude, double latitude)
    {
        if (longitude > MaxDegreesLongitude || longitude < MinDegreesLongitude)
            throw new ArgumentException("longitude");

        if (latitude > MaxDegreesLatitude || latitude < MinDegreesLatitude)
            throw new ArgumentException("latitude");

        _longitude = longitude;
        _latitude = latitude;
    }

    public Distance DistanceTo(Wgs84Point that)
    {
        if (that == null)
            throw new ArgumentNullException("that");

        if (this == that)
            return Distance.Zero;

        var dLat = DegreesToRadians(Latitude - that.Latitude);
        var dLon = DegreesToRadians(Longitude - that.Longitude);
        var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
            Math.Cos(DegreesToRadians(Latitude)) * 
            Math.Cos(DegreesToRadians(that.Latitude)) *
            Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
        var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
        var d = Distance.RadiusOfEarth.ToDouble() * c;
        return new Distance(d);
    }

    static double DegreesToRadians(double degrees)
    {
        return degrees * (Math.PI / 180);
    }
}
Matt
  • 1,598
  • 13
  • 21
  • What is the syntax to use this? Suppose, in my table I added to my edit above, I want to find the distance from Stockton to Holt. What syntax would I use? – Johnny Bones Jul 13 '15 at 13:49
  • Table? The [original version of] your question doesn't mention the persistence mechanism. You will have to construct C# classes using EF, DataReader or DataSet and use the above code from that point. Alternatively, look for a SQL implementation of the above code. – Matt Jul 13 '15 at 14:24