Search code examples
javascripthtmlazure-maps

Return county from a given address


I am using azure maps to return an address when a user clicks on a map. Currently I am getting the full address of where the user is clicking but instead I want to only provide the county.

Here is the function I am using to get the address:

 function mapClicked(e) {
            //Reverse address search query then open a popup once a response is received
            searchURL.searchAddressReverse(atlas.service.Aborter.timeout(3000), e.position, {
                view: 'Auto'
            }).then(results => {
                //Get the results in GeoJSON format
                var data = results.geojson.getFeatures();

                var content = '<div style="padding:10px">';

                if (data.features.length > 0 && data.features[0].properties 
                     && data.features[0].properties.address 
                     && data.features[0].properties.address.freeformAddress) {

                    content += data.features[0].properties.address.freeformAddress;
                    theAddress += content;

                } else {
                    content += 'No address for that location!';
                }

                content += '</div>';

                popup.setOptions({
                    position: e.position,
                    content: content
                });

                //Open popup
                popup.open(map);
            });

Any suggestions on how to isolate the county is appreciated, thank you!


Solution

  • Just had to change freeformAddress to countySecondarySubdivision like so:

     if (data.features.length > 0 && data.features[0].properties 
                    && data.features[0].properties.address 
                    && data.features[0].properties.address.countrySecondarySubdivision) {
                    content += data.features[0].properties.address.countrySecondarySubdivision;
                    theAddress += content;