- 组件jsx文件
import React, { useEffect, useRef, useState } from 'react';
import './index.less';
import { webMercator2LngLat, getCoordinateCommon } from '@/util/transformUtil';
import icon_punch_locate from '@/asset/image/informationization/afterSalesPunch/icon_punch_locate.png';
const Map = (props) => {
const mapRef = useRef(null);
const [position, setPosition] = useState(null);
const initMap = () => {
mapRef.current = new window.AMap.Map('map-container', {
resizeEnable: true,
zoom: 15
});
mapRef.current.on('click', getLnglat);
window.AMap.plugin(['AMap.ToolBar'], () => {
mapRef.current.addControl(new window.AMap.ToolBar());
});
getCoordinateCommon((coordinate) => {
const [x, y] = coordinate.split(',');
const [lng, lat] = webMercator2LngLat(x, y);
mapRef.current.clearMap();
const marker = new window.AMap.Marker({
icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png',
position: [lng, lat]
});
marker.setMap(mapRef.current);
mapRef.current.setCenter(new window.AMap.LngLat(lng, lat));
getAddressByPosition(lng, lat);
});
};
const getLnglat = (e) => {
mapRef.current.clearMap();
const x = e.lnglat.getLng();
const y = e.lnglat.getLat();
const marker = new window.AMap.Marker({
icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png',
position: [x, y]
});
marker.setMap(mapRef.current);
mapRef.current.setCenter(new window.AMap.LngLat(x, y));
};
const getAddressByPosition = (lng, lat) => {
const geocoder = new window.AMap.Geocoder();
geocoder.getAddress(new window.AMap.LngLat(lng, lat), (status, result) => {
if (String(status) === 'complete') {
const address = result.regeocode.formattedAddress;
setPosition(address);
}
});
};
useEffect(() => {
initMap();
}, []);
return (
<div className="map-wrapper">
<div id="map-container"></div>
<div className="map-input">
<div>
<img src={icon_punch_locate} />
<span>{position}</span>
</div>
<span
className="re-locate"
onClick={() => {
initMap();
}}>
重新定位
</span>
</div>
</div>
);
};
export default Map;
- transformUtil文件
const myBrowser= () => {
const ua = navigator.userAgent;
const android = ua.match(/(Android);?[\s\/]+([\d.]+)?/),
iphone = ua.match(/(Android);?[\s\/]+([\d.]+)?/),
chrome = ua.match(/Chrome\/([\d.]+)/) || ua.match(/CriOS\/([\d.]+)/),
safari =
webview ||
ua.match(/Version\/([\d.]+)([^S](Safari)|[^M]*(Mobile)[^S]*(Safari))/),
webview =
!chrome && ua.match(/(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/),
miniprogram = ua.toLowerCase().includes("miniprogram"),
weixinpage = ua.toLowerCase().includes("micromessenger"),
dingTalk = ua.toLowerCase().includes("dingtalk"),
wxWork = ua.toLowerCase().includes("wxwork")
if (dingTalk) {
return "dingtalk"
}
if (wxWork) {
return 'wxWork'
}
if (miniprogram && weixinpage) {
return "miniprogram";
}
if (!weixinpage && android) {
return "android";
}
if (!weixinpage && webview) {
return "iOS";
}
if (weixinpage) {
return "wxcommon"
}
return "web";
}
const positionByGaode = (callback) => {
let that=this
window.AMap.plugin('AMap.Geolocation', () => {
const geolocation = new window.AMap.Geolocation({
enableHighAccuracy: true,
timeout: 10000,
buttonOffset: new window.AMap.Pixel(10, 20),
zoomToAccuracy: true,
buttonPosition: 'RB'
});
if (window.AMap.UA.ios) {
let remoGeo = new window.RemoGeoLocation();
navigator.geolocation.getCurrentPosition = function () {
return remoGeo.getCurrentPosition.apply(remoGeo, arguments);
};
navigator.geolocation.watchPosition = function() {
return remoGeo.watchPosition.apply(remoGeo, arguments);
};
}
geolocation.getCurrentPosition((status, data) => {
if(status == 'complete'){
const { position: { lng, lat } } = data;
callback&&typeof callback === 'function' && callback(lng, lat);
}else{
console.log(data)
callback&&typeof callback === 'function' && callback('', '');
}
});
});
}
const lngLat2WebMercator = (lng, lat) => {
const earthRad = 6378137.0;
const x = ((lng * Math.PI) / 180) * earthRad;
const a = (lat * Math.PI) / 180;
const y =
(earthRad / 2) * Math.log((1.0 + Math.sin(a)) / (1.0 - Math.sin(a)));
return [x, y];
};
const webMercator2LngLat = (x, y) => {
let lng = (x / 20037508.34) * 180;
let lat = (y / 20037508.34) * 180;
lat =
(180 / Math.PI) *
(2 * Math.atan(Math.exp((lat * Math.PI) / 180)) - Math.PI / 2);
return [lng, lat];
};
const getCoordinateCommon = (callback) => {
const BROWSER_TYPE = myBrowser1();
const isPlatformMiniProgram = BROWSER_TYPE === 'miniprogram'
const isPlatformDingtalk = BROWSER_TYPE === 'dingtalk'
const isPlatformWeb = BROWSER_TYPE === 'web'
const isPlatformAndroid = BROWSER_TYPE === 'android'
const isPlatformIos = BROWSER_TYPE === 'iOS'
const isPlatformWxWork = BROWSER_TYPE === 'wxWork'
const isPlatformWxCommon = BROWSER_TYPE === 'wxcommon'
if(isPlatformAndroid || isPlatformIos) {
window.$bridge.callHandler("GET_COORDINATE", "调用获取坐标信息", data => {
const [lng, lat] = data.split(',');
const [x, y] = lngLat2WebMercator(lng, lat);
typeof callback === 'function' && callback(`${x},${y}`);
});
} else if(isPlatformMiniProgram || isPlatformWxWork || isPlatformWxCommon) {
window.wx.getLocation({
type: 'wgs84',
success (res) {
const lng = res.longitude;
const lat = res.latitude;
const [x, y] = lngLat2WebMercator(lng, lat);
typeof callback === 'function' && callback(`${x},${y}`);
},
fail(err) {
positionByGaode((lng, lat) => {
const [x, y] = lngLat2WebMercator(lng, lat);
typeof callback === 'function' && callback(`${x},${y}`);
});
}
})
} else {
positionByGaode((lng, lat) => {
const [x, y] = lngLat2WebMercator(lng, lat);
typeof callback === 'function' && callback(`${x},${y}`);
});
}
}
- 样式文件
@import '../../common/common.less';
@img-width: 0.5rem;
.map-wrapper {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
#map-container {
width: 100%;
flex: 1;
.amap-copyright {
display: none !important;
}
}
.map-input {
line-height: @line-height;
height: @line-height;
background-color: #ffffff;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 0.32rem;
width: 100%;
div {
display: flex;
align-items: center;
width: 80%;
img {
width: @img-width;
height: @img-width;
}
span {
color: #333333;
font-weight: bold;
margin-left: 0.1rem;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
.re-locate {
font-weight: 500;
color: #00a189;
white-space: nowrap;
}
}
}