import React, { useEffect } from 'react'
import styles from './index.less'
import AMapLoader from '@amap/amap-jsapi-loader';
export default function Map() {
let temp;
let map: {
getBounds: () => {
(): any;
new(): any;
getSouthWest: { (): any; new(): any };
};
panBy: (arg0: number, arg1: number) => void;
addControl: (arg0: any) => void;
};
const init = () => {
AMapLoader.load({
key: '81bcc0d331d7a59acce65fd3ea3f41e6',
version: '2.0',
plugins: ['AMap.ToolBar'],
AMapUI: {
version: '1.1',
plugins: [
'misc/PathSimplifier',
'overlay/SimpleMarker',
'misc/PositionPicker',
],
},
}).then((AMap) => {
AMapUI.loadUI(['misc/PositionPicker'], function (PositionPicker: any) {
map = new AMap.Map('container', {
zoom: 16,
scrollWheel: false,
});
AMap.plugin('AMap.Geolocation', function() {
var geolocation = new AMap.Geolocation({
enableHighAccuracy: true,
timeout: 10000,
buttonPosition:'RB',
buttonOffset: new AMap.Pixel(10, 20),
zoomToAccuracy: true,
});
map.addControl(geolocation);
geolocation.getCurrentPosition(function(status,result){
if(status=='complete'){
onComplete(result)
}else{
onError(result)
}
});
});
function onComplete(data) {
var str = [];
str.push('定位结果:' + data.position);
str.push('定位类别:' + data.location_type);
if(data.accuracy){
str.push('精度:' + data.accuracy + ' 米');
}
str.push('是否经过偏移:' + (data.isConverted ? '是' : '否'));
console.log(str)
}
function onError(data) {
console.log(data)
}
var positionPicker = new PositionPicker({
mode: 'dragMap',
map,
});
var dragMapMode = document.getElementsByName('mode')[0];
var dragMarkerMode = document.getElementsByName('mode')[1];
dragMapMode && dragMapMode.addEventListener('change', onModeChange);
dragMarkerMode &&
dragMarkerMode.addEventListener('change', onModeChange);
positionPicker.start();
map.panBy(0, 1);
map.addControl(
new AMap.ToolBar({
liteStyle: true,
}),
);
});
}).catch((e) => {
console.log(e);
});
};
useEffect(() => {
init()
}, [])
return (
<div>
<div id='container' style={{ width: '100%', height: '420px' }}></div>
<div className={styles.search}><i className="iconfont"></i>搜索地点</div>
{/* <h4 id='status'></h4>
<p id='result'></p>
<p>由于众多浏览器已不再支持非安全域的定位请求,为保位成功率和精度,请升级您的站点到HTTPS。</p> */}
</div>
)
}