Ad

Shopify Liquid: How To Render Google Address Url Into Google Map

I am making a generic solution, When I click on first Address Icon It must show the address map bind to it. Same with the second one. I have added the schema as address-id as type text and passed URL under Shopify Edit Themes promo block(Schema at bottom of the code.) for each address block. I am passing address-id into data HTML attribute. Then on click of .promo block, I am getting data attribute and storing in a variable and that variable I am passing into Jquery.html(mapAddress). I am getting current address or URL on click of each address icon.I want to display it as Map. I tried to add <iframe> directly to the address-id into input box address-id but that is not working fine. It is showing Iframe values. What can be done here to get google map url to be rendered as Map? Thanks.

Shopify liquid template

 <div class="sixteen columns featured_links">
    <div class="section clearfix feature">
      {% for block in section.blocks %}
        <div {% if block.settings.address-id != blank %} data-address="{{block.settings.address-id}}" {% endif %} class="promo {% if section.settings.featured_promos_per_row == 2 %}eight columns {% cycle 'alpha', 'omega' %}{% elsif section.settings.featured_promos_per_row == 3 %}one-third column {% cycle 'alpha', '', 'omega' %}{% else %}four columns {% cycle 'alpha', '', '', 'omega' %}{% endif %} {% if section.settings.featured_links_style != blank %}{{ section.settings.featured_links_style }} {% cycle 'delay-025s', 'delay-05s', 'delay-075s', 'delay-1s' %}{% endif %} center">
          {% if block.settings.link != blank %}
            <a target="_blank" rel="nofollow noreferrer" href="{{ block.settings.link }}">
          {% endif %}

          <div class="{% if section.settings.rounded_image != blank %}rounded{% endif %}">
            {% if block.settings.image != nil %}
              <img  src="{{ block.settings.image | img_url: '300x' }}"
                    alt="{{ block.settings.image.alt }}"
                    data-src="{{ block.settings.image | img_url: '2048x' }}"
                    class="lazyload"
                    {% comment %} data-sizes="auto" {% endcomment %}
                    data-srcset=" {{ block.settings.image | img_url: '2048x' }} 2048w,
                                  {{ block.settings.image | img_url: '1600x' }} 1600w,
                                  {{ block.settings.image | img_url: '1200x' }} 1200w,
                                  {{ block.settings.image | img_url: '1000x' }} 1000w,
                                  {{ block.settings.image | img_url: '800x' }} 800w,
                                  {{ block.settings.image | img_url: '600x' }} 600w,
                                  {{ block.settings.image | img_url: '400x' }} 400w"
                     />
            {% else %}
              {% capture i %}{% cycle "1", "2", "3", "4", "5", "6" %}{% endcapture %}
              {{ 'collection-' | append: i | placeholder_svg_tag: 'placeholder-svg placeholder-svg--promotions' }}
            {% endif %}
          </div>

          {% if block.settings.title != blank %}
            <h3>{{ block.settings.title | escape }}</h3>
            {% if section.settings.show_divider %}
              <div class="feature_divider"></div>
            {% endif %}
          {% endif %}
          {% if block.settings.link != blank %}
            </a>
          {% endif %}

          {% if block.settings.text != blank %}
            {{ block.settings.text }}
          {% endif %}
        </div>

        {% if section.settings.featured_promos_per_row == 2 %}
          {% cycle '', '<br class="clear " />' %}
        {% elsif section.settings.featured_promos_per_row == 3 %}
          {% cycle '', '', '<br class="clear" />' %}
        {% else %}
          {% cycle '', '', '', '<br class="clear" />' %}
        {% endif %}
      {% endfor %}
    </div>

    {% if section.settings.contact_address != blank %}
    <br class="clear" />
    <div class="embed-container maps">
        <iframe width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d945.7084267048235!2d73.8854388291583!3d18.536415399212462!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0xdda3387f821de029!2sWink+%26+Nod!5e0!3m2!1sen!2sin!4v1525613363322"></iframe>
    </div>
    {% endif %}    
  </div> 
</div>

JQuery Code onClick load google Map

<script>
$('.promo').click(function(){
    var mapAddress=$(this).attr("data-address");
  $('.embed-container, .maps').html(mapAddress);
}); 
</script>

Schema

{% schema %}
{
        {
          "type": "text",
          "id": "address-id",
          "label": "Address-ID"
        }
}
{% endschema %}
Ad

Answer

I simply have to pass mapAddress variable into Iframe src using .html()and Vola. It worked !!!

$('.promo').click(function(){
    var mapAddress=$(this).attr("data-address");
    $(".embed-container, .maps").html('<iframe  width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+mapAddress+'"><\/iframe>');
});
Ad
source: stackoverflow.com
Ad