Skip to content

Commit c47b544

Browse files
authored
Merge pull request #284491 from jecmenicanikola/patch-1
Update how-to-dev-guide-csharp-sdk.md
2 parents e0010e7 + 28811d5 commit c47b544

File tree

1 file changed

+17
-199
lines changed

1 file changed

+17
-199
lines changed

articles/azure-maps/how-to-dev-guide-csharp-sdk.md

Lines changed: 17 additions & 199 deletions
Original file line numberDiff line numberDiff line change
@@ -134,183 +134,25 @@ var subscriptionKey = Environment.GetEnvironmentVariable("SUBSCRIPTION_KEY") ??
134134
var credential = new AzureKeyCredential(subscriptionKey);
135135
var client = new MapsSearchClient(credential);
136136
```
137+
## Geocode an address
137138

138-
### Fuzzy search an entity
139-
140-
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. In `Program.cs`:
139+
Call the `GetGeocoding` method to get the coordinate of an address.
141140

142141
```csharp
143-
using System;
144-
using Azure;
145-
using Azure.Core.GeoJson;
146-
using Azure.Maps.Search;
147-
using Azure.Maps.Search.Models;
148-
149142
// Use Azure Maps subscription key authentication
150143
var subscriptionKey = Environment.GetEnvironmentVariable("SUBSCRIPTION_KEY") ?? string.Empty;
151144
var credential = new AzureKeyCredential(subscriptionKey);
152145
var client = new MapsSearchClient(credential);
153146

