Geocoder
类用于获取用户的地址解析。
构造函数
| 构造函数 | 描述 |
|---|---|
Geocoder() | 创建一个地址解析器的实例 |
LocationOptions
此类表示Geocoder的地址解析请求的可选参数。它不可实例化。
| 属性 | 类型 | 描述 |
|---|---|---|
| poiRadius | Number | 附近POI所处于的最大半径,默认为100米 |
| numPois | Number | 返回的POI点个数,默认为10个。取值范围 |
GeocoderResult
此类表示Geocoder的地址解析结果。它在地址解析的回调函数的参数中返回,不可实例化。
属性
| 属性 | 类型 | 描述 |
|---|---|---|
| point | Point | 坐标点 |
| address | String | 地址描述 |
| addressComponents | AddressComponent | 结构化的地址描述 |
| surroundingPois | Array<LocalResultPoi> | 附近的POI点 |
| business | String | 商圈字段,代表此点所属的商圈 |
方法
| 方法 | 返回值 | 描述 |
|---|---|---|
| getPoint(address: String, callback: Function, city: String) | none | 对指定的地址进行解析。如果地址定位成功,则以地址所在的坐标点Point为参数调用回调函数。否则,回调函数的参数为null。city为地址所在的城市名,例如"北京市" |
| getLocation(point: Point, callback: Function, options: LocationOptions) | none | 对指定的坐标点进行反地址解析。如果解析成功,则回调函数的参数为GeocoderResult对象,否则回调函数的参数为null |
示例
ts
// 创建地址解析器实例
const geocoder = new BMapGL.Geocoder();
// 地址解析
geocoder.getPoint('北京市海淀区上地十街10号', (point: BMapGL.Point) => {
if (point) {
console.log('地址解析成功:', point.lng, point.lat);
} else {
console.log('地址解析失败');
}
}, '北京市');
// 逆地址解析(带选项)
const point = new BMapGL.Point(116.404, 39.915);
const options = {
poiRadius: 200, // 设置POI搜索半径为200米
numPois: 5 // 返回5个POI点
};
geocoder.getLocation(point, (result: BMapGL.GeocoderResult) => {
if (result) {
console.log('逆地址解析成功:', result.address);
console.log('所在商圈:', result.business);
console.log('结构化地址:', result.addressComponents);
console.log('附近POI:', result.surroundingPois);
} else {
console.log('逆地址解析失败');
}
}, options);