1

There's something that I'm missing in the migration from smartMaps to GoogleMaps. I have a series of points that represent different types/outcomes/modes of transportation crashes. I'm setting options on the markers and then trying to push that back into the googleMaps markers.

This worked with smartMaps but my JS-skillz are not super strong and I'm getting an error: Example: Uncaught TypeError: Cannot read property 'mapAlpha.5981.address' of undefined

What am I missing here?

 {% set locations = craft.entries.section('crashes').all() %}

  {% set options = {
    id: 'mapAlpha'
  } %}

  {{ googleMaps.map(locations, options).tag() }}

  <script>

    var crashMarkers = Array();

    {#  Loop through entries and add settings to markers #}
    {% for entry in locations %}
      var markerId = 'mapAlpha.{{ entry.id }}.address';

      // Uncaught TypeError: Cannot read property 'mapAlpha.5981.address' of undefined

      googleMaps.marker[markerId].setOptions(
          {
              'icon': '{{ icon }}',
          }
      );

      crashMarkers.push(googleMaps.marker[markerId]);

    {% endfor %}

</script>
2

Icon management is handled a bit differently in the new Google Maps plugin. Now, you set icons before the map is rendered, even in Twig!

{# Get all bars & restaurants #}
{% set bars        = craft.entries.section('locations').type('bars').all() %}
{% set restaurants = craft.entries.section('locations').type('restaurants').all() %}

{# Create a dynamic map (with no markers) #}
{% set map = googleMaps.map() %}

{# Add all bar markers #}
{% do map.markers(bars, {
    'icon': '/images/bar-icon.png'
}) %}

{# Add all restaurant markers #}
{% do map.markers(restaurants, {
    'icon': '/images/restaurant-icon.png'
}) %}

{# Display map #}
{{ map.tag() }}
2
  • Got it — I see how to add the infowindow via options, but in this case, how do I pass those options to the set map = googleMaps.map() without an array of locations? Mar 16 at 14:30
  • 1
    googleMaps.map([], options) - Skip the first parameter by passing in an empty array.
    – Lindsey D
    Mar 17 at 3:31

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.