Skip to content

Commit bdeade3

Browse files
authored
Merge pull request #49896 from walsehgal/mymapsbranch
added a service module to get POI response from the Maps search API
2 parents aaed00b + f1b77bf commit bdeade3

File tree

1 file changed

+43
-59
lines changed

1 file changed

+43
-59
lines changed

articles/azure-maps/tutorial-search-location.md

Lines changed: 43 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Search with Azure Maps | Microsoft Docs
33
description: Search nearby point of interest using Azure Maps
44
author: dsk-2015
55
ms.author: dkshir
6-
ms.date: 05/07/2018
6+
ms.date: 08/23/2018
77
ms.topic: tutorial
88
ms.service: azure-maps
99
services: azure-maps
@@ -76,8 +76,9 @@ The Map Control API is a convenient client library that allows you to easily int
7676
<meta name="viewport" content="width=device-width, user-scalable=no" />
7777
<title>Map Search</title>
7878

79-
<link rel="stylesheet" href="https://atlas.microsoft.com/sdk/css/atlas.min.css?api-version=1.0" type="text/css" />
80-
<script src="https://atlas.microsoft.com/sdk/js/atlas.min.js?api-version=1.0"></script>
79+
<link rel="stylesheet" href="https://atlas.microsoft.com/sdk/css/atlas.min.css?api-version=1" type="text/css" />
80+
<script src="https://atlas.microsoft.com/sdk/js/atlas.min.js?api-version=1"></script>
81+
<script src="https://atlas.microsoft.com/sdk/js/atlas-service.min.js?api-version=1"></script>
8182

8283
<style>
8384
html,
@@ -126,10 +127,12 @@ The Map Control API is a convenient client library that allows you to easily int
126127

127128
## Add search capabilities
128129

129-
This section shows how to use the Maps Search API to find a point of interest on your map. It is a RESTful API designed for developers to search for addresses, points of interest, and other geographical information. The Search service assigns a latitude and longitude information to a specified address.
130+
This section shows how to use the Maps Search API to find a point of interest on your map. It is a RESTful API designed for developers to search for addresses, points of interest, and other geographical information. The Search service assigns a latitude and longitude information to a specified address. The **Service Module** explained below can be used to search for a location using the Maps Search API.
130131

131-
1. Add a new layer to your map to display the search results. Add the following Javascript code to the *script* block, after the code that initializes the map.
132+
### Service Module
132133

134+
1. Add a new layer to your map to display the search results. Add the following Javascript code to the script block, after the code that initializes the map.
135+
133136
```JavaScript
134137
// Initialize the pin layer for search results to the map
135138
var searchLayerName = "search-results";
@@ -140,69 +143,50 @@ This section shows how to use the Maps Search API to find a point of interest on
140143
});
141144
```
142145

