@@ -4,7 +4,7 @@ titleSuffix: Azure Maps
4
4
description : How to develop applications that incorporate Azure Maps using the C# SDK Developers Guide.
5
5
author : sinnypan
6
6
ms.author : sipa
7
- ms.date : 11/11/2021
7
+ ms.date : 08/27/2024
8
8
ms.topic : how-to
9
9
ms.service : azure-maps
10
10
ms.subservice : rest-sdk
@@ -139,6 +139,11 @@ var client = new MapsSearchClient(credential);
139
139
Call the ` GetGeocoding ` method to get the coordinate of an address.
140
140
141
141
``` csharp
142
+ using System ;
143
+ using Azure ;
144
+ using Azure .Maps .Search ;
145
+ using Azure .Maps .Search .Models ;
146
+
142
147
// Use Azure Maps subscription key authentication
143
148
var subscriptionKey = Environment .GetEnvironmentVariable (" SUBSCRIPTION_KEY" ) ?? string .Empty ;
144
149
var credential = new AzureKeyCredential (subscriptionKey );
@@ -147,19 +152,90 @@ var client = new MapsSearchClient(credential);
147
152
Response < GeocodingResponse > searchResult = client .GetGeocoding (
148
153
" 1 Microsoft Way, Redmond, WA 98052" );
149
154
150
- Console .WriteLine ($" The Coordinate: ({searchResult .Value .Features [0 ].Geometry .Coordinates })" );
155
+ for (int i = 0 ; i < searchResult .Value .Features .Count ; i ++ )
156
+ {
157
+ Console .WriteLine (" Coordinate:" + string .Join (" ," , searchResult .Value .Features [i ].Geometry .Coordinates ));
158
+ }
159
+ ```
160
+
161
+ ## Batch geocode addresses
162
+
163
+ This sample demonstrates how to perform batch search address.
164
+
165
+ ``` csharp
166
+ using System ;
167
+ using Azure ;
168
+ using Azure .Maps .Search ;
169
+ using System .Collections .Generic ;
170
+ using Azure .Maps .Search .Models ;
171
+ using Azure .Maps .Search .Models .Queries ;
172
+
173
+ // Use Azure Maps subscription key authentication
174
+ var subscriptionKey = Environment .GetEnvironmentVariable (" SUBSCRIPTION_KEY" ) ?? string .Empty ;
175
+ var credential = new AzureKeyCredential (subscriptionKey );
176
+ var client = new MapsSearchClient (credential );
177
+
178
+ List < GeocodingQuery > queries = new List <GeocodingQuery >
179
+ {
180
+ new GeocodingQuery ()
181
+ {
182
+ Query = " 15171 NE 24th St, Redmond, WA 98052, United States"
183
+ },
184
+ new GeocodingQuery ()
185
+ {
186
+ AddressLine = " 400 Broad St"
187
+ },
188
+ };
189
+ Response < GeocodingBatchResponse > results = client .GetGeocodingBatch (queries );
190
+
191
+ // Print coordinates
192
+ for (var i = 0 ; i < results .Value .BatchItems .Count ; i ++ )
193
+ {
194
+ for (var j = 0 ; j < results .Value .BatchItems [i ].Features .Count ; j ++ )
195
+ {
196
+ Console .WriteLine (" Coordinates: " + string .Join (" ," , results .Value .BatchItems [i ].Features [j ].Geometry .Coordinates ));
197
+ }
198
+ }
199
+ ```
200
+
201
+ ## Reverse geocode a coordinates
202
+
203
+ You can translate coordinates into human-readable street addresses. This process is also called reverse geocoding.
204
+
205
+ ``` csharp
206
+ using System ;
207
+ using Azure ;
208
+ using Azure .Maps .Search ;
209
+ using Azure .Core .GeoJson ;
210
+ using Azure .Maps .Search .Models ;
211
+
212
+ // Use Azure Maps subscription key authentication
213
+ var subscriptionKey = Environment .GetEnvironmentVariable (" SUBSCRIPTION_KEY" ) ?? string .Empty ;
214
+ var credential = new AzureKeyCredential (subscriptionKey );
215
+ var client = new MapsSearchClient (credential );
216
+
217
+ GeoPosition coordinates = new GeoPosition (- 122 . 138685 , 47 . 6305637 );
218
+ Response < GeocodingResponse > result = client .GetReverseGeocoding (coordinates );
219
+
220
+ // Print addresses
221
+ for (int i = 0 ; i < result .Value .Features .Count ; i ++ )
222
+ {
223
+ Console .WriteLine (result .Value .Features [i ].Properties .Address .FormattedAddress );
224
+ }
151
225
```
152
226
153
227
## Batch reverse geocode a set of coordinates
154
228
155
229
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.
156
230
157
231
``` csharp
158
- using system ;
159
- using Azure ;
232
+ using System ;
233
+ using Azure ;
234
+ using Azure .Maps .Search ;
235
+ using System .Collections .Generic ;
160
236
using Azure .Core .GeoJson ;
161
- using Azure .Maps .Search ;
162
237
using Azure .Maps .Search .Models ;
238
+ using Azure .Maps .Search .Models .Queries ;
163
239
164
240
// Use Azure Maps subscription key authentication
165
241
var subscriptionKey = Environment .GetEnvironmentVariable (" SUBSCRIPTION_KEY" ) ?? string .Empty ;
@@ -170,16 +246,62 @@ List<ReverseGeocodingQuery> items = new List<ReverseGeocodingQuery>
170
246
{
171
247
new ReverseGeocodingQuery ()
172
248
{
173
- Coordinates = new GeoPosition (- 122 . 34255 , 47 . 0 )
249
+ Coordinates = new GeoPosition (- 122 . 349309 , 47 . 620498 )
174
250
},
175
251
new ReverseGeocodingQuery ()
176
252
{
177
- Coordinates = new GeoPosition (- 122 . 34255 , 47 . 0 )
253
+ Coordinates = new GeoPosition (- 122 . 138679 , 47 . 630356 ),
254
+ ResultTypes = new List <ReverseGeocodingResultTypeEnum >(){ ReverseGeocodingResultTypeEnum .Address , ReverseGeocodingResultTypeEnum .Neighborhood }
178
255
},
179
256
};
180
- Response < GeocodingBatchResponse > = client .GetReverseGeocodingBatch (items );
257
+ Response < GeocodingBatchResponse > result = client .GetReverseGeocodingBatch (items );
258
+ // Print addresses
259
+ for (var i = 0 ; i < result .Value .BatchItems .Count ; i ++ )
260
+ {
261
+ Console .WriteLine (result .Value .BatchItems [i ].Features [0 ].Properties .Address .AddressLine );
262
+ Console .WriteLine (result .Value .BatchItems [i ].Features [0 ].Properties .Address .Neighborhood );
263
+ }
181
264
```
182
265
266
+ ## Get polygons for a given location
267
+
268
+ This sample demonstrates how to search polygons.
269
+
270
+ ``` csharp
271
+ using System ;
272
+ using Azure ;
273
+ using Azure .Maps .Search ;
274
+ using Azure .Core .GeoJson ;
275
+ using Azure .Maps .Search .Models ;
276
+ using Azure .Maps .Search .Models .Options ;
277
+
278
+ // Use Azure Maps subscription key authentication
279
+ var subscriptionKey = Environment .GetEnvironmentVariable (" SUBSCRIPTION_KEY" ) ?? string .Empty ;
280
+ var credential = new AzureKeyCredential (subscriptionKey );
281
+ var client = new MapsSearchClient (credential );
282
+
283
+ GetPolygonOptions options = new GetPolygonOptions ()
284
+ {
285
+ Coordinates = new GeoPosition (- 122 . 204141 , 47 . 61256 ),
286
+ ResultType = BoundaryResultTypeEnum .Locality ,
287
+ Resolution = ResolutionEnum .Small ,
288
+ };
289
+ Response < Boundary > result = client .GetPolygon (options );
290
+
291
+ var count = ((GeoJsonPolygon )((GeoJsonGeometryCollection )result .Value .Geometry ).Geometries [0 ]).Coordinates .Count ;
292
+ for (var i = 0 ; i < count ; i ++ )
293
+ {
294
+ var coorCount = ((GeoJsonPolygon )((GeoJsonGeometryCollection )result .Value .Geometry ).Geometries [0 ]).Coordinates [i ].Count ;
295
+ for (var j = 0 ; j < coorCount ; j ++ )
296
+ {
297
+ Console .WriteLine (string .Join (" ," ,((GeoJsonPolygon )((GeoJsonGeometryCollection )result .Value .Geometry ).Geometries [0 ]).Coordinates [i ][j ]));
298
+ }
299
+ }
300
+ ```
301
+
302
+ ## Using V1 SDKs for Search and Render
303
+
304
+ For more information on using Search v1, see [ Azure Maps Search client library for .NET] ( https://www.nuget.org/packages/Azure.Maps.Search/1.0.0-beta.5 ) . For more information on using Render v1, see [ Azure Maps Render client library for .NET] ( https://www.nuget.org/packages/Azure.Maps.Rendering/1.0.0-beta.3 ) .
183
305
184
306
## Additional information
185
307
0 commit comments