Location地理位置支持返回gcj02坐标 (#1446)

* Location地理位置支持返回gcj02坐标

* Location地理位置支持返回gcj02坐标
This commit is contained in:
hiro0729 2021-01-26 15:51:34 +08:00 committed by GitHub
parent 4944ee3542
commit 73407b4151
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 14 deletions

View File

@ -38,3 +38,4 @@ order: 30
| ak | `string` | 无 | 百度地图的 ak |
| clearable | `boolean` | false | 输入框是否可清空 |
| placeholder | `string` | '请选择位置' | 默认提示 |
| coordinatesType | `string` | 'bd09' | 默为百度坐标,可设置为'gcj02' |

View File

@ -6,8 +6,29 @@ import {Icon} from './icons';
declare const BMap: any;
/**
*
* COORDINATES_WGS84 = 1, WGS84坐标
* COORDINATES_WGS84_MC = 2, WGS84的平面墨卡托坐标
* COORDINATES_GCJ02 = 3GCJ02坐标
* COORDINATES_GCJ02_MC = 4, GCJ02的平面墨卡托坐标
* COORDINATES_BD09 = 5, bd09经纬度坐标
* COORDINATES_BD09_MC = 6bd09墨卡托坐标
* COORDINATES_MAPBAR = 7mapbar地图坐标
* COORDINATES_51 = 851
*/
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;

View File

@ -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}
/>
) : (

View File

@ -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() {