1

I am trying to complete an advanced search query with Smart Map, but I can't seem to quite get it. I tried to merge several URL parameters, and they seem to work fine except when it comes down to Smart Maps's query.

I followed along with the example here, but it's not fully rendering correctly. The Smart Map field is named jobAddress, in the section called jobListings.

Can anyone take a peek at my code and perhaps point out what I am doing wrong? Disclaimer... I am extremely new to Craft. I appreciate your help and guidance!

{% extends "_layout" %}

{% block main %}

    {# Set variables for each possible param: #}
    {% set state = craft.app.request.getParam('state') %}
    {% set trucktype = craft.app.request.getParam('trucktype') %}
    {% set city = craft.app.request.getParam('city') %}
    {% set target = craft.app.request.getParam('zip') %}
    {% set range = craft.app.request.getParam('range') %}

    {# Here we set the criteria and combine all the parameter variables #}
    {% set criteria = {
        section: 'jobListings',
        state: state,
        truckType: trucktype,
        city: city,
        jobAddress:{
            zip:target
        }
    } %}

    {# Pass the criteria object to the query builder: #}
    {% set query = craft.entries(criteria) %}

    {% if target is not empty and range is not empty %}
        {% set query = query|merge({
            jobAddress:{
                range: range
            }
        }) %}
    {% endif %}

    {# We build the map location based on criteria #}
    <div class="container-fluid" id="map">
        <div class="row bg-secondary">
            <div class="col-sm-12 p-0">
                {% set locations = query %}
                {% set options = {
                    height:400,
                    zoom: 5,
                    'markerInfo': 'searchjobs/infoBubble',
                    markerOptions: {
                        icon: 'https://maps.google.com/mapfiles/ms/icons/green-dot.png'
                    }
                } %}
                {{ craft.smartMap.map(locations, options) }}
            </div>
        </div>
    </div>

    {# DISPLAY Search Results #}
    <div class="container-fluid pt-5 pb-1 jobs-index">
        <h1 class="text-center">Search Results</h1>
        {# Set up pagination, and let it grab the current page's results: #}
        {% paginate query as pageInfo, results %}

        {% for result in results %}
            <div class="row p-4">
                <div class="col-sm-8 offset-sm-2 col-md-6 offset-md-3 bg-light p-2">
                    <h3 class="job-header">{{ result.title }}</h3>
                    <p><strong><em>Truck:</em></strong> {{ result.truckType }}</p>
                    <p><strong><em>State</em></strong>{{ result.state}}</p>
                    <p><strong><em>City:</em></strong> {{ result.city}}</p>
                    <a class="btn btn-small btn-details" href="/jobs/{{result.slug}}">Get More Details &nbsp; <i class="fas fa-arrow-right"></i> </a>
                    <p class="pt-2"><strong>Benefits: </strong>
                        {% for option in result.jobBenefits %}
                            {{ option.value }}
                            {# Value: {{ option }} or {{ option.value }} #}
                        {% endfor %}
                    </p>
                    {# Show the Radius #}
                    <p><strong>{{ result.jobAddress.distance | number_format(1) }} miles away</strong></p>
                </div>
            </div>
        {% endfor %}

        <div class="search-again">
            <h3 class="text-center">Didn't find what you're looking for?</h3>
            <a class="btn btn-lg btn-success search-again-btn" href="/jobs">Search Again</a>
        </div>
    </div>
{% endblock %}

Here is a live preview of the current site...

1

Author of Smart Map here... I can't tell exactly what question you are asking, so I'll give an answer based on my understanding of the problem.

It looks like your biggest problem is here:

    {% set criteria = {
        section: 'jobListings',
        state: state,
        truckType: trucktype,
        city: city,
        jobAddress: {
            zip: target
        }
    } %}

If you are trying to filter by the state, city, and zip of the jobAddress field, then those will need to be contained in a filter parameter...

    {% set criteria = {
        section: 'jobListings',
        truckType: trucktype,
        jobAddress: {
            filter: {
                state: state,
                city: city,
                zip: target
            }
        }
    } %}

Check out the docs on Filtering entries by subfield value for more information. Hope that helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.