• Hi,

    My website has multiple (custom) posts types. Right now they’re activities, accommodations and restaurants but in the future there will be more. Every post has a custom field with the latitude/longitude. I also have a custom field with the full address.

    When I’m for example on a activity page I would like to show 5 nearby accommodations and 5 nearby restaurants within 10 kilometers from the location of the activity. And vice versa.

    Is this possible and if so, how should I do this? I would like to do this without a plugin if possible. Hopefully someone can get me started with this.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Hello edow my friend!

    Always the one with interesting coding challenges! ??
    Such a simple, reasonable desire, yet not so simple to code, but possible I think. It’s a problem if latitude and longitude are in the same field because you really need to query each dimension separately. A one time script to separate the values should be possible. OTOH there may be a solution where the two can stay together, but queries will not be nearly as efficient.

    It would be difficult to query for records within a 10 km circle, or 10 km by road, but fairly easy to get records contained inside a 20 km square centered on the location. Is that OK? You then mainly just need to determine how many fractions of a degree of longitude or latitude make up 10 km, which will give you an upper and lower range of each dimension. Then query for records that fall within those ranges.

    I don’t know how to determine that, but the information is available. The arc distance per kilometer is fairly constant for latitude, though it does vary some because the earth is not round. (It’s an approximate ellipsoid). The arc distance per kilometer for longitude varies quite a bit depending on latitude. It approaches infinite near the poles and is relatively small at the equator.

    Depending on the actual latitude range of all your data, you may be able to take a happy middle distance and use it everywhere and call it close enough. For larger latitude ranges, you will need to consider it in your calculations.

    How many records are we talking about here? If it’s huge, it may be worth developing a proper SQL query to separate the values. If more finite, a simple PHP loop stepping through the records may suffice. I think it’s important to separate the values. The code to break them apart on the fly for every query frightens me!

    What is the maximum latitude range of all of your records? Will that grow in the future? Your answer will determine if you can use a single conversion constant for longitude or if latitude must be considered. What sort of East/West error in distance are you willing to tolerate? What if that 20 km square was really 21×20? No problem? What about 22×20? Where should I stop?

    Thread Starter edow

    (@edow)

    Hi bcworkz! The easy questions I try to manage myself ?? It seems that you’re the only one responding to my complex questions. Thank you for that!

    I hear it’s not that simple as it seems. Right now there’re about 300 posts, but within a couple of years that should be thousands. For now I can change the lat/lon to two seperate custom fields if necessary, but I understand there’re more problems.

    A 20 km square is also fine. It doesn’t have to be that precise, but it would be fine if the exact distance is shown. For example:

    Nearby accommodations:

    • Hotel ABC 2,1 km
    • Hotel Stay2Day 8,6 km
    • B&B 1234 9,5 km
    • Motel OK 11,9 km
    • Hotel Go 19,1 km

    And then a similar list for restaurants/activities.

    So, it don’t have to be very precise, but the precise distance have to be shown (maybe not after the comma). In the future ther’re hopefully a lot of posts (probably high in the thousands), so I’m looking for a solid function which is not asking to much from the server.

    Maybe I also should mention that I want this function in the sidebar. I noticed that the sidebar sometimes works different than the actual post.

    What do you mean with What is the maximum latitude range of all of your records? If I think I know what you mean it is than the answer is that the lat/lon can be all over the world. Right now only in Europe, but later I hope/think it will be outside Europe also.

    Hopefully it’s doable. I understand it’s not easy. Where should I start? Hopefully you can show me the right way so I can try to achieve this.

    Moderator bcworkz

    (@bcworkz)

    The square is ideal mainly for querying for results. Once you have the query results, the exact distance can be calculated to whatever precision is available from the original lat/lon data.

    Geodetic calculations like this are quite cumbersome for us mere mortals, but once encoded, it is fairly trivial as far as server load goes, I see no issue as long as the returned results are a manageable size.

    Since you envision world wide coverage, you will of course need to consider latitude in determining the longitudinal spread for the initial query. Not a big deal, just a bit more work.

    You will need to find the formulas used to convert from km distance to the arc distance in degrees lat/lon. An example: For simplicity, let’s say 10 km is found to be equal to 0,1 degrees in both lat and lon (it’s not of course) at 52,3 deg N and 5,3 deg E (a bit East of Amsterdam actually). You would then query for records where the lat falls between 52,1 and 52,4 and lon falls between 5,1 and 5,4, which would be the bounds of a 20 km square. See why it’s very useful to have the lat separate from lon values?

    You can then plug in the actual lat/lon of the center point and that of any found record into yet another formula and get the actual straight line (great circle arc actually) distance. Accurate to the mm if your lat/lon data is that accurate ??

    The most accurate calculation is probably via Vincenty’s Formulae. Unless you’re a mathematics major, that link is going to look very scary! For your needs, less accurate methods should suffice. Take a look at Geographical Distance. You may want to skip past the planar solutions, they involve a lot of distortion. Spherical surface formulae should suit you fine.

    You would first need to invert the problem because as illustrated it solves for distance, you have that, you want to solve for lat/lon. Of course, coding all of this is prone to error. Fortunately, there are online calculators with which you can check your work against. Just be sure the calculator you use uses the same underlying formulae.

    If you can’t be bothered with all this sine and cosine stuff, another approach would be to use an online calculator to determine the degree longitude per kilometer for a range of latitudes and compile a look up table that the code can use to get a conversion constant for any particular latitude range. You will only need a few values for degree latitude per kilometer, perhaps just one value will work for you. Get a value at 0 deg latitude and 70 deg latitude to see how much variation we’re talking about.

    For background information about geometric modeling of the earth, try reading Figure of the Earth. Much less mathematics than the previous links ??

    Thread Starter edow

    (@edow)

    Thank you for your comprehensive answer! I have to think hard about this, because it sounds like a lot of work which (I think) I can’t do myself. As you mentioned it could also contain a lot of errors, so it have to be a solid code.

    I’m going to read the websites you mentioned and think hard about this. Again thank you and I’ll come back on this topic soon.

    bcworkz, I don’t intend to hijack this thread, but we have a project that might be well suited for you on a consultancy basis. If interested, please contact me (Kelley) at [email protected]. Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to show nearby locations with latitude and longitude’ is closed to new replies.