3 Replies Latest reply on Jun 14, 2007 9:07 PM by Newsgroup_User

    Calculating distance using city/state input fields

    ssailer Level 1
      I have a form with a zip code field and a city and state field on it. The user can either enter a zip code to perform a radius search, or enter a city/state combination.

      I have a formula for calculating the distance from a given zip code, but how does this translate into a city/state search when a city/state combination could potentially have tons of different latitude and longitude coordinates? Which coordinate do you use as a starting point?

      This formula works great for zip codes, but how can I make this work for a city/state combo?

      Distance = SQR((69.1*(Zip2Lat-Zip1Lat))*(69.1*(Zip2Lat-Zip1Lat))+(69.1*(Zip2Lon-Zip1Lon)*COS(Zip1Lat /57.3)*(69.1*(Zip2Lon-Zip1Lon)*COS(Zip1Lat/57.3))))

      Has anyone solved this problem?
        • 1. Re: Calculating distance using city/state input fields
          Level 7
          ssailer wrote:
          > I have a formula for calculating the distance from a given zip code, but how
          > does this translate into a city/state search when a city/state combination

          it doesn't. if you need city-to-city distance calculations & all you have are
          zip code centroid coords, then you'll have to pick a centroid position (single
          point) to represent the whole city.

          i guess a more detailed answer depends on what data you have & how you need to
          calculate distances (crow-flies, road travel, etc.).

          • 2. Re: Calculating distance using city/state input fields
            ssailer Level 1
            I was using one latitude and logitude coordinate to base my calculations from (taking an average), but the results weren't quite as accurate as the client wanted. I've been playing around with it quite a bit, and I think I have lessened the gap enough to make him happy. Thanks!
            • 3. Re: Calculating distance using city/state input fields
              Level 7
              ssailer wrote:
              > I was using one latitude and logitude coordinate to base my calculations from
              > (taking an average), but the results weren't quite as accurate as the client

              unless you do some kind of "spatial" average, they won't be & if you do they
              still might not be based on the city shape, etc. in any case, you'd need some
              GIS tools.

              btw i just remembered the USGS's gazetteer http://geonames.usgs.gov/ you might
              have to do some data massaging but you probably won't miss anything using it.