143-
2. Create an [XMLHttpRequest](https://xhr.spec.whatwg.org/) and add an event handler to parse the JSON response sent by the Maps search service. This code snippet builds the event handler to collect the addresses, names, and latitude and longitude information for each location returned in the `searchPins` variable. Finally, it adds this collection of location points to the `map` control as pins.
146+
2. To instantiate the client service, add the following Javascript code to the script block, after the code that initializes the map.
144147

145148
```JavaScript
146-
// Perform a request to the search service and create a pin on the map for each result
147-
var xhttp = new XMLHttpRequest();
148-
xhttp.onreadystatechange = function () {
149-
var searchPins = [];
150-
151-
if (this.readyState === 4 && this.status === 200) {
152-
var response = JSON.parse(this.responseText);
153-
154-
var poiResults = response.results.filter((result) => { return result.type === "POI" }) || [];
155-
156-
searchPins = poiResults.map((poiResult) => {
157-
var poiPosition = [poiResult.position.lon, poiResult.position.lat];
158-
return new atlas.data.Feature(new atlas.data.Point(poiPosition), {
159-
name: poiResult.poi.name,
160-
address: poiResult.address.freeformAddress,
161-
position: poiResult.position.lat + ", " + poiResult.position.lon
162-
});
163-
});
164-
165-
map.addPins(searchPins, {
166-
name: searchLayerName
167-
});
168-
169-
var lons = searchPins.map((pin) => { return pin.geometry.coordinates[0] });
170-
var lats = searchPins.map((pin) => { return pin.geometry.coordinates[1] });
171-
172-
var swLon = Math.min.apply(null, lons);
173-
var swLat = Math.min.apply(null, lats);
174-
var neLon = Math.max.apply(null, lons);
175-
var neLat = Math.max.apply(null, lats);
176-
177-
map.setCameraBounds({
178-
bounds: [swLon, swLat, neLon, neLat],
179-
padding: 50
180-
});
181-
}
182-
};
149+
var client = new atlas.service.Client(subscriptionKey);
183150
```
184151

185-
3. Add the following code to the *script* block to build the query and send the XMLHttpRequest to the Maps Search service:
152+
3. Add the following script block to build the query. It uses the Fuzzy Search Service, which is a basic search API of the Search Service. Fuzzy Search Service handles most fuzzy inputs like any combination of address and point of interest (POI) tokens. It searches for nearby Gasoline Stations within the specified radius. The response is then parsed into GeoJSON format and converted into point features, which are added to the map as pins. The last part of the script adds camera bounds for the map by using the Map's [setCameraBounds](https://docs.microsoft.com/javascript/api/azure-maps-control/models.cameraboundsoptions?view=azure-iot-typescript-latest) property.
186153

187154
```JavaScript
188-
var url = "https://atlas.microsoft.com/search/fuzzy/json?";
189-
url += "api-version=1.0";
190-
url += "&query=gasoline%20station";
191-
url += "&subscription-key=" + MapsAccountKey;
192-
url += "&lat=47.6292";
193-
url += "&lon=-122.2337";
194-
url += "&radius=100000";
195-
196-
xhttp.open("GET", url, true);
197-
xhttp.send();
198-
```
199-
This snippet uses the basic search API of the Search Service, called the **Fuzzy Search**. It handles the most fuzzy of inputs, including any combination of address or point of interest (POI) tokens. It searches for nearby **gasoline stations** within a specified radius of the given latitude and longitude coordinates. It uses your account's primary key provided earlier in the sample file to make the call to Maps. It returns the results as latitude/longitude pairs for the locations found.
200-
155+
client.search.getSearchFuzzy("gasoline station", {
156+
lat: 47.6292,
157+
lon: -122.2337,
158+
radius: 100000
159+
}).then(response => {
160+
// Parse the response into GeoJSON
161+
var geojsonResponse = new atlas.service.geojson.GeoJsonSearchResponse(response);
162+
163+
// Create the point features that will be added to the map as pins
164+
var searchPins = geojsonResponse.getGeoJsonResults().features.map(poiResult => {
165+
var poiPosition = [poiResult.properties.position.lon, poiResult.properties.position.lat];
166+
return new atlas.data.Feature(new atlas.data.Point(poiPosition), {
167+
name: poiResult.properties.poi.name,
168+
address: poiResult.properties.address.freeformAddress,
169+
position: poiPosition[1] + ", " + poiPosition[0]
170+
});
171+
});
172+
173+
// Add pins to the map for each POI
174+
map.addPins(searchPins, {
175+
name: searchLayerName
176+
});
177+
178+
// Set the camera bounds
179+
map.setCameraBounds({
180+
bounds: geojsonResponse.getGeoJsonResults().bbox,
181+
padding: 50
182+
);
183+
});
184+
```
201185
4. Save the **MapSearch.html** file and refresh your browser. You should now see that the map is centered on Seattle and blue pins mark the locations of gasoline stations in the area.
202186

203187
![View the map with search results](./media/tutorial-search-location/pins-map.png)
204188

205-
5. You can see the raw data that the map is rendering by taking the XMLHTTPRequest that you build in the file and entering it in your browser. Replace \<your account key\> with your primary key.
189+
5. You can see the raw data that the map is rendering by entering the following HTTPRequest in your browser. Replace \<your account key\> with your primary key.
206190

207191
```http
208192
https://atlas.microsoft.com/search/fuzzy/json?api-version=1.0&query=gasoline%20station&subscription-key=<your account key>&lat=47.6292&lon=-122.2337&radius=100000
@@ -232,7 +216,7 @@ The map that we've made so far only looks at the latitude/longitude data for the
232216
popupContentElement.appendChild(popupAddressElement);
233217

234218
var popupPositionElement = document.createElement("div");
235-
popupPositionElement.innerText = e.features[0].properties.name;
219+
popupPositionElement.innerText = e.features[0].properties.position;
236220
popupContentElement.appendChild(popupPositionElement);
237221

238222
popup.setPopupOptions({

0 commit comments

Comments
 (0)