Implement Geocoding with the Azure Maps Services
Environment
| Product Version | 2025.1.211 |
| Product | RadMap for WPF |
Description
Implementing geocoding (searching) functionality using the Azure Maps services.
Solution
To achieve geocoding functionality using the Azure Maps services, you can create a new helper class. It will create a URL request for the Microsoft's geocoding API and use the response to center the RadMap control. In order to receive a valid response from the geocoding API, you will need to pass a valid location and subscription key to the request URL string.
Implementing a helper class for retrieving the searched location
internal class AzureGeocodingHelper
{
private static HttpClient httpClient = new HttpClient();
internal async static Task<Location> GetGeoCode(string location)
{
//Modify this URL to match your Azure Mapss subscription key and location
var requestUrl = $"https://atlas.microsoft.com/geocode?api-version=2025-01-01&query={location}&subscription-key={MyAzureMapsSubscriptionKey}";
var response = await httpClient.GetAsync(requestUrl);
response.EnsureSuccessStatusCode();
var jsonContent = await response.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true
};
var featureCollection = JsonSerializer.Deserialize<FeatureCollection>(jsonContent, options);
var firstCoordinates = featureCollection.Features.First().Geometry.Coordinates;
return new Location(firstCoordinates[1], firstCoordinates[0]);
}
}
public class FeatureCollection
{
public List<Feature> Features { get; set; }
}
public class Feature
{
public Geometry Geometry { get; set; }
}
public class Geometry
{
public List<double> Coordinates { get; set; }
}Additionally, you can follow the next example, which showcases how to utilize the created AzureGeocodingHelper class and its GetGeoCode method.
Defining the RadMap and the UI elements for the geocoding logic
<Grid>
<Grid.Resources>
<ResourceDictionary Source="/Telerik.Windows.Controls;component/Themes/FontResources.xaml"/>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Center">
<TextBox x:Name="textBoxSearchAddress" VerticalAlignment="Center" Width="200"/>
<telerik:RadButton Click="RadButton_Click" VerticalAlignment="Center" Margin="4 0 0 0">
<telerik:RadButton.Content>
<telerik:RadGlyph Glyph="{StaticResource GlyphZoom}"/>
</telerik:RadButton.Content>
</telerik:RadButton>
</StackPanel>
<telerik:RadMap x:Name="map" Grid.Row="1">
<telerik:RadMap.Provider>
<telerik:AzureMapProvider SubscriptionKey="Your Subscription Key"/>
</telerik:RadMap.Provider>
<telerik:VisualizationLayer x:Name="visualizationLayer"/>
</telerik:RadMap>
</Grid>
Utilizing the AzureGeocodingHelper class's GetGeoCode method
private async void RadButton_Click(object sender, RoutedEventArgs e)
{
try
{
Location location = await AzureGeocodingHelper.GetGeoCode(this.textBoxSearchAddress.Text);
this.visualizationLayer.Items.Clear();
this.visualizationLayer.Items.Add(location);
this.map.Center = location;
}
catch (Exception exception)
{
MessageBox.Show("Please, enter a another location");
}
}RadMap with AzureMapProvider and geocoding functionality

For a more in-depth example of an Azure Maps services geocoding, check the Geocoding demo from our Demos application.