Jump to content

radial search (over Map Marker coords)


Spica
 Share

Recommended Posts

With lots of locations with coords generated with Map Marker I now want do do a radial search over the coords. Google gives an sql statement example:

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;

Source: https://developers.google.com/maps/articles/phpsqlsearch_v3#findnearsql

So now I wonder, what is the best way to do it within pw. Is it possible to build a selector or better do a sql select?

Link to comment
Share on other sites

ok. Read that already somewhen, but hoped to avoid that :-). Especially as it comes to a search over combinated fields with the geolocation. Would you then recommend a sql select with a join of tables or compare the simple raw sql result against another selector pw result?

Link to comment
Share on other sites

Hi Spica

At my company, I've needed the same functionality so I've been trying to work it into the module. It pretty much works, but I'm not sure if this is the best way to modify the fieldtype module to work like this with a 'virtual' field and multiple selectors - it seems like a bit of a hack. It will also order results by distance regardless.

https://github.com/CloudDataService/FieldtypeMapMarker/tree/develop

I apologise for the formatting/whitespace diff.

Example - assume the MapMarker field is called 'geo', I would search like this to get all 'place' pages where the map marker is within 30 miles of lat/lon 54.97,-1.62.

template=place, geo.unit=mi, geo.distance<30, geo.point_lat=54.97, geo.point_lng=-1.62

In the results, you can display the distance like this:

// Centre of the search area:
$lat = 54.97;
$lng = -1.62;

$results = $pages->find("template=place, geo.unit=mi, geo.distance<30, geo.point_lat={$lat}, geo.point_lng={$lng}");

foreach ($results as $result) {
    // Calculate distance between result's marker and our search centre
    $distance = round($result->geo->distance($lat, $lng, 'mi'));
    echo "<li>{$result->title} ($distance miles away)</li>";
}
?>

Hope it helps anyway.

  • Like 8
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...