提交 5343f364 authored 作者: 钟是志's avatar 钟是志

定位地址修改

上级 06fc9c30
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import getLocation from '@/H5Public/utils/handleLocation'; import getLocation from '@/H5Public/utils/handleLocation';
export default function Test() { export default function Test() {
const [location, setLocation] = useState({}); const [location, setLocation] = useState({});
...@@ -10,11 +9,11 @@ export default function Test() { ...@@ -10,11 +9,11 @@ export default function Test() {
}; };
useEffect(()=>{ useEffect(()=>{
getLocation(setInfo) getLocation(setInfo);
},[]) },[])
return ( return (
<div id={'baiduMap'}> <div id="baiduMap">
{JSON.stringify(location)} {JSON.stringify(location)}
</div> </div>
); );
......
...@@ -2,22 +2,17 @@ const isHttps = window.location.protocol.indexOf('https') > -1; ...@@ -2,22 +2,17 @@ const isHttps = window.location.protocol.indexOf('https') > -1;
const apiVersion = isHttps ? '3.0' : '2.0'; const apiVersion = isHttps ? '3.0' : '2.0';
const mapContaienrId = "baiduMap"; const mapContaienrId = "baiduMap";
const apiSrc = `https://api.map.baidu.com/api?v=${apiVersion}&ak=WPau2m9l3Rty2HR7Kfc9879nzP2Fj0UU&callback=baiduMapInitialize`; const apiSrc = `https://api.map.baidu.com/api?v=${apiVersion}&ak=WPau2m9l3Rty2HR7Kfc9879nzP2Fj0UU&callback=baiduMapInitialize`;
const hasMapContainer = !!document.getElementById(mapContaienrId);
function loadScript(callBack) { function loadScript(callBack) {
window.baiduMapInitialize = function () { window.baiduMapInitialize = function () {
getLocationByMap(callBack); getLocationByMap(callBack);
} }
if (!hasMapContainer) {
document.write(`
<div id="${mapContaienrId}"></div>
`);
}
document.write(`<script type="text/javascript" src="${apiSrc}"></script>`); document.write(`<script type="text/javascript" src="${apiSrc}"></script>`);
} }
function getLocationByMap(callback) { function getLocationByMap(callback) {
const BMap = window.BMap; const BMap = window.BMap;
//
const map = new BMap.Map(mapContaienrId); const map = new BMap.Map(mapContaienrId);
const point = new BMap.Point(107.303748,34.346725); const point = new BMap.Point(107.303748,34.346725);
map.centerAndZoom(point, 12); map.centerAndZoom(point, 12);
...@@ -93,8 +88,9 @@ export default function getLocation(callBack = () => {}){ ...@@ -93,8 +88,9 @@ export default function getLocation(callBack = () => {}){
if(typeof BMap === 'undefined') { if(typeof BMap === 'undefined') {
loadScript(callBack); loadScript(callBack);
return false; return false;
} }else{
getLocationByMap(callBack); getLocationByMap(callBack);
}
} }
export function getAddress(point, callBack=()=>{}){ export function getAddress(point, callBack=()=>{}){
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论