1

I see the docs here about getting the lat/long of an address from a zip code, city, etc. I am having issues getting the map to render from this info. My code:

{% if craft.smartMap.visitor.ip != '' %}
    {% set locations = craft.smartMap.visitor.coords %}
{% else %}
    {% set locations = craft.smartMap.lookupCoords(90210) %}
{% endif %}

{{ craft.smartMap.map(locations, options) }}

I get:

Undefined index: fieldId

How do I convert the lat/long result into one of the correct location types?

1

Let me start with a minor side note... You'll want to change the first line to this:

{% if craft.smartMap.visitor.ip %}

It's better to test against a "falsey" IP address, instead of explicitly testing for an empty string. I believe the IP address would resolve to false if nothing was found. Either way, a general "falsey" check will work as expected here.


It's not currently possible to convert a raw set of coordinates into a more complex object.

That being said, you could fairly easily do it using the built-in smartMap JavaScript object...

{# Get visitor's coordinates #}
{% if craft.smartMap.visitor.ip %}
    {% set coords = craft.smartMap.visitor.coords %}
    {% set jsCoords = 'smartMap.coords(' ~ coords[0] ~ ', ' ~ coords[1] ~ ')' %}
{% else %}
    {% set coords = craft.smartMap.lookupCoords(90210) %}
    {% set jsCoords = 'smartMap.coords(' ~ coords['lat'] ~ ', ' ~ coords['lng'] ~ ')' %}
{% endif %}

{# Blank map #}
{{ craft.smartMap.map([], options) }}

{# Add visitor marker #}
{% js %}
    smartMap.createMarker('visitor-marker', {
        position: {{ jsCoords }},
        map: smartMap.map['smartmap-mapcanvas-1'],
    });
{% endjs %}

UPDATE: I fixed a bug in v3.0.5 which makes this even easier.

| improve this answer | |
0

Actually you can check google map api directly other than using map plugins

| improve this answer | |
  • Thank you both. I don't just want to put a pin on a map, I want to display all addresses in a craft structure within a certain distance. – Eric Snyder Mar 31 '18 at 18:16

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.