Spica Posted January 4, 2016 Share Posted January 4, 2016 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 More sharing options...
LostKobrakai Posted January 4, 2016 Share Posted January 4, 2016 The current selector syntax does not support any kind of parameters for comparisons, but you can use raw sql like described here: https://processwire.com/talk/topic/17-functionsmethods-to-access-the-db/ Link to comment Share on other sites More sharing options...
Spica Posted January 4, 2016 Author Share Posted January 4, 2016 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 More sharing options...
Craig Posted January 4, 2016 Share Posted January 4, 2016 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. 8 Link to comment Share on other sites More sharing options...
Spica Posted January 4, 2016 Author Share Posted January 4, 2016 Hi Craig a Rodway, that sounds good. Just had a first glance on your modifications, not sure if I got it all. I will give it a try next days and return. Link to comment Share on other sites More sharing options...
Spica Posted January 6, 2016 Author Share Posted January 6, 2016 Implemented it and works fine. Thanks a lot. Only the 'bicyclingLayer' => false, definition is missing in the headdefinitions Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now