How to make my Azure Map determine accurate location information for my mobile users

Jim Derry 146 Reputation points
2025-04-11T10:21:00.2966667+00:00

I use the Azure Map service in my SaaS application. Users do not get an accurate location returned when we try to determine their position

Azure Maps
Azure Maps
An Azure service that provides geospatial APIs to add maps, spatial analytics, and mobility solutions to apps.
{count} votes

Answer accepted by question author
  1. rbrundritt 21,581 Reputation points Moderator
    2025-04-11T15:22:30.5333333+00:00

    You are using the old reverse geocoding service, use the latest: https://learn.microsoft.com/en-us/rest/api/maps/search/get-reverse-geocoding?view=rest-maps-2025-01-01&tabs=HTTP

    The latest should be nearly identical data as Bing Maps.

    Note the API interface of the new version is very different. Some key things to modify in your code:

    • "query" parameter becomes "coordinates" and the format it "longitude,latitude" (reverse of the old service).
    • The response format is a GeoJSON Feature Collection which is very different format from previous version.

    Here is a modified version of your code:

    import requests
    
    def get_address(latitude, longitude, subscription_key):
        # Define the endpoint and parameters for the Azure Maps Reverse Geocoding API
        endpoint = "https://atlas.microsoft.com/reverseGeocode"
        params = {
            'api-version': '2025-01-01',
            'subscription-key': subscription_key,
            'language': 'en-US',
            'coordinates': f'{longitude},{latitude}'
        }
        
        # Make a request to the API
        response = requests.get(endpoint, params=params)
        
        # Check if the request was successful
        if response.status_code == 200:
            data = response.json()
            #print(data)
            if data['features']:
                # Extract the address from the response
                address_info = data['features'][0]['properties']['address']
                freeform_address = address_info.get('formattedAddress', 'Address not found')
                #building_name = address_info.get('buildingName', 'Building name not found')
                #print(f"Address: {freeform_address}")
                #print(f"Building Name: {building_name}")
            else:
                print('Address not found')
        else:
            print('Error: Unable to fetch address')
    
        return freeform_address
    
    # Lattitude and longitude can be found from navigator.geolocation too
    latitude = 17.967673
    longitude = 79.487091
    subscription_key = '<azuremapskey>'  # Replace with your actual subscription key
    freeform_address = get_address(latitude, longitude, subscription_key)
    print(f"The address for latitude {latitude} and longitude {longitude} is: {freeform_address}")
    
    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.