forked from gzzfw/frontEnd/gzDyh

zhangyongtian
2024-08-27 457f4a4be58936a7cbdefe307aad539cee1eb62a
feat: 地图
2 files added
2 files modified
167 ■■■■■ changed files
gz-customerSystem/src/assets/images/icon/frame.png patch | view | raw | blame | history
gz-customerSystem/src/assets/images/icon/selectIcon.png patch | view | raw | blame | history
gz-customerSystem/src/views/register/index.less 46 ●●●●● patch | view | raw | blame | history
gz-customerSystem/src/views/register/visit/component/map.jsx 121 ●●●● patch | view | raw | blame | history
gz-customerSystem/src/assets/images/icon/frame.png
gz-customerSystem/src/assets/images/icon/selectIcon.png
gz-customerSystem/src/views/register/index.less
@@ -197,3 +197,49 @@
//     background-color: #1A6FB8;
//     border-color: #1A6FB8;
// }
.mapPopconfirm {
    width: 300px;
    height: 90px;
    background: #fff;
    border-radius: 4px;
    box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.10);
    padding: 15px;
    &::after {
        content: " ";
        position: absolute;
        height: 0;
        width: 0;
        border: solid transparent;
        top: 100%;
        right: 50px;
        border-top-color: white;
        border-width: 10px;
    }
    .mapPopconfirm-btn {
        background-color: #1A6FB8;
        color: #fff;
        cursor: pointer;
        padding: 3px 10px;
        width: 80px;
        text-align: center;
        position: absolute;
        right: 15px;
        bottom: 15px;
    }
}
.mapVicinity {
    margin-bottom: 5px;
    cursor: pointer;
    &:hover {
        color: #1A6FB8;
    }
    .mapVicinityIcon {
    }
}
gz-customerSystem/src/views/register/visit/component/map.jsx
@@ -7,8 +7,9 @@
 * @Description: 地图
 */
