mirror of
https://gitee.com/baidu/amis.git
synced 2024-11-29 18:39:05 +08:00
Location地理位置支持返回gcj02坐标 (#1446)
* Location地理位置支持返回gcj02坐标 * Location地理位置支持返回gcj02坐标
This commit is contained in:
parent
4944ee3542
commit
73407b4151
@ -38,3 +38,4 @@ order: 30
|
||||
| ak | `string` | 无 | 百度地图的 ak |
|
||||
| clearable | `boolean` | false | 输入框是否可清空 |
|
||||
| placeholder | `string` | '请选择位置' | 默认提示 |
|
||||
| coordinatesType | `string` | 'bd09' | 默为百度坐标,可设置为'gcj02' |
|
||||
|
@ -6,8 +6,29 @@ import {Icon} from './icons';
|
||||
|
||||
declare const BMap: any;
|
||||
|
||||
/**
|
||||
* 坐标常量说明:
|
||||
* COORDINATES_WGS84 = 1, WGS84坐标
|
||||
* COORDINATES_WGS84_MC = 2, WGS84的平面墨卡托坐标
|
||||
* COORDINATES_GCJ02 = 3,GCJ02坐标
|
||||
* COORDINATES_GCJ02_MC = 4, GCJ02的平面墨卡托坐标
|
||||
* COORDINATES_BD09 = 5, 百度bd09经纬度坐标
|
||||
* COORDINATES_BD09_MC = 6,百度bd09墨卡托坐标
|
||||
* COORDINATES_MAPBAR = 7,mapbar地图坐标
|
||||
* COORDINATES_51 = 8,51地图坐标
|
||||
*/
|
||||
const COORDINATES_WGS84 = 1;
|
||||
const COORDINATES_WGS84_MC = 2;
|
||||
const COORDINATES_GCJ02 = 3;
|
||||
const COORDINATES_GCJ02_MC = 4;
|
||||
const COORDINATES_BD09 = 5;
|
||||
const COORDINATES_BD09_MC = 6;
|
||||
const COORDINATES_MAPBAR = 7;
|
||||
const COORDINATES_51 = 8;
|
||||
|
||||
interface MapPickerProps {
|
||||
ak: string;
|
||||
coordinatesType: string;
|
||||
classnames: ClassNamesFn;
|
||||
classPrefix: string;
|
||||
value?: {
|
||||
@ -66,6 +87,7 @@ export class BaiduMapPicker extends React.Component<
|
||||
leading: false
|
||||
}
|
||||
);
|
||||
convertor: any;
|
||||
|
||||
componentDidMount() {
|
||||
if ((window as any).BMap) {
|
||||
@ -91,12 +113,18 @@ export class BaiduMapPicker extends React.Component<
|
||||
enableMapClick: false
|
||||
});
|
||||
this.map = map;
|
||||
this.convertor = new BMap.Convertor();
|
||||
|
||||
const value = this.props.value;
|
||||
let point = value
|
||||
? new BMap.Point(value.lng, value.lat)
|
||||
: new BMap.Point(116.404, 39.915);
|
||||
map.centerAndZoom(point, 15);
|
||||
if (this.props.coordinatesType == 'gcj02') {
|
||||
point = await this.covertPoint(point, COORDINATES_GCJ02, COORDINATES_BD09);
|
||||
map.centerAndZoom(point, 15);
|
||||
} else {
|
||||
map.centerAndZoom(point, 15);
|
||||
}
|
||||
|
||||
map.addControl(
|
||||
// @ts-ignore
|
||||
@ -201,12 +229,7 @@ export class BaiduMapPicker extends React.Component<
|
||||
return;
|
||||
}
|
||||
|
||||
this.props?.onChange({
|
||||
address: locs[0].address,
|
||||
lat: locs[0].lat,
|
||||
lng: locs[0].lng,
|
||||
city: locs[0].city
|
||||
});
|
||||
this.triggerOnChange(locs[0]);
|
||||
}
|
||||
);
|
||||
});
|
||||
@ -239,16 +262,45 @@ export class BaiduMapPicker extends React.Component<
|
||||
this.map.addOverlay(mk);
|
||||
this.map.panTo(point);
|
||||
|
||||
this.props?.onChange({
|
||||
address: loc.address.trim() || loc.title,
|
||||
lat: loc.lat,
|
||||
lng: loc.lng,
|
||||
city: loc.city
|
||||
});
|
||||
this.triggerOnChange(loc);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
covertPoint(point: any, from: number, to: number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.convertor.translate([point], from, to, (res:any)=> {
|
||||
if (res.status === 0 && res.points.length) {
|
||||
resolve(new BMap.Point(res.points[0].lng, res.points[0].lat));
|
||||
} else {
|
||||
reject();
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
triggerOnChange(loc: LocationItem) {
|
||||
const point = new BMap.Point(loc.lng, loc.lat);
|
||||
if (this.props.coordinatesType == 'gcj02') {
|
||||
this.covertPoint(point, COORDINATES_BD09, COORDINATES_GCJ02).then((convertedPoint:any)=>{
|
||||
this.props?.onChange({
|
||||
address: loc.address.trim() || loc.title,
|
||||
lat: convertedPoint.lat,
|
||||
lng: convertedPoint.lng,
|
||||
city: loc.city
|
||||
});
|
||||
})
|
||||
} else {
|
||||
this.props?.onChange({
|
||||
address: loc.address.trim() || loc.title,
|
||||
lat: loc.lat,
|
||||
lng: loc.lng,
|
||||
city: loc.city
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@autobind
|
||||
handleSugSelect(e: React.MouseEvent<HTMLDivElement>) {
|
||||
const value = e.currentTarget.innerText;
|
||||
|
@ -10,6 +10,7 @@ import {LocaleProps, localeable} from '../locale';
|
||||
|
||||
export interface LocationProps extends ThemeProps, LocaleProps {
|
||||
vendor: 'baidu' | 'gaode' | 'tenxun';
|
||||
coordinatesType: 'bd09' | 'gcj02';
|
||||
placeholder: string;
|
||||
clearable: boolean;
|
||||
ak: string;
|
||||
@ -134,6 +135,7 @@ export class LocationPicker extends React.Component<
|
||||
clearable,
|
||||
popOverContainer,
|
||||
vendor,
|
||||
coordinatesType,
|
||||
ak
|
||||
} = this.props;
|
||||
const __ = this.props.translate;
|
||||
@ -192,6 +194,7 @@ export class LocationPicker extends React.Component<
|
||||
<BaiduMapPicker
|
||||
ak={ak}
|
||||
value={value}
|
||||
coordinatesType={coordinatesType}
|
||||
onChange={this.handleChange}
|
||||
/>
|
||||
) : (
|
||||
|
@ -29,11 +29,13 @@ export interface LocationControlProps
|
||||
onChange: (value: any) => void;
|
||||
vendor: 'baidu' | 'gaode' | 'tenxun';
|
||||
ak: string;
|
||||
coordinatesType: 'bd09' | 'gcj02';
|
||||
}
|
||||
|
||||
export class LocationControl extends React.Component<LocationControlProps> {
|
||||
static defaultProps = {
|
||||
vendor: 'baidu'
|
||||
vendor: 'baidu',
|
||||
coordinatesType: 'bd09'
|
||||
};
|
||||
|
||||
render() {
|
||||
|
Loading…
Reference in New Issue
Block a user