154-
SearchAddressResult searchResult = client.FuzzySearch(
155-
"Starbucks", new FuzzySearchOptions
156-
{
157-
Coordinates = new GeoPosition(-122.34255, 47.61010),
158-
Language = SearchLanguage.EnglishUsa
159-
});
160-
161-
162-
// Print the search results
163-
foreach (var result in searchResult.Results)
164-
{
165-
Console.WriteLine($"""
166-
* {result.Address.StreetNumber} {result.Address.StreetName}
167-
{result.Address.Municipality} {result.Address.CountryCode} {result.Address.PostalCode}
168-
Coordinate: ({result.Position.Latitude:F4}, {result.Position.Longitude:F4})
169-
""");
170-
}
171-
```
172-
173-
The above code snippet demonstrates how to create a `MapsSearchClient` object using your Azure credentials, then uses its [FuzzySearch] method, passing in the point of interest (POI) name "_Starbucks_" and coordinates _GeoPosition(-122.31, 47.61)_. The SDK packages and sends the results to the Azure Maps REST endpoints. When the search results are returned, they're written out to the screen using `Console.WriteLine`.
174-
175-
The following libraries are used:
147+
Response<GeocodingResponse> searchResult = client.GetGeocoding(
148+
"1 Microsoft Way, Redmond, WA 98052");
176149

177-
1. `Azure.Maps.Search` is required for the `MapsSearchClient` class.
178-
1. `Azure.Maps.Search.Models` is required for the `SearchAddressResult` class.
179-
1. `Azure.Core.GeoJson` is required for the `GeoPosition` struct used by the `FuzzySearchOptions` class.
180-
181-
To run your application, go to the project folder and execute `dotnet run` in PowerShell:
182-
183-
```powershell
184-
dotnet run
185-
```
186-
187-
You should see a list of Starbucks address and coordinate results:
188-
189-
```text
190-
* 1912 Pike Place
191-
Seattle US 98101
192-
Coordinate: 47.61016, -122.34248
193-
* 2118 Westlake Avenue
194-
Seattle US 98121
195-
Coordinate: 47.61731, -122.33782
196-
* 2601 Elliott Avenue
197-
Seattle US 98121
198-
Coordinate: 47.61426, -122.35261
199-
* 1730 Howell Street
200-
Seattle US 98101
201-
Coordinate: 47.61716, -122.3298
202-
* 220 1st Avenue South
203-
Seattle US 98104
204-
Coordinate: 47.60027, -122.3338
205-
* 400 Occidental Avenue South
206-
Seattle US 98104
207-
Coordinate: 47.5991, -122.33278
208-
* 1600 East Olive Way
209-
Seattle US 98102
210-
Coordinate: 47.61948, -122.32505
211-
* 500 Mercer Street
212-
Seattle US 98109
213-
Coordinate: 47.62501, -122.34687
214-
* 505 5Th Ave S
215-
Seattle US 98104
216-
Coordinate: 47.59768, -122.32849
217-
* 425 Queen Anne Avenue North
218-
Seattle US 98109
219-
Coordinate: 47.62301, -122.3571
150+
Console.WriteLine($"The Coordinate: ({searchResult.Value.Features[0].Geometry.Coordinates})");
220151
```
221152

222-
## Search an address
223-
224-
Call the `SearchAddress` method to get the coordinate of an address. Modify the Main program from the sample as follows:
225-
226-
```csharp
227-
// Use Azure Maps subscription key authentication
228-
var subscriptionKey = Environment.GetEnvironmentVariable("SUBSCRIPTION_KEY") ?? string.Empty;
229-
var credential = new AzureKeyCredential(subscriptionKey);
230-
var client = new MapsSearchClient(credential);
231-
232-
SearchAddressResult searchResult = client.SearchAddress(
233-
"1301 Alaskan Way, Seattle, WA 98101, US");
234-
235-
if (searchResult.Results.Count > 0)
236-
{
237-
SearchAddressResultItem result = searchResult.Results.First();
238-
Console.WriteLine($"The Coordinate: ({result.Position.Latitude:F4}, {result.Position.Longitude:F4})");
239-
}
240-
```
241-
242-
The `SearchAddress` method returns results ordered by confidence score and since `searchResult.Results.First()` is used, only the coordinates of the first result are returned.
243-
244-
## Batch reverse search
245-
246-
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 example demonstrates how to call the batched reverse search methods:
247-
248-
```csharp
249-
var queries = new List<ReverseSearchAddressQuery>()
250-
{
251-
new ReverseSearchAddressQuery(new ReverseSearchOptions()
252-
{
253-
Coordinates = new GeoPosition(2.294911, 48.858561)
254-
}),
255-
new ReverseSearchAddressQuery(new ReverseSearchOptions()
256-
{
257-
Coordinates = new GeoPosition(-122.127896, 47.639765),
258-
RadiusInMeters = 5000
259-
})
260-
};
261-
```
153+
## Batch reverse geocode a set of coordinates
262154

263-
In the above example, two queries are passed to the batched reverse search request. To get the LRO results, you have few options. The first option is to pass `WaitUntil.Completed` to the method. The request waits until all requests are finished and return the results:
264-
265-
```csharp
266-
// Wait until the LRO return batch results
267-
Response<ReverseSearchAddressBatchOperation> waitUntilCompletedResults = client.ReverseSearchAddressBatch(WaitUntil.Completed, queries);
268-
269-
// Print the result addresses
270-
printReverseBatchAddresses(waitUntilCompletedResults.Value);
271-
```
272-
273-
Another option is to pass `WaitUntil.Started`. The request returns immediately, and you need to manually poll the results:
274-
275-
```csharp
276-
// Manual polling the batch results
277-
Response<ReverseSearchAddressBatchOperation> manualPollingOperation = client.ReverseSearchAddressBatch(WaitUntil.Started, queries);
278-
279-
// Keep polling until we get the results
280-
while (true)
281-
{
282-
manualPollingOperation.Value.UpdateStatus();
283-
if (manualPollingOperation.Value.HasCompleted) break;
284-
Task.Delay(1000);
285-
}
286-
printReverseBatchAddresses(manualPollingOperation);
287-
```
288-
289-
We can also call `WaitUntilCompletion()` to explicitly wait for the result:
290-
291-
```csharp
292-
Response<ReverseSearchAddressBatchOperation> manualPollingResult = manualPollingResults.WaitUntilCompleted();
293-
294-
printReverseBatchAddresses(manualPollingResult.Value);
295-
```
296-
297-
The third method requires the operation ID to get the results, which is cached on the server side for 14 days:
298-
299-
```csharp
300-
ReverseSearchAddressBatchOperation longRunningOperation = client.ReverseSearchAddressBatch(WaitUntil.Started, queries);
301-
302-
// Get batch results by ID
303-
string operationId = longRunningOperation.Value.Id;
304-
305-
// After the LRO completes, create a new operation
306-
// to get the results from the server
307-
ReverseSearchAddressBatchOperation newOperation = new ReverseSearchAddressBatchOperation(client, operationId);
308-
Response<ReverseSearchAddressBatchOperation> newOperationResult = newOperation.WaitForCompletion();
309-
310-
printReverseBatchAddresses(newOperationResult);
311-
```
312-
313-
The complete code for reverse address batch search with operation ID:
155+
Azure Maps Search also provides some batch query APIs. The Reverse Geocoding Batch API sends batches of queries to [Reverse Geocoding API](/rest/api/maps/search/get-reverse-geocoding) using just a single API call. The API allows caller to batch up to **100** queries.
314156

315157
```csharp
316158
using system;
@@ -322,46 +164,23 @@ using Azure.Maps.Search.Models;
322164
// Use Azure Maps subscription key authentication
323165
var subscriptionKey = Environment.GetEnvironmentVariable("SUBSCRIPTION_KEY") ?? string.Empty;
324166
var credential = new AzureKeyCredential(subscriptionKey);
325-
var client = new MapsSearchClient(credential);
167+
var client = new MapsSearchClient(credential);
326168

