如何通过Azure Maps逆地址查询仅提取县/郡信息?
修改Azure Maps逆地理编码仅返回县/郡信息的方案
Azure Maps的逆地理编码API返回的地址对象中,县/郡信息通常对应adminDistrict2字段(这是覆盖大多数地区的通用字段)。你可以修改原代码中的地址提取逻辑,直接读取该字段来实现需求:
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.adminDistrict2) { // 提取县/郡信息 content += data.features[0].properties.address.adminDistrict2; theAddress += content; } else { content += '未找到该位置的县/郡信息!'; } content += '</div>'; popup.setOptions({ position: e.position, content: content }); //Open popup popup.open(map); }); }
关键修改说明:
- 将原代码中读取
freeformAddress的逻辑,替换为读取address.adminDistrict2字段 - 保留了完整的存在性校验,避免因字段缺失导致的报错
- 调整了无数据时的提示文本,更贴合当前需求
如果部分地区的县/郡信息存储在其他字段(如部分国家的countrySecondarySubdivision),可以在判断逻辑中增加多字段兼容,示例如下:
// 多字段兼容的提取逻辑 const addressProps = data.features[0].properties.address; const county = addressProps.adminDistrict2 || addressProps.countrySecondarySubdivision; if (county) { content += county; theAddress += content; } else { content += '未找到该位置的县/郡信息!'; }
内容的提问来源于stack exchange,提问作者Sean
相关产品推荐
相关产品推荐

