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
Azure Maps Python SDK supports Python version 3.7 or later. For more information on future Python versions, see [Azure SDK for Python version support policy].
53
+
Azure Maps Python SDK supports Python version 3.8 or later. For more information on future Python versions, see [Azure SDK for Python version support policy].
The following code snippet demonstrates how, in a simple console application, to import the `Azure.Maps.Search` package and perform a fuzzy search on “Starbucks” near Seattle. This example uses subscription key credentials to authenticate MapsSearchClient. In `demo.py`:
148
+
The following code snippet demonstrates how, in a simple console application, to obtain longitude and latitude coordinates for a given address. This example uses subscription key credentials to authenticate MapsSearchClient. In `demo.py`:
149
149
150
150
```Python
151
-
import os
152
-
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import HttpResponseError
177
154
178
-
This sample code instantiates `AzureKeyCredential` with the Azure Maps subscription key, then uses it to instantiate the `MapsSearchClient` object. The methods provided by `MapsSearchClient` forward the request to the Azure Maps REST endpoints. In the end, the program iterates through the results and prints the address and coordinates for each result.
You should see a list of Starbucks address and coordinate results:
187
-
188
-
```text
189
-
* 1912 Pike Place
190
-
Seattle US 98101
191
-
Coordinate: 47.61016, -122.34248
192
-
* 2118 Westlake Avenue
193
-
Seattle US 98121
194
-
Coordinate: 47.61731, -122.33782
195
-
* 2601 Elliott Avenue
196
-
Seattle US 98121
197
-
Coordinate: 47.61426, -122.35261
198
-
* 1730 Howell Street
199
-
Seattle US 98101
200
-
Coordinate: 47.61716, -122.3298
201
-
* 220 1st Avenue South
202
-
Seattle US 98104
203
-
Coordinate: 47.60027, -122.3338
204
-
* 400 Occidental Avenue South
205
-
Seattle US 98104
206
-
Coordinate: 47.5991, -122.33278
207
-
* 1600 East Olive Way
208
-
Seattle US 98102
209
-
Coordinate: 47.61948, -122.32505
210
-
* 500 Mercer Street
211
-
Seattle US 98109
212
-
Coordinate: 47.62501, -122.34687
213
-
* 505 5Th Ave S
214
-
Seattle US 98104
215
-
Coordinate: 47.59768, -122.32849
216
-
* 425 Queen Anne Avenue North
217
-
Seattle US 98109
218
-
Coordinate: 47.62301, -122.3571
173
+
except HttpResponseError as exception:
174
+
if exception.error isnotNone:
175
+
print(f"Error Code: {exception.error.code}")
176
+
print(f"Message: {exception.error.message}")
177
+
178
+
if__name__=='__main__':
179
+
geocode()
219
180
```
220
181
221
-
## Search an Address
182
+
This sample code instantiates `AzureKeyCredential` with the Azure Maps subscription key, then uses it to instantiate the `MapsSearchClient` object. The methods provided by `MapsSearchClient` forward the request to the Azure Maps REST endpoints. In the end, the program iterates through the results and prints the coordinates for each result.
183
+
222
184
223
-
Call the `SearchAddress` method to get the coordinate of an address. Modify the Main program from the sample as follows:
185
+
## Batch geocode addresses
186
+
187
+
This sample demonstrates how to perform batch search address:
224
188
225
189
```Python
226
190
import os
227
-
from azure.core.credentials import AzureKeyCredential
228
-
from azure.maps.search import MapsSearchClient
229
191
230
-
defsearch_address():
231
-
subscription_key = os.getenv("SUBSCRIPTION_KEY")
192
+
from azure.core.exceptions import HttpResponseError
{"query": "15127 NE 24th Street, Redmond, WA 98052"},
206
+
],
207
+
},)
250
208
251
-
The `SearchAddress` method returns results ordered by confidence score and prints the coordinates of the first result.
209
+
ifnot result.get('batchItems', False):
210
+
print("No batchItems in geocoding")
211
+
return
252
212
253
-
## Batch reverse search
213
+
for item in result['batchItems']:
214
+
ifnot item.get('features', False):
215
+
print(f"No features in item: {item}")
216
+
continue
254
217
255
-
Azure Maps Search also provides some batch query methods. These methods return long-running operations (LRO) objects. The requests might not return all the results immediately, so users can choose to wait until completion or query the result periodically. The following examples demonstrate how to call the batched reverse search method.
Since these return LRO objects, you need the `asyncio` method included in the `aiohttp` package:
222
+
except HttpResponseError as exception:
223
+
if exception.error isnotNone:
224
+
print(f"Error Code: {exception.error.code}")
225
+
print(f"Message: {exception.error.message}")
258
226
259
-
```powershell
260
-
pip install aiohttp
227
+
if__name__=='__main__':
228
+
geocode_batch()
261
229
```
262
230
263
-
```Python
264
-
import asyncio
231
+
232
+
## Make a Reverse Address Search to translate coordinate location to street address
233
+
234
+
You can translate coordinates into human-readable street addresses. This process is also called reverse geocoding. This is often used for applications that consume GPS feeds and want to discover addresses at specific coordinate points.
235
+
236
+
```python
265
237
import os
266
-
from azure.core.credentials import AzureKeyCredential
267
-
from azure.maps.search.aio import MapsSearchClient
268
238
269
-
asyncdefbegin_reverse_search_address_batch():
270
-
subscription_key = os.getenv("SUBSCRIPTION_KEY")
239
+
from azure.core.exceptions import HttpResponseError
In the above example, three queries are passed to the batched reverse search request. To get the LRO results, the request creates a batch request with a batch ID as result that can be used to fetch batch response later. The LRO results are cached on the server side for 14 days.
292
268
293
-
The following example demonstrates the process of calling the batch ID and retrieving the operation results of the batch request:
269
+
## Batch request for reverse geocoding
270
+
271
+
This sample demonstrates how to perform reverse search by given coordinates in batch.
294
272
295
273
```python
296
-
import asyncio
297
274
import os
298
275
from azure.core.credentials import AzureKeyCredential
299
-
from azure.maps.search.aio import MapsSearchClient
0 commit comments