简体   繁体   English

在Shopify中实现JQuery存储定位器插件

[英]Implementing JQuery store locator plugin into Shopify

I am attempting to setup the following jQuery store locator plugin into Shopify: https://github.com/bjorn2404/jQuery-Store-Locator-Plugin 我正在尝试将以下jQuery商店定位器插件安装到Shopify中: https : //github.com/bjorn2404/jQuery-Store-Locator-Plugin

I have managed to do the initial setup and the map works well. 我已经完成了初始设置,并且地图运行良好。

At the moment I have a static json file which pulls in the locations of the stores for the plugin. 目前,我有一个静态json文件,该文件提取插件存储位置。 This is the format of my json file: 这是我的json文件的格式:

[
  {
    "id": "1",
    "name": "Store One",
    "lat": "-38.944094",
    "lng": "110.824870",
    "address": "Address of store",
    "address2": "",
    "city": "City",
    "state": "WA",
    "postal": "90210",
    "phone": "9555 5555",
    "hours1": "Mon-Sun 11am-10pm",
    "hours2": "",
    "hours3": ""
  }
]

Here is the code to pull in the locations: 这是放置位置的代码:

$('#bh-sl-map-container').storeLocator({
    'slideMap': false,
    'fullMapStart': true,
    'lengthUnit': 'km',
    'distanceAlert': -1,
    'dataType': 'json',
    'dataLocation': 'locations.json'
});

What I'm having trouble with is dynamically creating the json file so the locations can be updated by the cms. 我遇到的麻烦是动态创建json文件,以便可以通过cms更新位置。 At the moment I have fields setup for the page under "Customise" as seen below: 目前,我在“自定义”下为页面设置了字段,如下所示:

Shopify自定义设置

What I would like to do is to have these fields pull through into the json file. 我想做的就是让这些字段进入json文件。 My questions are: 我的问题是:

  • Am I approaching this the right way? 我是否采用正确的方法?
  • And how do I dynamically create the json file to display the information thats needed? 以及如何动态创建json文件以显示所需的信息?

Any help would be appreciated. 任何帮助,将不胜感激。

It might not be the best solution but I found a solution which worked and I thought I might share here to help others and maybe improve the code. 它可能不是最好的解决方案,但我找到了一个可行的解决方案,我想我可以在这里分享以帮助他人,并可能改善代码。

I couldn't find a way to post data to the json file in Shopify in the format I needed so I output the XML on the page and called in the raw data (which the jQuery plugins allows). 我找不到一种以所需格式将数据发布到Shopify中的json文件中的方法,因此我在页面上输出XML并调用了原始数据(jQuery插件允许)。

var markers = $('.markers').html();

  $('#bh-sl-map-container').storeLocator({
    'slideMap': false,
    'fullMapStart': true,
    'lengthUnit': 'km',
    'distanceAlert': -1,
    'dataType': 'xml',
    'regionID': 'AU',
    'dataRaw': markers,
    'listTemplatePath': '[PATH OF LIST TEMPLATE]',
    'infowindowTemplatePath' :'[PATH OF INFO WINDOW]',
    'mapSettings': { 
      zoom : 14,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      styles: [{"featureType":"water","elementType":"geometry","stylers":[{"color":"#e9e9e9"},{"lightness":17}]},{"featureType":"landscape","elementType":"geometry","stylers":[{"color":"#f5f5f5"},{"lightness":20}]},{"featureType":"road.highway","elementType":"geometry.fill","stylers":[{"color":"#ffffff"},{"lightness":17}]},{"featureType":"road.highway","elementType":"geometry.stroke","stylers":[{"color":"#ffffff"},{"lightness":29},{"weight":0.2}]},{"featureType":"road.arterial","elementType":"geometry","stylers":[{"color":"#ffffff"},{"lightness":18}]},{"featureType":"road.local","elementType":"geometry","stylers":[{"color":"#ffffff"},{"lightness":16}]},{"featureType":"poi","elementType":"geometry","stylers":[{"color":"#f5f5f5"},{"lightness":21}]},{"featureType":"poi.park","elementType":"geometry","stylers":[{"color":"#dedede"},{"lightness":21}]},{"elementType":"labels.text.stroke","stylers":[{"visibility":"on"},{"color":"#ffffff"},{"lightness":16}]},{"elementType":"labels.text.fill","stylers":[{"saturation":36},{"color":"#333333"},{"lightness":40}]},{"elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"transit","elementType":"geometry","stylers":[{"color":"#f2f2f2"},{"lightness":19}]},{"featureType":"administrative","elementType":"geometry.fill","stylers":[{"color":"#fefefe"},{"lightness":20}]},{"featureType":"administrative","elementType":"geometry.stroke","stylers":[{"color":"#fefefe"},{"lightness":17},{"weight":1.2}]}]
    }
  });     

Then I setup my custom fields to be edited by the client in the customise section: 然后,我在定制部分中设置要由客户端编辑的定制字段:

{% schema %}
{
"name": "Store Locations",
"settings": [],
"blocks": [
  {
    "type": "Location",
    "name": "Location",
    "settings": [
      {
        "id": "google_location_store",
        "type": "text",
        "label": "Store"
      },
      {
        "id": "google_lat",
        "type": "text",
        "label": "Latitude"
      },
      {
        "id": "google_lng",
        "type": "text",
        "label": "Longitude"
      },
      {
        "id": "google_address",
        "type": "text",
        "label": "Address"
      },
      {
        "id": "google_city",
        "type": "text",
        "label": "City"
      },
      {
        "id": "google_state",
        "type": "text",
        "label": "State"
      },
      {
        "id": "google_postcode",
        "type": "text",
        "label": "Postcode"
      },
      {
        "id": "google_phone",
        "type": "text",
        "label": "Phone"
      },
      {
        "id": "google_opening_hours",
        "type": "textarea",
        "label": "Opening Hours"
      }
    ]
  }
]
}
{% endschema %}

Followed by the liquid code at the top of the template to pull through the xml data: 随后是模板顶部的液体代码以遍历xml数据:

{% capture data %}
{% for block in section.blocks %}
  <marker name="{{ block.settings.google_location_store }}" lat="{{ block.settings.google_lat }}" lng="{{ block.settings.google_lng }}" address="{{ block.settings.google_address }}" city="{{ block.settings.google_city }}" state="{{ block.settings.google_state }}" postal="{{ block.settings.google_postcode }}" phone="{{ block.settings.google_phone }}" email="{{ block.settings.email }}" hours1="{{ block.settings.google_opening_hours }}" />
{% endfor %}
{% endcapture %}


<div class="markers">
  <?xml version="1.0" encoding="utf-8"?>
  <markers>
    {{ data }}
  </markers>
</div>

If anyone had any improvements on the code to call it through the json I still feel that would be the best way but this is the only solution I could find. 如果有人对通过json调用它的代码有任何改进,我仍然认为这是最好的方法,但这是我能找到的唯一解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM