doublesecretagency / craft-googlemaps

Google Maps plugin for Craft CMS - Maps in minutes. Powered by the Google Maps API.
https://plugins.doublesecretagency.com/google-maps/
Other
10 stars 8 forks source link

Internal proximity search #94

Closed EmptyStreets closed 10 months ago

EmptyStreets commented 11 months ago

Hello,

If I read correctly I can avoid relying on Google maps api while performing an internal proximity search if the starting point and the desired entries have geo coordinates defined.

What would be the working snippet for that?

Channels

Default radius of 50 miles.

Locations: Screenshot_20230805-230755_Samsung_Internet

Labs: Screenshot_20230805-230947_Samsung_Internet

Thanks.

lindseydiloreto commented 10 months ago

Here's a snippet which covers the basics... I haven't tested it, but I'm 95% confident it will work.

This is adapted from the "Simple Example" shown on the Proximity Search page.

{# Get a "Location" entry #}
{% set entry = craft.entries.section('locations').one() %}

{# Use a set of coordinates as the search target #}
{% set target = {
    'lat': entry.lat,
    'lng': entry.lng
} %}

{# Configure the proximity search #}
{% set options = {
    'target': target,
    'range': 50
} %}

{# Find nearby "Labs" #}
{% set entries = craft.entries
    .section('labs')
    .myAddressField(options)
    .orderBy('distance')
    .all() %}

And here are a few links which should tie it all together...

Hope that helps! 🙂