diff --git a/Tutorials/building-a-store-locator.md b/Tutorials/building-a-store-locator.md index e1346f1..0559a63 100644 --- a/Tutorials/building-a-store-locator.md +++ b/Tutorials/building-a-store-locator.md @@ -19,57 +19,53 @@ prependJs: contentType: tutorial --- -This guide will walk you through how to create a store locator map using Mapbox GL JS. You'll be able to browse all the locations from a sidebar and select a specific store to view more information. Selecting a [marker](/help/glossary/marker/) on the map will highlight the selected store on the sidebar. + +此教程将展示如何利用Mapbix GL JS创建一个商店定位地图。您将可以通过侧边栏浏览所有的位置并选择一个位置以查看详细信息。从侧边栏中选择一个[标签](/help/glossary/marker/)可以进而标记地图上所选商店的位置。 {{ }} -You will use [Sweetgreen](http://sweetgreen.com), a local salad shop, as an example. They have a healthy number of locations, plus their salads are delicious! - -This guide shows you how to use [Mapbox GL JS](https://www.mapbox.com/mapbox-gl-js/) to build an interactive web map. If you're new to Mapbox GL JS, you might want to read our guide on Mapbox [web applications](/help/how-mapbox-works/web-apps/) first. - -## Getting started - -For this project, we recommend that you create a local folder called "store-locator" to house your project files. You'll see this folder referred to as your *project folder*. - -There are a few resources you'll need before getting started: +我们将以一个当地的沙拉店,[Sweetgreen](http://sweetgreen.com),作为示例。他们拥有一系列的连锁店位置,而且他们的沙拉非常新鲜可口! -- [__A style URL__](/help/glossary/style-url). A style URL points to a unique map you have created with Mapbox Studio. You can either create a custom style with the [Mapbox Studio style editor](https://www.mapbox.com/studio-manual/reference/styles/) or use a [Mapbox style](https://www.mapbox.com/studio/styles). +本教程将展示如何使用[Mapbox GL JS](https://www.mapbox.com/mapbox-gl-js/)创建一个交互式的网页地图。如果您是第一次使用Mapbox GL JS,我们建议您先阅读Mapbox所提供的关于[网络应用](/help/how-mapbox-works/web-apps/)的教程。 -- [__An access token__](/help/glossary/access-token/) from your account. You will use an access token to associate a map with your account. Your access token is on the [Account page](https://www.mapbox.com/account/). +## 准备工作 -- [__Mapbox GL JS__](https://www.mapbox.com/mapbox-gl-js/). The Mapbox JavaScript library that uses WebGL to render interactive maps from Mapbox GL styles. +首先,我们建议您创建一个本地文件夹,“store-locator”,以存放您的工程文件。在之后的教程中,我们称这个文件夹为*工程文件夹*。 -- __A text editor.__ You'll be writing HTML, CSS, and JavaScript after all. +您需要以下的资源: -- __Data__. We collected some of Sweetgreen's locations and marked up the data in GeoJSON. - -- __Custom map marker__. You'll be using an image for your map marker. Save the image to your project folder. +- [__风格URL__](/help/glossary/style-url):一个风格URL对应着您在Mapbox Studio中所创建一个地图风格。您可以在[Mapbox Studio风格编辑器](https://www.mapbox.com/studio-manual/reference/styles/)中创建一个自定义风格,或者使用[预设的风格](https://www.mapbox.com/studio/styles)。 +- 账号[__秘钥__](/help/glossary/access-token/):您需要一个秘钥来将您的账号和您的地图关联起来。您的秘钥可以在[账号信息页](https://www.mapbox.com/account/)获取。 +- [__Mapbox GL JS__](https://www.mapbox.com/mapbox-gl-js/):Mapbox JavaScript开发者库使用了WebGL来呈递Mapbox GL风格的交互式地图。 +- __文本编辑器__:您需要能够编写THML、CSS以及JavaScript。 +- __数据__:我们准备了一些Sweetgreen连锁店的位置信息,并编译成了GeoJSON格式的文件。 +- __自定义地图标签__:您需要一张图片来作为您的地图标签。请自行选择并保存一张图片至您的工作文件夹中。 {{ }} -## Add structure +## 页面布局设置 -In your project folder, create an `index.html` file. Set up the document by adding Mapbox GL JS and CSS to your `head`: +在您的工程文件夹中,创建一个文件并命名为`index.html`。在`head`中添加以下Mapbox GL JS和CSS代码: ```html ``` -Next, markup the page to create a map container and sidebar listing: +然后,我们需要标记此页面并创建一个地图容器和一个侧边栏: ```html
Map
``` -Then, apply some CSS to create the page layout: +接下来,我们设置一些CSS以定义页面的布局: ```css body { @@ -81,8 +77,7 @@ body { -webkit-font-smoothing: antialiased; } -/* The page is split between map and sidebar - the sidebar gets 1/3, map -gets 2/3 of the page. You can adjust this to your personal liking. */ +/*这个页面被分为了地图和侧边栏。侧边栏相对宽度为页面的三分之一,地图容器为三分之二。您可以自行调整相对宽度。*/ .sidebar { width: 33.3333%; } @@ -108,56 +103,53 @@ gets 2/3 of the page. You can adjust this to your personal liking. */ }} -## Initialize the map +## 初始化地图 -Now that you have the structure of the page, initialize the map with Mapbox GL JS. +您已经完成了页面的布局设计,我们可以通过Mapbox GL JS来初始化我们的地图。 -First, add your access token using `mapboxgl.accessToken`. Then, create a new `map` object using `new mapboxgl.Map()` and store it in a variable called `map`: +首先,在`mapboxgl.accessToken`中添加您的秘钥。然后,通过`new mapboxgl.Map()`创建一个新的`map`对象,并将其保存在变量`map`中: ```js mapboxgl.accessToken = '{{ }}'; -// This adds the map to your page +// 在页面中添加一个地图 var map = new mapboxgl.Map({ - // container id specified in the HTML + // HTML中所包含的容器ID container: 'map', - // style URL + // 风格URL style: 'mapbox://styles/mapbox/light-v{{constants.VERSION_LIGHT_STYLE}}', - // initial position in [lon, lat] format + // 初始化的位置用[x, y]格式标记 center: [-77.034084, 38.909671], - // initial zoom + // 初始的缩放程度 zoom: 14 }); ``` -As you can see above, the Mapbox GL JS map requires several options: - -- `container`: the `id` of the `
` element on the page where the map should live. In this case, the `id` for the `
` is `'map'`. - -- `style`: the [style URL](/help/glossary/style-url/) for the map style. In this case, use the {{ }} which has the style URL `mapbox://styles/mapbox/light-{{constants.VERSION_LIGHT_STYLE}}`. +如您所见,Mapbox GL JS初始化地图的时候需要以下的设置: -- `center`: the initial centerpoint of the map in [longitude, latitude] format. +- `contianer`:`
`的`id`元素标注了地图的显示位置。在上面的例子中,我们 的`id`是`map`。 +- `style`:地图所选用的[风格URL](/help/glossary/style-url/)。在我们的例子中,{{ }}包含了风格URL`mapbox://styles/mapbox/light-{{constants.VERSION_LIGHT_STYLE}}`。 +- `center`:由[x, y]所标注的初始化地图中心位置。 +- `zoom`:初始的地图缩放程度。 -- `zoom`: the initial zoom level of the map. +## 加载数据 -## Load data +在Mapbox GL JS中,地图的呈递过程在浏览器中实现。想要实现这一过程,您需要添加一个具有地理信息的图层以及对应的操作步骤。 -With Mapbox GL JS, map rendering happens in the browser. For the browser to render your map, you need to add a layer with geospatial data and instructions for how that data should be rendered. - -To add a source to the map, your code needs to access the geospatial data. Store all the GeoJSON data in `sweetgreen.geojson` in a variable called `stores`: +您的代码需要能够访问地理信息才能够添加一个源地图。将所有的`sweetgreen.geojson`GeoJSON数据保存在变量`stores`中: ```js var stores = {{ sweetgreenLocations }}; ``` -Now you can add a layer that contains this data and describes how it should be rendered. Add the data to your map once the map loads using `addLayer()`. Create a new layer, and specify `stores` as a GeoJSON data source. Then, add instructions for rendering the source. This example only adds minimal styling --- for full details on all the layer styling options, see the [Mapbox Style Specification](https://www.mapbox.com/mapbox-gl-style-spec/): +现在您可以添加一个图层并进一步定义这个图层应该如何显示。当地图通过`addLayer()`显示的时候,向地图中添加您的数据。创建一个新的图层,并设置`stores`为您的GeoJSON数据源。然后,添加如何显示数据的操作步骤。此教程仅使用了极简风格,如果您想要了解完整的图层风格设置信息,请阅读[Mapbox风格设置文档](https://www.mapbox.com/mapbox-gl-style-spec/): ```js map.on('load', function(e) { - // Add the data to your map as a layer + // 向地图中添加数据图层 map.addLayer({ id: 'locations', type: 'symbol', - // Add a GeoJSON source containing place coordinates and information. + // 添加包含有坐标和附加信息的GeoJSON数据源 source: { type: 'geojson', data: stores @@ -170,17 +162,17 @@ map.on('load', function(e) { }); ``` -_Note: `restaurant-15` refers to an icon in the Mapbox Light style you added earlier in the code._ +_请注意,`restaurant-15`指的是Mapbox轻风格中的一个图标。您在之前的步骤中已经添加了这个风格。_ {{ }} -## Build store listing +## 创建商店列表 -Now that the points are on your map, it's time to build the restaurant location listing by iterating through the GeoJSON and creating a list of restaurants dynamically. This means that if you need to add a location then you *only* need to update the GeoJSON. +您已经将商店的位置显示在了地图上,现在可以利用一个循环语句来自动创建包含所有商店位置信息的列表了。我们使用一个循环语句遍历所有的GeoJSON数据,这样一来,如果您需要修改或者添加商店位置信息,您**只需要**修改GeoJSON文件,而不需要改动任何的代码。 -First, update the sidebar HTML to hold the listing information and update your CSS to accommodate the layout changes: +首先,我们需要改动HTML以保证列表信息的显示,同时相应的我们需要更改CSS格式设置: ```html