Skip to content

Latest commit

 

History

History
56 lines (32 loc) · 3.28 KB

geocoding.md

File metadata and controls

56 lines (32 loc) · 3.28 KB
title description ms.date no-loc
Geocoding
Learn how to use the .NET MAUI IGeocoding interface in the Microsoft.Maui.Devices.Sensors namespace. This interface provides APIs to both geocode a placemark to a positional coordinate, and reverse geocode coordinates to a placemark.
02/02/2023
Microsoft.Maui
Microsoft.Maui.Devices
Microsoft.Maui.Devices.Sensors

Geocoding

Browse sample. Browse the sample

This article describes how you can use the .NET Multi-platform App UI (.NET MAUI) xref:Microsoft.Maui.Devices.Sensors.IGeocoding interface. This interfaces provides APIs to geocode a placemark to a positional coordinates and reverse geocode coordinates to a placemark.

The default implementation of the IGeocoding interface is available through the xref:Microsoft.Maui.Devices.Sensors.Geocoding.Default?displayProperty=nameWithType property. Both the IGeocoding interface and Geocoding class are contained in the Microsoft.Maui.Devices.Sensors namespace.

Get started

To access the Geocoding functionality the following platform-specific setup is required.

No setup is required.

No setup is required.

A Bing Maps API key is required to use geocoding functionality. Sign up for a free Bing Maps account. Under My account > My keys, create a new key and fill out information based on your application type, which should be Windows Application.

To enable geocoding functionality in your app, invoke the xref:Microsoft.Maui.Hosting.EssentialsExtensions.ConfigureEssentials%2A method on the xref:Microsoft.Maui.Hosting.MauiAppBuilder object in the MauiProgram.cs file. Then, on the xref:Microsoft.Maui.Hosting.IEssentialsBuilder object, call the xref:Microsoft.Maui.Hosting.IEssentialsBuilder.UseMapServiceToken%2A method and pass your Bing Maps API key as the argument:

:::code language="csharp" source="../snippets/shared_1/MauiProgram.cs" id="bootstrap_maptoken" highlight="12-15":::


Use geocoding

The following example demonstrates how to get the location coordinates for an address:

:::code language="csharp" source="../snippets/shared_1/SensorsPage.xaml.cs" id="geocoding_location":::

The altitude isn't always available. If it isn't available, the xref:Microsoft.Maui.Devices.Sensors.Location.Altitude property might be null, or the value might be 0. If the altitude is available, the value is in meters above sea level.

Reverse geocoding

Reverse geocoding is the process of getting placemarks for an existing set of coordinates. The following example demonstrates getting placemarks:

:::code language="csharp" source="../snippets/shared_1/SensorsPage.xaml.cs" id="geocoding_reverse":::

Get the distance between two locations

The xref:Microsoft.Maui.Devices.Sensors.Location and xref:Microsoft.Maui.Devices.Sensors.LocationExtensions classes define methods to calculate the distance between two locations. For an example of getting the distance between two locations, see Distance between two locations.