327-
var queries = new List<ReverseSearchAddressQuery>()
169+
List<ReverseGeocodingQuery> items = new List<ReverseGeocodingQuery>
328170
{
329-
new ReverseSearchAddressQuery(new ReverseSearchOptions()
171+
new ReverseGeocodingQuery()
330172
{
331-
Coordinates = new GeoPosition(2.294911, 48.858561)
332-
}),
333-
new ReverseSearchAddressQuery(new ReverseSearchOptions()
173+
Coordinates = new GeoPosition(-122.34255, 47.0)
174+
},
175+
new ReverseGeocodingQuery()
334176
{
335-
Coordinates = new GeoPosition(-122.127896, 47.639765),
336-
RadiusInMeters = 5000
337-
})
177+
Coordinates = new GeoPosition(-122.34255, 47.0)
178+
},
338179
};
339-
340-
// Manual polling the batch results
341-
ReverseSearchAddressBatchOperation longRunningOperation = client.ReverseSearchAddressBatch(WaitUntil.Started, queries);
342-
343-
// Get batch results by ID
344-
string operationId = longRunningOperation.Id;
345-
346-
// A few days later, create a new operation and get the result from server
347-
ReverseSearchAddressBatchOperation newOperation = new ReverseSearchAddressBatchOperation(client, operationId);
348-
Response<ReverseSearchAddressBatchResult> newOperationResult = newOperation.WaitForCompletion();
349-
printReverseBatchAddresses(newOperationResult.Value);
350-
void printReverseBatchAddresses(ReverseSearchAddressBatchResult batchResult)
351-
{
352-
// Print the search results
353-
for (int i = 0; i < batchResult.Results.Count; i++)
354-
{
355-
Console.WriteLine($"Possible addresses for query {i}:");
356-
var result = batchResult.Results[i];
357-
foreach (var address in result.Addresses)
358-
{
359-
Console.WriteLine($"{address.Address.FreeformAddress}");
360-
}
361-
}
362-
}
180+
Response<GeocodingBatchResponse> = client.GetReverseGeocodingBatch(items);
363181
```
364182

183+
365184
## Additional information
366185

367186
The [Azure.Maps Namespace] in the .NET documentation.
@@ -372,7 +191,6 @@ The [Azure.Maps Namespace] in the .NET documentation.
372191
[Azure Maps account]: quick-demo-map-app.md#create-an-azure-maps-account
373192
[Azure.Maps Namespace]: /dotnet/api/azure.maps
374193
[defaultazurecredential.NET]: /dotnet/api/overview/azure/identity-readme#defaultazurecredential
375-
[FuzzySearch]: /dotnet/api/azure.maps.search.mapssearchclient.fuzzysearch
376194
[geolocation readme]: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/maps/Azure.Maps.Geolocation/README.md
377195
[geolocation sample]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/maps/Azure.Maps.Geolocation/samples
378196
[geolocation package]: https://www.nuget.org/packages/Azure.Maps.geolocation

0 commit comments

Comments
 (0)