From 77a65ddea3c8034fca655cee31d88db554a746c0 Mon Sep 17 00:00:00 2001 From: rmglennon Date: Thu, 27 Apr 2017 16:37:33 -0700 Subject: [PATCH 1/2] updates for API key and mapzen.js changes --- add-search-to-a-map.md | 129 +++++++++++++++++++++-------------------- 1 file changed, 67 insertions(+), 62 deletions(-) diff --git a/add-search-to-a-map.md b/add-search-to-a-map.md index 24d01cb..890a080 100644 --- a/add-search-to-a-map.md +++ b/add-search-to-a-map.md @@ -8,9 +8,9 @@ Through a process known as [geocoding](https://en.wikipedia.org/wiki/Geocoding), In this tutorial, you will learn how to make a map with a search box that allows you to enter addresses and place names and locate them on a map. To complete the tutorial, you should have some familiarity with HTML and JavaScript, although all the source code is provided. You can use any text editor and operating system, but must keep an Internet connection while you are working. -You also need a Mapzen API key, which you can get by following the steps in the Mapzen [developer overview](https://mapzen.com/documentation/overview/#developer-accounts-and-api-keys). +You also need a Mapzen API key, which you can get by following the steps in the Mapzen [developer overview](https://mapzen.com/documentation/overview/). -Suggested text editor applications include [Atom - OS X, Windows, Linux](https://atom.io/); [Notepad++ - Windows](https://notepad-plus-plus.org/); [TextWrangler - OS X](http://www.barebones.com/products/textwrangler/); and [Sublime - OS X, Windows, Linux; free trial](http://www.sublimetext.com/). While you can use the apps installed with your operating system, such as Notepad or TextEdit, they do not provide the helpful indentations, code coloring and autocomplete, or text alignment options found in the other editors. For TextEdit, you must go to the Format menu and click Make Plain Text to use the plain-text version of the file. Do not use an app that applies rich formatting, such as Word or Wordpad. +To get started making your map, you will need to use a text editor to update the HTML. See some of Mapzen's [suggested text editors](https://mapzen.com/documentation/guides/install-text-editor/) in the developer guide documentation. ## Create an HTML page @@ -116,14 +116,25 @@ To display a Leaflet map on a page, you need a `
` element, which is a conta
``` -3. Directly after the `
`, add this JavaScript code within a ` + ``` + + The `your-mapzen-api-key` text is the Mapzen API key; paste your own API key inside the single quotes. You can get an API key by following the steps in the Mapzen [developer overview](https://mapzen.com/documentation/overview/). + +4. Inside the same ` ``` @@ -132,8 +143,6 @@ To display a Leaflet map on a page, you need a `
` element, which is a conta The next line sets the `zoom` level, which is like a map scale or resolution, where a smaller value shows a larger area in less detail, and a larger zoom level value depicts smaller area in great detail. - The `scene: L.Mapzen.BasemapStyles.BubbleWrap` line sets the style used for the map. In this case, it is Mapzen's all-purpose stylesheet called BubbleWrap. - 4. Save your edits and refresh the browser. Your index.html should look something like this: @@ -160,10 +169,11 @@ Your index.html should look something like this:
@@ -180,18 +190,14 @@ To recap how you created this, you added references to the Mapzen JS and CSS fil So far, you have referenced the necessary files, initialized Leaflet with a map container on the page, and added data to the map. Now, you are ready to add the Search box. -1. Inside the same ` [...] @@ -235,32 +241,42 @@ If you look at your browser's developer tools console as you are doing this, you Although you will not be using it in this tutorial, [\reverse](https://mapzen.com/documentation/search/reverse/) is another common Mapzen Search endpoint. It performs reverse geocoding to find the address at a given coordinate location. You can find a listing of all the endpoints and parameters in the [Mapzen Search documentation](https://mapzen.com/documentation/search/). -1. Modify the geocoder code block so you can pass in other parameters, as shown below. Make sure your () and {} close properly. +1. Add a variable to allow you to set options for the geocoder. Inside the script tags, and above the geocoder line, add this block. ```js - var geocoder = L.Mapzen.geocoder('mapzen-xxxxxx', { - }); - geocoder.addTo(map); + var geocoderOptions = { + autocomplete: false + }; ``` -2. Add `autocomplete: false` to specify that the Search box should not suggest potential text matches as you type. Autocomplete is enabled by default, so adding this means that you will turn it off. + You are setting `autocomplete: false` to specify that the Search box should not suggest potential text matches as you type. Autocomplete is enabled by default, so adding this means that you will turn it off. + +2. Modify the existing geocoder code to pass in two parameters. The first is a placeholder for an additional API key, and the second is the `geocoderOptions` you set. (If you want to use a different API key for the geocoding function of you app, you could add it here; with the `""`, you are using the same global API key that you set before.) ```js - var geocoder = L.Mapzen.geocoder('mapzen-xxxxxx', { - autocomplete: false - }); - geocoder.addTo(map); + var geocoder = L.Mapzen.geocoder("", geocoderOptions); ``` 3. Save your edits and refresh the browser. 4. Type `901 12th Avenue` in the Search box and press Enter. Notice now that the matching search results are not listed until you press the Enter key. +The code from this section should look something like this. + +```js +var geocoderOptions = { + autocomplete: false +}; + +var geocoder = L.Mapzen.geocoder("", geocoderOptions); +geocoder.addTo(map); +``` + ### Extra credit: View the JSON response 1. Open your browser's developer tools console. In Chrome, you can do this by clicking the menu in the corner, pointing to More Tools, and clicking Developer Tools. 2. Click the Network tab to see the Internet traffic, including the queries to the Mapzen servers. -3. Click the Headers tab for more information about the request, including the full URL. For example, the URL might look something like `https://search.mapzen.com/v1/search?text=901%2012th%20avenue&focus.point.lat=47.61032944737081&focus.point.lon=-122.31800079345703&api_key=mapzen-xxxxxx` -4. Paste this URL into a new browser tab to see the JSON response, which can be mapped. +3. Click the Headers tab for more information about the request, including the full URL. For example, the URL might look something like `https://search.mapzen.com/v1/search?text=901%2012th%20avenue&focus.point.lat=47.61032944737081&focus.point.lon=-122.31800079345703&api_key=your-mapzen-api-key` +4. Paste this URL into a new browser tab and use your own API key to see the JSON response, which can be mapped. _Tip: You can install a plug-in for your browser to display JSON in a more formatted manner. You can search the web store for your browser to find and install applicable products._ @@ -274,16 +290,15 @@ You can choose which data sources to search by passing a parameter for the `sour As you were searching, you might have noticed results that looked similar. Mapzen Search does perform some elimination, but the differing data sources may still cause seemingly matching results to appear. Choosing a particular data source can reduce the occurrence of duplicated entries. -1. Within the geocoder block, add the `params:` list and a parameter for `sources:`. Be sure to add a `,` at the end of the `autocomplete: false` line. +1. Within the `geocoderOptions` block, add the `params:` list and a parameter for `sources:`. Be sure to add a `,` at the end of the `autocomplete: false` line. ```js - var geocoder = L.Mapzen.geocoder('mapzen-xxxxxx', { + var geocoderOptions = { autocomplete: false, params: { sources: 'osm' } - }); - geocoder.addTo(map); + }; ``` 2. Save your edits and refresh the browser. @@ -295,36 +310,22 @@ Mapzen Search provides options for customizing your search parameters, such as l Mapzen.js automatically provides a [focus point](https://mapzen.com/documentation/search/search/#prioritize-around-a-point) for you based on the current map view extent. You can add other parameters to filter the search results, such as to limit the results to a particular country or type of result. -1. Within the geocoder block, add add a `,` at the end of the `sources: 'osm'` line and then a parameter for `'boundary.country': 'USA'` on the next line. You need to enclose with single quotation marks any parameter names that use the dot notation (such as `boundary.country`) to make sure JavaScript can parse the text correctly. +1. Within the `geocoderOptions` block, add add a `,` at the end of the `sources: 'osm'` line and then a parameter for `'boundary.country': 'USA'` on the next line. You need to enclose with single quotation marks any parameter names that use the dot notation (such as `boundary.country`) to make sure JavaScript can parse the text correctly. ```js - var geocoder = L.Mapzen.geocoder('mapzen-xxxxxx', { + var geocoderOptions = { autocomplete: false, params: { sources: 'osm', 'boundary.country': 'USA' } - }); - geocoder.addTo(map); + }; ``` 3. Save your edits and refresh the browser. 4. Search again for city names in the Search box. Notice that you only see results from within the United States. For example, `Vancouver` in Canada is no longer listed, but you can find the city in Washington. 5. Optionally, trying changing the `boundary.country` to another country code, such as `AUS` for Australia. There is a [specific format](https://en.wikipedia.org/wiki/ISO_3166-1) you need to use for the country code. Change the code back to `USA` when you are done. -The code you added in this section should look something like this. - -```js -var geocoder = L.Mapzen.geocoder('mapzen-xxxxxx', { - autocomplete: false, - params: { - sources: 'osm', - 'boundary.country': 'USA' - } -}); -geocoder.addTo(map); -``` - ## Filter the results by type of place In Mapzen Search, types of places are referred to as `layers`, and you can use these to filter your results. For example, if your app has an input form where your users should only be able to enter a city, you can use Mapzen Search to limit the results to show only matching city names. This is common in travel apps, such as searching for a hotel or flight, where you enter a destination city. @@ -333,18 +334,17 @@ In this section, you will filter the results to search only addresses and venues You can review the [Mapzen Search documentation](https://mapzen.com/documentation/search/search/#filter-by-data-type) to learn the types of `layers` you can use in a search. -1. Within the geocoder block, add add a `,` at the end of the `'boundary.country: 'USA'` line and then a parameter for `layers: 'address,venue'` on the next line. +1. Within the `geocoderOptions` block, add add a `,` at the end of the `'boundary.country: 'USA'` line and then a parameter for `layers: 'address,venue'` on the next line. ```js - var geocoder = L.Mapzen.geocoder('mapzen-xxxxxx', { + var geocoderOptions = { autocomplete: false, params: { sources: 'osm', 'boundary.country': 'USA', layers: 'address,venue' } - }); - geocoder.addTo(map); + }; ``` 2. Save your edits and refresh the browser. 3. Search for `102 Pike Street, Seattle, WA 98101` (the first Starbucks) and press Enter. Some other places you can try include `Starbucks`, `400 Broad Street` (the address of the Space Needle), `Space Needle`, and `University of Washington`. @@ -381,25 +381,30 @@ You can refer to this HTML if you want to review your work or troubleshoot an er
From b6db3fd8555532375146629d27e1b485b1211a16 Mon Sep 17 00:00:00 2001 From: rmglennon Date: Fri, 28 Apr 2017 08:24:34 -0700 Subject: [PATCH 2/2] update global api key for fix in mapzen.js --- add-search-to-a-map.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/add-search-to-a-map.md b/add-search-to-a-map.md index 890a080..600a03f 100644 --- a/add-search-to-a-map.md +++ b/add-search-to-a-map.md @@ -251,10 +251,10 @@ Although you will not be using it in this tutorial, [\reverse](https://mapzen.co You are setting `autocomplete: false` to specify that the Search box should not suggest potential text matches as you type. Autocomplete is enabled by default, so adding this means that you will turn it off. -2. Modify the existing geocoder code to pass in two parameters. The first is a placeholder for an additional API key, and the second is the `geocoderOptions` you set. (If you want to use a different API key for the geocoding function of you app, you could add it here; with the `""`, you are using the same global API key that you set before.) +2. Modify the existing geocoder code to pass in the `geocoderOptions` you set. ```js - var geocoder = L.Mapzen.geocoder("", geocoderOptions); + var geocoder = L.Mapzen.geocoder(geocoderOptions); ``` 3. Save your edits and refresh the browser. @@ -267,7 +267,7 @@ var geocoderOptions = { autocomplete: false }; -var geocoder = L.Mapzen.geocoder("", geocoderOptions); +var geocoder = L.Mapzen.geocoder(geocoderOptions); geocoder.addTo(map); ``` @@ -401,8 +401,8 @@ You can refer to this HTML if you want to review your work or troubleshoot an er } }; - // Add the geocoder to the map, set parameters for an optional API key and other geocoder options - var geocoder = L.Mapzen.geocoder("", geocoderOptions); + // Add the geocoder to the map, set parameters for geocoder options + var geocoder = L.Mapzen.geocoder(geocoderOptions); geocoder.addTo(map);