import React, { useEffect, useRef, useState } from 'react';
import { Map, Marker, NavigationControl, InfoWindow } from 'react-bmapgl';
import { Form, Input, Button, Message } from '@arco-design/web-react';
import { Map, Marker, NavigationControl, InfoWindow, CustomOverlay } from 'react-bmapgl';
import { Form, Input, Button, Message, Popconfirm } from '@arco-design/web-react';
import { Row, Col } from 'antd';
const FormItem = Form.Item;
@@ -21,55 +22,44 @@
  },
};
// var map = new window.BMapGL.Map("container");
// map.centerAndZoom(new window.BMapGL.Point(116.401952, 40.032704), 12);
// map.enableScrollWheelZoom();
const BMapGL = window.BMapGL
// 自定义Marker的icon
let myIcon = new BMapGL.Icon(require("@/assets/images/icon/frame.png"), new BMapGL.Size(24, 24), {
  // 是否允许缩放图标
  enableScaling: true
});
export default function MapView(props) {
  const mapRef = useRef()
  const formRef = useRef()
  const [addressList, setAddressList] = useState([])
  const [addressData, setAddressData] = useState({})
  const [selectData, setSelectData] = useState({})
  useEffect(() => {
    //初次进入定位在当前浏览器
    if (mapRef.current) {
      let geolocation = new window.BMapGL.Geolocation();
      let geolocation = new BMapGL.Geolocation();
      geolocation.getCurrentPosition((r) => {
        if (geolocation.getStatus() === 0) {
          handleAnalysis(r.point);
        } else {
          Message.warning(`Failed with status ${geolocation.getStatus()}`);
        }
      });
      }, '广州市');
    }
  }, [mapRef]);
  // const addCustomLabel = (point, content) => {
  //   var label = new window.BMapGL.Label(content, {
  //     position: point,
  //     offset: new window.BMapGL.Size(10, 20)
  //   });
  //   label.setStyle({
  //     color: '#000',
  //     fontSize: '30px',
  //     border: '2px solid #1E90FF'
  //   });
  //   mapRef.current.map.addOverlay(label);
  // };
  //查询
  const handleSubmit = () => {
    if (formRef.current) {
      formRef.current.validate(undefined, (errors, values) => {
        if (!errors) {
          var myGeo = new window.BMapGL.Geocoder();
          var myGeo = new BMapGL.Geocoder();
          myGeo.getPoint(values.name, (point) => {
            if (point) {
              mapRef.current.map.centerAndZoom(point, 15);
              mapRef.current.map.addOverlay(new window.BMapGL.Marker(point, { title: values.name }));
              handleAnalysis(point, values.name); // 添加地点名称
              // addCustomLabel(point, 'label'); // 添加自定义样式标签
              handleAnalysis(point); // 添加地点名称
            } else {
              Message.warning('您输入的地址没有解析到结果!');
            }
@@ -79,35 +69,40 @@
    }
  };
  // 解析地址为中文
  const handleAnalysis = (pt, name) => {
    let geoc = new window.BMapGL.Geocoder();
  const handleAnalysis = (pt) => {
    let geoc = new BMapGL.Geocoder();
    geoc.getLocation(pt, (rs) => {
      console.log(rs, 'rsss')
      let addComp = rs.addressComponents;
      let addName = `${addComp.province}${addComp.city}${addComp.district}${addComp.street}${addComp.streetNumber}`;
      let addComp = rs.content;
      let addName = `${addComp.address}  ${addComp.poi_desc}`;
      let surroundingPois = rs.surroundingPois;
      if (name) {
        addName += ` ${name}`; // 添加地点名称
      }
      mapRef.current?.map.centerAndZoom(pt, 15);
      mapRef.current?.map.addOverlay(new window.BMapGL.Marker(pt, { title: addName }));
      mapRef.current?.map.centerAndZoom(pt, 15);//地图指向中心
      mapRef.current?.map.clearOverlays();//清除所有标点
      mapRef.current?.map.addOverlay(new BMapGL.Marker(pt, { icon: myIcon }));//添加标点
      setAddressList(surroundingPois);
      console.log(pt, addName, 'pt', 'addName')
      setAddressData({
        pt,
        addName
      })
    });
  };
  console.log(addressList, 'addressList')
  //点击附近地址
  const clickVicinity = (data) => {
    const address = data.address + data.title
    setSelectData({
      uid: data.uid,
      address
    })
    formRef.current.setFieldValue('name', address)
    handleSubmit()
  }
  //使用地址
  const handleUseAddress = (e) => {
    e.stopPropagation()
    console.log(addressData);
  }
  return (
    <div>
@@ -141,21 +136,33 @@
            enableScrollWheelZoom
            onClick={(e) => {
              let pt = e.latlng;
              handleAnalysis(pt, null);
              // addCustomLabel(pt, 'label'); // 添加自定义样式标签
              handleAnalysis(pt);
            }}
          >
            <Marker position={{ lng: 116.402544, lat: 39.928216 }} />
            <NavigationControl />
            <InfoWindow position={{ lng: 116.402544, lat: 39.928216 }} text="内容" title="标题" />
            <CustomOverlay
              position={addressData.pt || new BMapGL.Point(113.26552377996691, 23.143506946336696)}
              offset={{ width: -91, height: -25 }}
            >
              <div className='mapPopconfirm'>
                <div>{addressData.addName}</div>
                <div className='mapPopconfirm-btn' onClick={handleUseAddress}>使用地址</div>
              </div>
            </CustomOverlay>
          </Map>
        </Col>
        <Col span={8}>
          <div style={{ color: '#86909C', marginTop: '43px' }}>附近地址</div>
          <div style={{ height: 'calc(100% - 64px)', overflowY: 'scroll' }}>
            {addressList?.map((item, index) => (
              <div key={index}>{item.address}&nbsp;&nbsp;&nbsp;&nbsp;{item.title}</div>
          <div style={{ height: '343px', overflowY: 'scroll' }}>
            {addressList?.map(item => (
              <div
                key={item.uid}
                className='mapVicinity'
                onClick={() => { clickVicinity(item) }}
                style={{ color: selectData.uid === item.uid ? '#1A6FB8' : '' }}
              >
                {item.address}&nbsp;&nbsp;&nbsp;&nbsp;{item.title}
                <div className='mapVicinityIcon'></div>
              </div>
            ))}
          </div>
        </Col>