Search code examples
c#web-servicesvirtual-earthreverse-geocoding

How to get the closest significant population centre from a latitude and longitude?


I'm currently trying to reverse geocode a series of lat/long co-ordinates using the Virtual Earth/Bing Maps web services. Whilst I can successfully retrieve an address for the positions I also need to be able to retrieve the closest significant population centre for the position so I can display a heading and distance to the centre of the nearest town/city/metropolis etc. This is for cases where the location is travelling between locations e.g. on a highway/motorway.

Has anyone out there got any ideas how to do this as I've been banging my head against it for a few days now and I've gotten nowhere!

Cheers in advance...


Solution

  • I think it is safe to assume that the nearest city is always quite close compared with the size of the Earth, so you can use a simple pythagoras triangle.

    Suppose you are at (lat0, long0) and a trial city is at (lat1, long1).

    Horizontal (EW) distance is roughly

    d_ew = (long1 - long0) * cos(lat0)
    

    This is multiplied by cos(lat0) to account for longitude lines getting closer together at high latitude.

    Vertical (NS) distance is easier

    d_ns = (lat1 - lat0)
    

    So the distance between the two points is

    d = sqrt(d_ew * d_ew + d_ns * d_ns)
    

    You can refine this method for more exacting tasks, but this should be good enough for the nearest city.

    For comparing distances, it will be fine to compare d squared, which means you can omit the sqrt operation.