azure-maps-search-dotnet
microsoft/skills
Integre os serviços baseados em localização do Azure Maps a aplicativos .NET para geocodificação, cálculo de rotas, renderização, geolocalização e dados meteorológicos.
...Expandir tudoAzure Maps (.NET)
O SDK do Azure Maps para .NET oferece serviços baseados em localização: geocodificação, cálculo de rotas, renderização, geolocalização e previsão do tempo.
Instalação
# Pesquisa (geocodificação, geocodificação reversa)
dotnet add package Azure.Maps.Search --prerelease
# Roteamento (direções, matriz de rotas)
dotnet add package Azure.Maps.Routing --prerelease
# Renderização (blocos de mapa, imagens estáticas)
dotnet add package Azure.Maps.Rendering --prerelease
# Geolocalização (IP para localização)
dotnet add package Azure.Maps.Geolocation --prerelease
# Clima
dotnet add package Azure.Maps.Weather --prerelease
# Gerenciamento de recursos (gerenciamento de contas, tokens SAS)
dotnet add package Azure.ResourceManager.Maps --prerelease
# Necessário para autenticação
dotnet add package Azure.Identity
Versões atuais:
Azure.Maps.Search: v2.0.0-beta.5Azure.Maps.Routing: v1.0.0-beta.4Azure.Maps.Rendering: v2.0.0-beta.1Azure.Maps.Geolocation: v1.0.0-beta.3Azure.ResourceManager.Maps: v1.1.0-beta.2
Variáveis de ambiente
AZURE_MAPS_SUBSCRIPTION_KEY= # Necessário apenas para autenticação com AzureKeyCredential
AZURE_MAPS_CLIENT_ID= # Obrigatório: ID do cliente do Azure Maps
AZURE_TOKEN_CREDENTIALS=prod # Necessário apenas se DefaultAzureCredential for usado em produção
Autenticação
Chave de assinatura (chave compartilhada)
using Azure;
using Azure.Maps.Search;
var subscriptionKey = Environment.GetEnvironmentVariable("AZURE_MAPS_SUBSCRIPTION_KEY");
var credential = new AzureKeyCredential(subscriptionKey);
var client = new MapsSearchClient(credential);
Credencial de token do Microsoft Entra
using Azure.Identity;
using Azure.Maps.Search;
// Desenvolvimento local: DefaultAzureCredential. Produção: defina AZURE_TOKEN_CREDENTIALS=prod ou AZURE_TOKEN_CREDENTIALS=
var credential = new DefaultAzureCredential(
DefaultAzureCredential.DefaultEnvironmentVariableName
);
// Ou use uma credencial específica diretamente em produção:
// Consulte https://learn.microsoft.com/dotnet/api/overview/azure/identity-readme?view=azure-dotnet#credential-classes
// var credential = new ManagedIdentityCredential();
var clientId = Environment.GetEnvironmentVariable("AZURE_MAPS_CLIENT_ID");
var client = new MapsSearchClient(credential, clientId);
Assinatura de Acesso Compartilhado (SAS)
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Maps;
using Azure.ResourceManager.Maps.Models;
using Azure.Maps.Search;
// Autenticar com o Azure Resource Manager
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
// Obter o recurso da conta do Maps
ResourceIdentifier mapsAccountResourceId = MapsAccountResource.CreateResourceIdentifier(
subscriptionId, resourceGroupName, accountName);
MapsAccountResource mapsAccount = armClient.GetMapsAccountResource(mapsAccountResourceId);
// Gerar token SAS
MapsAccountSasContent sasContent = new MapsAccountSasContent(
MapsSigningKey.PrimaryKey,
principalId,
maxRatePerSecond: 500,
start: DateTime.UtcNow.ToString("O"),
expiry: DateTime.UtcNow.AddDays(1).ToString("O"));
Resposta sas = mapsAccount.GetSas(sasContent);
// Criar cliente com o token SAS
var sasCredential = new AzureSasCredential(sas.Value.AccountSasToken);
var client = new MapsSearchClient(sasCredential);
Hierarquia do cliente
Azure.Maps.Search
└── MapsSearchClient
├── GetGeocoding() → Geocodificação de endereços
├── GetGeocodingBatch() → Geocodificação em lote
├── GetReverseGeocoding() → Coordenadas para endereço
├── GetReverseGeocodingBatch() → Geocodificação reversa em lote
└── GetPolygon() → Obter polígonos de limites
Azure.Maps.Routing
└── MapsRoutingClient
├── GetDirections() → Obter instruções de rota
├── GetImmediateRouteMatrix() → Matriz de rotas (síncrona, ≤100)
├── GetRouteMatrix() → Matriz de rotas (assíncrona, ≤700)
└── GetRouteRange() → Isócrona/alcance
Azure.Maps.Rendering
└── MapsRenderingClient
├── GetMapTile() → Blocos de mapa
├── GetMapStaticImage() → Imagens estáticas do mapa
└── GetCopyrightCaption() → Informações de direitos autorais
Azure.Maps.Geolocation
└── MapsGeolocationClient
└── GetCountryCode() → IP para país/região
Azure.Maps.Weather
└── MapsWeatherClient
├── GetCurrentWeatherConditions() → Condições meteorológicas atuais
├── GetDailyForecast() → Previsão diária
├── GetHourlyForecast() → Previsão por hora
└── GetSevereWeatherAlerts() → Alertas meteorológicos
Fluxos de trabalho principais
1. Geocodificação (Endereço para coordenadas)
using Azure;
using Azure.Maps.Search;
var credential = new AzureKeyCredential(subscriptionKey);
var client = new MapsSearchClient(credential);
Resposta result = client.GetGeocoding("1 Microsoft Way, Redmond, WA 98052");
foreach (var feature in result.Value.Features)
{
Console.WriteLine($"Coordenadas: {string.Join(",", feature.Geometry.Coordinates)}");
Console.WriteLine($"Endereço: {feature.Properties.Address.FormattedAddress}");
Console.WriteLine($"Confiança: {feature.Properties.Confidence}");
}
2. Geocodificação em lote
using Azure.Maps.Search.Models.Queries;
List queries = new List{
new GeocodingQuery() { Query = "400 Broad St, Seattle, WA" },
new GeocodingQuery() { Query = "1 Microsoft Way, Redmond, WA" },
new GeocodingQuery() { AddressLine = "Space Needle", Top = 1 },
};
Resposta results = client.GetGeocodingBatch(queries);
foreach (var batchItem in results.Value.BatchItems)
{
foreach (var feature in batchItem.Features)
{
Console.WriteLine($"Coordenadas: {string.Join(",", feature.Geometry.Coordinates)}");
}
}
3. Geocodificação reversa (coordenadas para endereço)
using Azure.Core.GeoJson;
GeoPosition coordenadas = new GeoPosition(-122.138685, 47.6305637);
Response resultado = client.GetReverseGeocoding(coordenadas);
foreach (var feature in result.Value.Features)
{
Console.WriteLine($"Endereço: {feature.Properties.Address.FormattedAddress}");
Console.WriteLine($"Localidade: {feature.Properties.Address.Locality}");
}
4. Obter polígono de limite
using Azure.Maps.Search.Models;
GetPolygonOptions opções = new GetPolygonOptions()
{
Coordenadas = new GeoPosition(-122.204141, 47.61256),
TipoDeResultado = BoundaryResultTypeEnum.Localidade,
Resolução = ResolutionEnum.Pequena,
};
Resposta result = client.GetPolygon(options);
Console.WriteLine($"Direitos autorais do polígono de limites: {result.Value.Properties?.Copyright}");
Console.WriteLine($"Número de polígonos: {result.Value.Geometry.Count}");
5. Direções de rota
using Azure;
using Azure.Core.GeoJson;
using Azure.Maps.Routing;
using Azure.Maps.Routing.Models;
var client = new MapsRoutingClient(new AzureKeyCredential(subscriptionKey));
List routePoints = new List()
{
new GeoPosition(-122,34, 47,61), // Seattle
new GeoPosition(-122,13, 47,64) // Redmond
};
RouteDirectionQuery query = new RouteDirectionQuery(routePoints);
Response result = client.GetDirections(query);
foreach (var route in result.Value.Routes)
{
Console.WriteLine($"Distância: {route.Summary.LengthInMeters} metros");
Console.WriteLine($"Duração: {route.Summary.TravelTimeDuration}");
foreach (RouteLeg leg in route.Legs)
{
Console.WriteLine($"Pontos do trecho: {leg.Points.Count}");
}
}
6. Direções da rota com opções
RouteDirectionOptions options = new RouteDirectionOptions()
{
RouteType = RouteType.Fastest,
UseTrafficData = true,
TravelMode = TravelMode.Bicycle,
Language = RoutingLanguage.EnglishUsa,
InstructionsType = RouteInstructionsType.Text,
};
RouteDirectionQuery query = new RouteDirectionQuery(routePoints)
{
RouteDirectionOptions = options
};
Response result = client.GetDirections(query);
7. Matriz de rotas
RouteMatrixQuery routeMatrixQuery = new RouteMatrixQuery
{
Origins = new List()
{
new GeoPosition(-122,34, 47,61),
new GeoPosition(-122,13, 47,64)
},
Destinations = new List()
{
new GeoPosition(-122,20, 47,62),
new GeoPosition(-122,40, 47,65)
},
};
// Síncrono (até 100 combinações de rotas)
Response result = client.GetImmediateRouteMatrix(routeMatrixQuery);
foreach (var cell in result.Value.Matrix.SelectMany(row => row))
{
Console.WriteLine($"Distância: {cell.Response?.RouteSummary?.LengthInMeters}");
Console.WriteLine($"Duração: {cell.Response?.RouteSummary?.TravelTimeDuration}");
}
// Assíncrono (até 700 combinações de rotas)
RouteMatrixOptions routeMatrixOptions = new RouteMatrixOptions(routeMatrixQuery)
{
TravelTimeType = TravelTimeType.All,
};
GetRouteMatrixOperation asyncResult = client.GetRouteMatrix(WaitUntil.Completed, routeMatrixOptions);
8. Intervalo de rotas (isócrona)
RouteRangeOptions options = new RouteRangeOptions(-122,34, 47,61)
{
TimeBudget = new TimeSpan(0, 20, 0) // 20 minutos
};
Resposta result = client.GetRouteRange(options);
// result.Value.ReachableRange contém o polígono
Console.WriteLine($"Pontos de contorno: {result.Value.ReachableRange.Boundary.Count}");
9. Obter blocos de mapa
using Azure;
using Azure.Maps.Rendering;
var client = new MapsRenderingClient(new AzureKeyCredential(subscriptionKey));
int zoom = 10;
int tileSize = 256;
// Converter coordenadas em índice de bloco
MapTileIndex tileIndex = MapsRenderingClient.PositionToTileXY(
new GeoPosition(13.3854, 52.517), zoom, tileSize);
// Obter bloco do mapa
GetMapTileOptions options = new GetMapTileOptions(
MapTileSetId.MicrosoftImagery,
new MapTileIndex(tileIndex.X, tileIndex.Y, zoom)
);
Response mapTile = client.GetMapTile(options);
// Salvar em arquivo
using (FileStream fileStream = File.Create("./MapTile.png"))
{
mapTile.Value.CopyTo(fileStream);
}
10. Geolocalização por IP
using System.Net;
using Azure;
using Azure.Maps.Geolocation;
var client = new MapsGeolocationClient(new AzureKeyCredential(subscriptionKey));
IPAddress ipAddress = IPAddress.Parse("2001:4898:80e8:b::189");
Response result = client.GetCountryCode(ipAddress);
Console.WriteLine($"Código ISO do país: {result.Value.IsoCode}");
11. Clima atual
using Azure;
using Azure.Core.GeoJson;
using Azure.Maps.Weather;
var cliente = new MapsWeatherClient(new AzureKeyCredential(chaveDaAssinatura));
var position = new GeoPosition(-122.13071, 47.64011);
var options = new GetCurrentWeatherConditionsOptions(position);
Resposta result = client.GetCurrentWeatherConditions(options);
foreach (var condition in result.Value.Results)
{
Console.WriteLine($"Temperatura: {condition.Temperature.Value} {condition.Temperature.Unit}");
Console.WriteLine($"Clima: {condition.Phrase}");
Console.WriteLine($"Umidade: {condition.RelativeHumidity}%");
}
Referência de tipos de chave
Pesquisar pacote
| Tipo | Finalidade |
|---|---|
MapsSearchClient |
Cliente principal para operações de pesquisa |
Resposta de geocodificação |
Resultado da geocodificação |
Resposta de geocodificação em lote |
Resultado da geocodificação em lote |
GeocodingQuery |
Consulta para geocodificação em lote |
ReverseGeocodingQuery |
Consulta para geocodificação reversa em lote |
GetPolygonOptions |
Opções para recuperação de polígonos |
Limite |
Resultado do polígono de limite |
BoundaryResultTypeEnum |
Tipo de limite (Localidade, Distrito Administrativo, etc.) |
ResolutionEnum |
Resolução do polígono (Pequeno, Médio, Grande) |
Pacote de roteamento
| Tipo | Finalidade |
|---|---|
MapsRoutingClient |
Cliente principal para operações de roteamento |
RouteDirectionQuery |
Consulta de direções de rota |
RouteDirectionOptions |
Opções de cálculo de rota |
RouteDirections |
Resultado das instruções de rota |
RouteLeg |
Segmento de um trajeto |
RouteMatrixQuery |
Consulta à matriz de rotas |
Resultado da matriz de rotas |
Resultado da matriz de rotas |
RouteRangeOptions |
Opções para isócrona |
RouteRangeResult |
Resultado da isócrona |
RouteType |
Tipo de rota (Mais rápida, Mais curta, Econômica, Emocionante) |
Modo de viagem |
Modo de deslocamento (Carro, Caminhão, Bicicleta, Pedestre) |
Pacote de renderização
| Tipo | Finalidade |
|---|---|
MapsRenderingClient |
Cliente principal para renderização |
GetMapTileOptions |
Opções de blocos do mapa |
MapTileIndex |
Coordenadas do bloco (X, Y, Zoom) |
MapTileSetId |
Identificador do conjunto de blocos |
Tipos comuns
| Tipo | Finalidade |
|---|---|
GeoPosition |
Posição geográfica (longitude, latitude) |
GeoBoundingBox |
Caixa delimitadora da área geográfica |
Melhores práticas
- Use o Entra ID em ambiente de produção — É preferível em relação às chaves de assinatura
- Operações em lote — Use a geocodificação em lote para vários endereços
- Armazeneos resultados em cache — Os resultados da geocodificação não mudam com frequência
- Use tamanhos de blocos adequados — 256 ou 512 pixels, dependendo da tela
- Lide com limites de taxa — Implemente o backoff exponencial
- Use matriz de rotas assíncrona — Para cálculos de matrizes grandes (>100)
- Leve em conta os dados de trânsito — Defina
UseTrafficData = truepara estimativas precisas de tempo de chegada
Tratamento de erros
try
{
Response result = client.GetGeocoding(address);
}
catch (RequestFailedException ex)
{
Console.WriteLine($"Status: {ex.Status}");
Console.WriteLine($"Erro: {ex.Message}");
switch (ex.Status)
{
case 400:
// Parâmetros de solicitação inválidos
break;
case 401:
// Falha na autenticação
break;
case 429:
// Limite de taxa atingido — implementar backoff
break;
}
}
SDKs relacionados
| SDK | Finalidade | Instalar |
|---|---|---|
Azure.Maps.Search |
Geocodificação, pesquisa | dotnet add package Azure.Maps.Search --prerelease |
Azure.Maps.Routing |
Rotas, matriz | dotnet add package Azure.Maps.Routing --prerelease |
Azure.Maps.Rendering |
Blocos de mapa, imagens | dotnet add package Azure.Maps.Rendering --prerelease |
Azure.Maps.Geolocation |
Geolocalização por IP | dotnet add package Azure.Maps.Geolocation --prerelease |
Azure.Maps.Weather |
Dados meteorológicos | dotnet add package Azure.Maps.Weather --prerelease |
Azure.ResourceManager.Maps |
Gerenciamento de contas | dotnet add package Azure.ResourceManager.Maps --prerelease |
Links de referência
| Recurso | URL |
|---|---|
| Documentação do Azure Maps | https://learn.microsoft.com/azure/azure-maps/ |
| Referência da API de Pesquisa | https://learn.microsoft.com/dotnet/api/azure.maps.search |
| Referência da API de Roteamento | https://learn.microsoft.com/dotnet/api/azure.maps.routing |
| Código-fonte no GitHub | https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/maps |
| Preços | https://azure.microsoft.com/pricing/details/azure-maps/ |
---
name: azure-maps-search-dotnet
description: Integrate Azure Maps location-based services into .NET applications for geocoding, routing, rendering, geolocation, and weather data.
license: MIT
---
# Azure Maps (.NET)
Azure Maps SDK for .NET providing location-based services: geocoding, routing, rendering, geolocation, and weather.
## Installation
```bash
# Search (geocoding, reverse geocoding)
dotnet add package Azure.Maps.Search --prerelease
# Routing (directions, route matrix)
dotnet add package Azure.Maps.Routing --prerelease
# Rendering (map tiles, static images)
dotnet add package Azure.Maps.Rendering --prerelease
# Geolocation (IP to location)
dotnet add package Azure.Maps.Geolocation --prerelease
# Weather
dotnet add package Azure.Maps.Weather --prerelease
# Resource Management (account management, SAS tokens)
dotnet add package Azure.ResourceManager.Maps --prerelease
# Required for authentication
dotnet add package Azure.Identity
```
**Current Versions**:
- `Azure.Maps.Search`: v2.0.0-beta.5
- `Azure.Maps.Routing`: v1.0.0-beta.4
- `Azure.Maps.Rendering`: v2.0.0-beta.1
- `Azure.Maps.Geolocation`: v1.0.0-beta.3
- `Azure.ResourceManager.Maps`: v1.1.0-beta.2
## Environment Variables
```bash
AZURE_MAPS_SUBSCRIPTION_KEY=<your-subscription-key> # Only required for AzureKeyCredential auth
AZURE_MAPS_CLIENT_ID=<your-client-id> # Required: Azure Maps client ID
AZURE_TOKEN_CREDENTIALS=prod # Required only if DefaultAzureCredential is used in production
```
## Authentication
### Subscription Key (Shared Key)
```csharp
using Azure;
using Azure.Maps.Search;
var subscriptionKey = Environment.GetEnvironmentVariable("AZURE_MAPS_SUBSCRIPTION_KEY");
var credential = new AzureKeyCredential(subscriptionKey);
var client = new MapsSearchClient(credential);
```
### Microsoft Entra Token Credential
```csharp
using Azure.Identity;
using Azure.Maps.Search;
// Local dev: DefaultAzureCredential. Production: set AZURE_TOKEN_CREDENTIALS=prod or AZURE_TOKEN_CREDENTIALS=<specific_credential>
var credential = new DefaultAzureCredential(
DefaultAzureCredential.DefaultEnvironmentVariableName
);
// Or use a specific credential directly in production:
// See https://learn.microsoft.com/dotnet/api/overview/azure/identity-readme?view=azure-dotnet#credential-classes
// var credential = new ManagedIdentityCredential();
var clientId = Environment.GetEnvironmentVariable("AZURE_MAPS_CLIENT_ID");
var client = new MapsSearchClient(credential, clientId);
```
### Shared Access Signature (SAS)
```csharp
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Maps;
using Azure.ResourceManager.Maps.Models;
using Azure.Maps.Search;
// Authenticate with Azure Resource Manager
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
// Get Maps account resource
ResourceIdentifier mapsAccountResourceId = MapsAccountResource.CreateResourceIdentifier(
subscriptionId, resourceGroupName, accountName);
MapsAccountResource mapsAccount = armClient.GetMapsAccountResource(mapsAccountResourceId);
// Generate SAS token
MapsAccountSasContent sasContent = new MapsAccountSasContent(
MapsSigningKey.PrimaryKey,
principalId,
maxRatePerSecond: 500,
start: DateTime.UtcNow.ToString("O"),
expiry: DateTime.UtcNow.AddDays(1).ToString("O"));
Response<MapsAccountSasToken> sas = mapsAccount.GetSas(sasContent);
// Create client with SAS token
var sasCredential = new AzureSasCredential(sas.Value.AccountSasToken);
var client = new MapsSearchClient(sasCredential);
```
## Client Hierarchy
```
Azure.Maps.Search
└── MapsSearchClient
├── GetGeocoding() → Geocode addresses
├── GetGeocodingBatch() → Batch geocoding
├── GetReverseGeocoding() → Coordinates to address
├── GetReverseGeocodingBatch() → Batch reverse geocoding
└── GetPolygon() → Get boundary polygons
Azure.Maps.Routing
└── MapsRoutingClient
├── GetDirections() → Route directions
├── GetImmediateRouteMatrix() → Route matrix (sync, ≤100)
├── GetRouteMatrix() → Route matrix (async, ≤700)
└── GetRouteRange() → Isochrone/reachable range
Azure.Maps.Rendering
└── MapsRenderingClient
├── GetMapTile() → Map tiles
├── GetMapStaticImage() → Static map images
└── GetCopyrightCaption() → Copyright info
Azure.Maps.Geolocation
└── MapsGeolocationClient
└── GetCountryCode() → IP to country/region
Azure.Maps.Weather
└── MapsWeatherClient
├── GetCurrentWeatherConditions() → Current weather
├── GetDailyForecast() → Daily forecast
├── GetHourlyForecast() → Hourly forecast
└── GetSevereWeatherAlerts() → Weather alerts
```
## Core Workflows
### 1. Geocoding (Address to Coordinates)
```csharp
using Azure;
using Azure.Maps.Search;
var credential = new AzureKeyCredential(subscriptionKey);
var client = new MapsSearchClient(credential);
Response<GeocodingResponse> result = client.GetGeocoding("1 Microsoft Way, Redmond, WA 98052");
foreach (var feature in result.Value.Features)
{
Console.WriteLine($"Coordinates: {string.Join(",", feature.Geometry.Coordinates)}");
Console.WriteLine($"Address: {feature.Properties.Address.FormattedAddress}");
Console.WriteLine($"Confidence: {feature.Properties.Confidence}");
}
```
### 2. Batch Geocoding
```csharp
using Azure.Maps.Search.Models.Queries;
List<GeocodingQuery> queries = new List<GeocodingQuery>
{
new GeocodingQuery() { Query = "400 Broad St, Seattle, WA" },
new GeocodingQuery() { Query = "1 Microsoft Way, Redmond, WA" },
new GeocodingQuery() { AddressLine = "Space Needle", Top = 1 },
};
Response<GeocodingBatchResponse> results = client.GetGeocodingBatch(queries);
foreach (var batchItem in results.Value.BatchItems)
{
foreach (var feature in batchItem.Features)
{
Console.WriteLine($"Coordinates: {string.Join(",", feature.Geometry.Coordinates)}");
}
}
```
### 3. Reverse Geocoding (Coordinates to Address)
```csharp
using Azure.Core.GeoJson;
GeoPosition coordinates = new GeoPosition(-122.138685, 47.6305637);
Response<GeocodingResponse> result = client.GetReverseGeocoding(coordinates);
foreach (var feature in result.Value.Features)
{
Console.WriteLine($"Address: {feature.Properties.Address.FormattedAddress}");
Console.WriteLine($"Locality: {feature.Properties.Address.Locality}");
}
```
### 4. Get Boundary Polygon
```csharp
using Azure.Maps.Search.Models;
GetPolygonOptions options = new GetPolygonOptions()
{
Coordinates = new GeoPosition(-122.204141, 47.61256),
ResultType = BoundaryResultTypeEnum.Locality,
Resolution = ResolutionEnum.Small,
};
Response<Boundary> result = client.GetPolygon(options);
Console.WriteLine($"Boundary copyright: {result.Value.Properties?.Copyright}");
Console.WriteLine($"Polygon count: {result.Value.Geometry.Count}");
```
### 5. Route Directions
```csharp
using Azure;
using Azure.Core.GeoJson;
using Azure.Maps.Routing;
using Azure.Maps.Routing.Models;
var client = new MapsRoutingClient(new AzureKeyCredential(subscriptionKey));
List<GeoPosition> routePoints = new List<GeoPosition>()
{
new GeoPosition(-122.34, 47.61), // Seattle
new GeoPosition(-122.13, 47.64) // Redmond
};
RouteDirectionQuery query = new RouteDirectionQuery(routePoints);
Response<RouteDirections> result = client.GetDirections(query);
foreach (var route in result.Value.Routes)
{
Console.WriteLine($"Distance: {route.Summary.LengthInMeters} meters");
Console.WriteLine($"Duration: {route.Summary.TravelTimeDuration}");
foreach (RouteLeg leg in route.Legs)
{
Console.WriteLine($"Leg points: {leg.Points.Count}");
}
}
```
### 6. Route Directions with Options
```csharp
RouteDirectionOptions options = new RouteDirectionOptions()
{
RouteType = RouteType.Fastest,
UseTrafficData = true,
TravelMode = TravelMode.Bicycle,
Language = RoutingLanguage.EnglishUsa,
InstructionsType = RouteInstructionsType.Text,
};
RouteDirectionQuery query = new RouteDirectionQuery(routePoints)
{
RouteDirectionOptions = options
};
Response<RouteDirections> result = client.GetDirections(query);
```
### 7. Route Matrix
```csharp
RouteMatrixQuery routeMatrixQuery = new RouteMatrixQuery
{
Origins = new List<GeoPosition>()
{
new GeoPosition(-122.34, 47.61),
new GeoPosition(-122.13, 47.64)
},
Destinations = new List<GeoPosition>()
{
new GeoPosition(-122.20, 47.62),
new GeoPosition(-122.40, 47.65)
},
};
// Synchronous (up to 100 route combinations)
Response<RouteMatrixResult> result = client.GetImmediateRouteMatrix(routeMatrixQuery);
foreach (var cell in result.Value.Matrix.SelectMany(row => row))
{
Console.WriteLine($"Distance: {cell.Response?.RouteSummary?.LengthInMeters}");
Console.WriteLine($"Duration: {cell.Response?.RouteSummary?.TravelTimeDuration}");
}
// Asynchronous (up to 700 route combinations)
RouteMatrixOptions routeMatrixOptions = new RouteMatrixOptions(routeMatrixQuery)
{
TravelTimeType = TravelTimeType.All,
};
GetRouteMatrixOperation asyncResult = client.GetRouteMatrix(WaitUntil.Completed, routeMatrixOptions);
```
### 8. Route Range (Isochrone)
```csharp
RouteRangeOptions options = new RouteRangeOptions(-122.34, 47.61)
{
TimeBudget = new TimeSpan(0, 20, 0) // 20 minutes
};
Response<RouteRangeResult> result = client.GetRouteRange(options);
// result.Value.ReachableRange contains the polygon
Console.WriteLine($"Boundary points: {result.Value.ReachableRange.Boundary.Count}");
```
### 9. Get Map Tiles
```csharp
using Azure;
using Azure.Maps.Rendering;
var client = new MapsRenderingClient(new AzureKeyCredential(subscriptionKey));
int zoom = 10;
int tileSize = 256;
// Convert coordinates to tile index
MapTileIndex tileIndex = MapsRenderingClient.PositionToTileXY(
new GeoPosition(13.3854, 52.517), zoom, tileSize);
// Fetch map tile
GetMapTileOptions options = new GetMapTileOptions(
MapTileSetId.MicrosoftImagery,
new MapTileIndex(tileIndex.X, tileIndex.Y, zoom)
);
Response<Stream> mapTile = client.GetMapTile(options);
// Save to file
using (FileStream fileStream = File.Create("./MapTile.png"))
{
mapTile.Value.CopyTo(fileStream);
}
```
### 10. IP Geolocation
```csharp
using System.Net;
using Azure;
using Azure.Maps.Geolocation;
var client = new MapsGeolocationClient(new AzureKeyCredential(subscriptionKey));
IPAddress ipAddress = IPAddress.Parse("2001:4898:80e8:b::189");
Response<CountryRegionResult> result = client.GetCountryCode(ipAddress);
Console.WriteLine($"Country ISO Code: {result.Value.IsoCode}");
```
### 11. Current Weather
```csharp
using Azure;
using Azure.Core.GeoJson;
using Azure.Maps.Weather;
var client = new MapsWeatherClient(new AzureKeyCredential(subscriptionKey));
var position = new GeoPosition(-122.13071, 47.64011);
var options = new GetCurrentWeatherConditionsOptions(position);
Response<CurrentConditionsResult> result = client.GetCurrentWeatherConditions(options);
foreach (var condition in result.Value.Results)
{
Console.WriteLine($"Temperature: {condition.Temperature.Value} {condition.Temperature.Unit}");
Console.WriteLine($"Weather: {condition.Phrase}");
Console.WriteLine($"Humidity: {condition.RelativeHumidity}%");
}
```
## Key Types Reference
### Search Package
| Type | Purpose |
|------|---------|
| `MapsSearchClient` | Main client for search operations |
| `GeocodingResponse` | Geocoding result |
| `GeocodingBatchResponse` | Batch geocoding result |
| `GeocodingQuery` | Query for batch geocoding |
| `ReverseGeocodingQuery` | Query for batch reverse geocoding |
| `GetPolygonOptions` | Options for polygon retrieval |
| `Boundary` | Boundary polygon result |
| `BoundaryResultTypeEnum` | Boundary type (Locality, AdminDistrict, etc.) |
| `ResolutionEnum` | Polygon resolution (Small, Medium, Large) |
### Routing Package
| Type | Purpose |
|------|---------|
| `MapsRoutingClient` | Main client for routing operations |
| `RouteDirectionQuery` | Query for route directions |
| `RouteDirectionOptions` | Route calculation options |
| `RouteDirections` | Route directions result |
| `RouteLeg` | Segment of a route |
| `RouteMatrixQuery` | Query for route matrix |
| `RouteMatrixResult` | Route matrix result |
| `RouteRangeOptions` | Options for isochrone |
| `RouteRangeResult` | Isochrone result |
| `RouteType` | Route type (Fastest, Shortest, Eco, Thrilling) |
| `TravelMode` | Travel mode (Car, Truck, Bicycle, Pedestrian) |
### Rendering Package
| Type | Purpose |
|------|---------|
| `MapsRenderingClient` | Main client for rendering |
| `GetMapTileOptions` | Map tile options |
| `MapTileIndex` | Tile coordinates (X, Y, Zoom) |
| `MapTileSetId` | Tile set identifier |
### Common Types
| Type | Purpose |
|------|---------|
| `GeoPosition` | Geographic position (longitude, latitude) |
| `GeoBoundingBox` | Bounding box for geographic area |
## Best Practices
1. **Use Entra ID for production** — Prefer over subscription keys
2. **Batch operations** — Use batch geocoding for multiple addresses
3. **Cache results** — Geocoding results don't change frequently
4. **Use appropriate tile sizes** — 256 or 512 pixels based on display
5. **Handle rate limits** — Implement exponential backoff
6. **Use async route matrix** — For large matrix calculations (>100)
7. **Consider traffic data** — Set `UseTrafficData = true` for accurate ETAs
## Error Handling
```csharp
try
{
Response<GeocodingResponse> result = client.GetGeocoding(address);
}
catch (RequestFailedException ex)
{
Console.WriteLine($"Status: {ex.Status}");
Console.WriteLine($"Error: {ex.Message}");
switch (ex.Status)
{
case 400:
// Invalid request parameters
break;
case 401:
// Authentication failed
break;
case 429:
// Rate limited - implement backoff
break;
}
}
```
## Related SDKs
| SDK | Purpose | Install |
|-----|---------|---------|
| `Azure.Maps.Search` | Geocoding, search | `dotnet add package Azure.Maps.Search --prerelease` |
| `Azure.Maps.Routing` | Directions, matrix | `dotnet add package Azure.Maps.Routing --prerelease` |
| `Azure.Maps.Rendering` | Map tiles, images | `dotnet add package Azure.Maps.Rendering --prerelease` |
| `Azure.Maps.Geolocation` | IP geolocation | `dotnet add package Azure.Maps.Geolocation --prerelease` |
| `Azure.Maps.Weather` | Weather data | `dotnet add package Azure.Maps.Weather --prerelease` |
| `Azure.ResourceManager.Maps` | Account management | `dotnet add package Azure.ResourceManager.Maps --prerelease` |
## Reference Links
| Resource | URL |
|----------|-----|
| Azure Maps Documentation | https://learn.microsoft.com/azure/azure-maps/ |
| Search API Reference | https://learn.microsoft.com/dotnet/api/azure.maps.search |
| Routing API Reference | https://learn.microsoft.com/dotnet/api/azure.maps.routing |
| GitHub Source | https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/maps |
| Pricing | https://azure.microsoft.com/pricing/details/azure-maps/ |
Todos os arquivos
0 arquivosInstalar azure-maps-search-dotnet
Baixe e descompacte os arquivos de habilidades no diretório .claude/skills/.
Baixar ZIPClone o repositório e copie os arquivos da habilidade para o seu projeto.
git clone https://github.com/microsoft/skills/tree/main/.github/plugins/azure-sdk-dotnet/skills/azure-maps-search-dotnet # Copy SKILL.md to your .claude/skills/ directory
Copiar





Lar
