|
| 1 | +# Azure Maps GeoLocation client library for .NET |
| 2 | + |
| 3 | +Azure Maps GeoLocation is a library that can find geolocation to a location or points of interest. |
| 4 | + |
| 5 | +[Source code](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/maps/Azure.Maps.GeoLocation/src) | [API reference documentation](https://docs.microsoft.com/rest/api/maps/) | [REST API reference documentation](https://docs.microsoft.com/rest/api/maps/geolocation) | [Product documentation](https://docs.microsoft.com/azure/azure-maps/) |
| 6 | + |
| 7 | +## Getting started |
| 8 | + |
| 9 | +### Install the package |
| 10 | + |
| 11 | +Install the client library for .NET with [NuGet](https://www.nuget.org/): |
| 12 | + |
| 13 | +```dotnetcli |
| 14 | +dotnet add package Azure.Maps.GeoLocation --prerelease |
| 15 | +``` |
| 16 | + |
| 17 | +### Prerequisites |
| 18 | + |
| 19 | +> You must have an [Azure subscription](https://azure.microsoft.com/free/dotnet/) and [Azure Maps account](https://docs.microsoft.com/azure/azure-maps/quick-demo-map-app#create-an-azure-maps-account). |
| 20 | +
|
| 21 | +To create a new Azure Maps account, you can use the Azure Portal, Azure PowerShell, or the Azure CLI. Here's an example using the Azure CLI: |
| 22 | + |
| 23 | +```powershell |
| 24 | +az maps account create --kind "Gen2" --disable-local-auth true --account-name "myMapAccountName" --resource-group "<resource group>" --sku "G2" --accept-tos |
| 25 | +``` |
| 26 | + |
| 27 | +### Authenticate the client |
| 28 | + |
| 29 | +There are 2 ways to authenticate the client: Shared key authentication and Azure AD. |
| 30 | + |
| 31 | +#### Shared Key authentication |
| 32 | + |
| 33 | +* Go to Azure Maps account > Authentication tab |
| 34 | +* Copy `Primary Key` or `Secondary Key` under **Shared Key Authentication** section |
| 35 | + |
| 36 | +```C# Snippet:InstantiateGeoLocationClientViaSubscriptionKey |
| 37 | +// Create a MapsGeoLocationClient that will authenticate through Subscription Key (Shared key) |
| 38 | +AzureKeyCredential credential = new AzureKeyCredential("<My Subscription Key>"); |
| 39 | +MapsGeoLocationClient client = new MapsGeoLocationClient(credential); |
| 40 | +``` |
| 41 | + |
| 42 | +#### Azure AD authentication |
| 43 | + |
| 44 | +In order to interact with the Azure Maps service, you'll need to create an instance of the `MapsGeoLocationClient` class. The [Azure Identity library](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/identity/Azure.Identity/README.md) makes it easy to add Azure Active Directory support for authenticating Azure SDK clients with their corresponding Azure services. |
| 45 | + |
| 46 | +To use AAD authentication, set the environment variables as described in the [Azure Identity README](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/identity/Azure.Identity/README.md) and create a `DefaultAzureCredential` instance to use with the `MapsRouteClient`. |
| 47 | + |
| 48 | +We also need **Azure Maps Client ID** which can get from Azure Maps page > Authentication tab > "Client ID" in Azure Active Directory Authentication section. |
| 49 | + |
| 50 | +```C# Snippet:InstantiateGeoLocationClientViaAAD |
| 51 | +// Create a MapsGeoLocationClient that will authenticate through Active Directory |
| 52 | +TokenCredential credential = new DefaultAzureCredential(); |
| 53 | +string clientId = "<Your Map ClientId>"; |
| 54 | +MapsGeoLocationClient client = new MapsGeoLocationClient(credential, clientId); |
| 55 | +``` |
| 56 | + |
| 57 | +## Key concepts |
| 58 | + |
| 59 | +`MapsGeoLocationClient` is designed for: |
| 60 | + |
| 61 | +* Communicate with Azure Maps GeoLocation SDK endpoint to get location from given IP address |
| 62 | + |
| 63 | +Learn more about examples in [samples](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/maps/Azure.Maps.GeoLocation/samples) |
| 64 | + |
| 65 | +### Thread safety |
| 66 | + |
| 67 | +We guarantee that all client instance methods are thread-safe and independent of each other ([guideline](https://azure.github.io/azure-sdk/dotnet_introduction.html#dotnet-service-methods-thread-safety)). This ensures that the recommendation of reusing client instances is always safe, even across threads. |
| 68 | + |
| 69 | +### Additional concepts |
| 70 | +<!-- CLIENT COMMON BAR --> |
| 71 | +[Client options](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/README.md#configuring-service-clients-using-clientoptions) | |
| 72 | +[Accessing the response](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/README.md#accessing-http-response-details-using-responset) | |
| 73 | +[Long-running operations](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/README.md#consuming-long-running-operations-using-operationt) | |
| 74 | +[Handling failures](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/README.md#reporting-errors-requestfailedexception) | |
| 75 | +[Diagnostics](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Diagnostics.md) | |
| 76 | +[Mocking](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/README.md#mocking) | |
| 77 | +[Client lifetime](https://devblogs.microsoft.com/azure-sdk/lifetime-management-and-thread-safety-guarantees-of-azure-sdk-net-clients/) |
| 78 | +<!-- CLIENT COMMON BAR --> |
| 79 | + |
| 80 | +## Examples |
| 81 | + |
| 82 | +You can familiarize yourself with different APIs using our [Samples](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/maps/Azure.Maps.GeoLocation/samples). |
| 83 | + |
| 84 | +Before calling geolocation APIs, instantiate a `MapsGeoLocationClient` first. Below example uses AAD to create the client instance: |
| 85 | + |
| 86 | +```C# Snippet:InstantiateGeoLocationClientViaAAD |
| 87 | +// Create a MapsGeoLocationClient that will authenticate through Active Directory |
| 88 | +TokenCredential credential = new DefaultAzureCredential(); |
| 89 | +string clientId = "<Your Map ClientId>"; |
| 90 | +MapsGeoLocationClient client = new MapsGeoLocationClient(credential, clientId); |
| 91 | +``` |
| 92 | + |
| 93 | +### Get Location |
| 94 | + |
| 95 | +This service will return the ISO country code for the provided IP address. Developers can use this information to block or alter certain content based on geographical locations where the application is being viewed from. |
| 96 | + |
| 97 | +```C# Snippet:GetLocation |
| 98 | +//Get location by given IP address |
| 99 | +string ipAddress = "2001:4898:80e8:b::189"; |
| 100 | +Response<IpAddressToLocationResult> result = client.GetLocation(ipAddress); |
| 101 | + |
| 102 | +//Get location result country code |
| 103 | +Console.WriteLine($"Country code results by given IP Address: {result.Value.IsoCode}"); |
| 104 | +``` |
| 105 | + |
| 106 | +For more detailed examples, please see [geolocation samples](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/maps/Azure.Maps.GeoLocation/samples/GetLocationSamples.md) page. |
| 107 | + |
| 108 | +## Troubleshooting |
| 109 | + |
| 110 | +### General |
| 111 | + |
| 112 | +When you interact with the Azure Maps services, errors returned by the Language service correspond to the same HTTP status codes returned for [REST API requests](https://docs.microsoft.com/rest/api/maps/geolocation). |
| 113 | + |
| 114 | +For example, if you pass wrong IP address, an error is returned, indicating "Bad Request" (HTTP Status code: 400). |
| 115 | + |
| 116 | +```C# Snippet:CatchGeoLocationException |
| 117 | +try |
| 118 | +{ |
| 119 | + // An invalid IP address |
| 120 | + string inValidIpAddress = "xxx"; |
| 121 | + |
| 122 | + Response<IpAddressToLocationResult> result = client.GetLocation(inValidIpAddress); |
| 123 | + // Do something with result ... |
| 124 | +} |
| 125 | +catch (RequestFailedException e) |
| 126 | +{ |
| 127 | + Console.WriteLine(e.ToString()); |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +## Next steps |
| 132 | + |
| 133 | +* For more context and additional scenarios, please see: [More detailed samples](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/maps/Azure.Maps.GeoLocation/samples) |
| 134 | + |
| 135 | +## Contributing |
| 136 | + |
| 137 | +See the [CONTRIBUTING.md](https://github.com/Azure/azure-sdk-for-net/blob/main/CONTRIBUTING.md) for details on building, testing, and contributing to this library. |
| 138 | + |
| 139 | +This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit <cla.microsoft.com>. |
| 140 | + |
| 141 | +When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA. |
| 142 | + |
| 143 | +This project has adopted the [Microsoft Open Source Code of Conduct ](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ ](https://opensource.microsoft.com/codeofconduct/faq/) or contact <[email protected]> with any additional questions or comments. |
| 144 | + |
| 145 | + |
0 commit comments