3

I have a database table of zipcodes with their Lat/Longs. I'm trying to find some code that shows a query that takes a zipcode and x miles and then return set of results that include all the zipcodes that are within that radius (precision is not very important - as long as it's close).

Can this be done with a Linq to SQL query so I don't have to use a Stored Procedure?

RichC
  • 7,829
  • 21
  • 85
  • 149
  • Possible duplicate of [Find closest location with longitude and latitude](http://stackoverflow.com/questions/12835851/find-closest-location-with-longitude-and-latitude) – StayOnTarget May 18 '17 at 18:51
  • @DaveInCaz If only I had had a time machine 7 years ago when I asked this question!! thanks for the down-vote. =\ – RichC May 18 '17 at 23:34
  • 1
    I didn't downvote this question, but I'm sorry if someone did because I think it is a good question. But coalescing duplicates seems to have a lot of value, even if they've been around for a while. – StayOnTarget May 18 '17 at 23:55
  • wouldn't their question be a duplicate of my question since I asked it first? – RichC May 19 '17 at 12:19
  • Hmm I'm not confident if that is the criteria for selecting a duplicate or not. I thought the moderator tries to determine which is the more "canonical" question. In this instance since the other one happened to have more answers etc. I thought perhaps it was the more logical one to leave open... but that certainly is only one way to look at it. – StayOnTarget May 19 '17 at 12:24
  • I just found this: https://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled . – StayOnTarget May 19 '17 at 12:25
  • Yes, I agree that the one with the "more" correct answer should have the focus regardless of date but "Possible duplicate..." makes it sound like my question did something wrong (and probably what got me the unwarranted down-vote from someone). I think it's just a verbiage issue. "Possible better answer over here...". I dunno - just my two cents. – RichC May 19 '17 at 15:55
  • 1
    Sorry if it gave that impression, that was not my intent. Also, the "possible duplicate of..." wording is not my own - SO uses that wording itself when I marked this as a possible duplicate. – StayOnTarget May 19 '17 at 15:56
  • Yeah - I get that and wasn't personal. Just my observation. :) – RichC May 19 '17 at 15:58

1 Answers1

1

I figured it out and it actually wasn't all that hard once i found the equation.

Public Function SearchStudents(ByVal SearchZip As String, ByVal Miles As Double) As IEnumerable(Of Student)
                Dim dc As New IMDataContext()

                Dim lat As Double
                Dim lng As Double
                Dim maxlat As Double
                Dim minlat As Double
                Dim maxlng As Double
                Dim minlng As Double

                Dim zip As ZipCode = (From z In dc.ZipCodes Where z.ZipCode = SearchZip).SingleOrDefault()

                lat = zip.Latitude
                lng = zip.Longitude

                maxlat = lat + Miles / 69.17
                minlat = lat - (maxlat - lat)
                maxlng = lng + Miles / (Math.Cos(minlat * Math.PI / 180) * 69.17)
                minlng = lng - (maxlng - lng)

                Dim ziplist = From z In dc.ZipCodes Where z.Latitude >= minlat _
                       And z.Latitude <= maxlat _
                       And z.Longitude >= minlng _
                       And z.Longitude <= maxlng Select z.ZipCode

                Return From i In dc.Students Where ziplist.Contains(i.Zip)
            End Function
RichC
  • 7,829
  • 21
  • 85
  • 149
  • 1
    That checks if it is within a square, not a radius. You need to compare the distance between lat/long pairs to get a radius... – KristoferA Nov 01 '09 at 10:57
  • @KristoferA - I personally didn't really care as long as it was pretty close but if you have a better formula that fits this function, I certainly wouldn't mind the increased accuracy. – RichC Nov 02 '09 at 13:30
  • So does this code still make a square or was it adjusted to be a radius? – Sealer_05 Feb 19 '13 at 20:10
  • I believe it's still a square – RichC Feb 20 '13 at 01:18