You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -126,10 +127,12 @@ The Map Control API is a convenient client library that allows you to easily int
126
127
127
128
## Add search capabilities
128
129
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.
130
131
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
132
133
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
+
133
136
```JavaScript
134
137
// Initialize the pin layer for search results to the map
135
138
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
140
143
});
141
144
```
142
145
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.
144
147
145
148
```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) {
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);
183
150
```
184
151
185
-
3. Add the following code to the *script* block to build the queryand 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.
186
153
187
154
```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.
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.
202
186
203
187

204
188
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 \<youraccountkey\> 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 \<youraccountkey\> with your primary key.
0 commit comments