From a923c0ad0bc50abd9160b068abba60beea98275b Mon Sep 17 00:00:00 2001 From: dminyi <1301963064@qq.com> Date: Tue, 27 Aug 2024 10:22:12 +0800 Subject: [PATCH] 地图 --- gz-customerSystem/src/views/register/visit/component/map.jsx | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 156 insertions(+), 12 deletions(-) diff --git a/gz-customerSystem/src/views/register/visit/component/map.jsx b/gz-customerSystem/src/views/register/visit/component/map.jsx index 2833d94..c319d6b 100644 --- a/gz-customerSystem/src/views/register/visit/component/map.jsx +++ b/gz-customerSystem/src/views/register/visit/component/map.jsx @@ -2,20 +2,164 @@ * @Author: dminyi 1301963064@qq.com * @Date: 2024-08-17 14:41:57 * @LastEditors: dminyi 1301963064@qq.com - * @LastEditTime: 2024-08-17 16:25:27 + * @LastEditTime: 2024-08-27 10:17:49 * @FilePath: \gzDyh\gz-customerSystem\src\views\register\visit\component\map.jsx * @Description: 地图 */ -import React from 'react'; +import React, { useEffect, useRef, useState } from 'react'; import { Map, Marker, NavigationControl, InfoWindow } from 'react-bmapgl'; -export default function MapView() { - return ( - <div> - <Map center={{ lng: 116.402544, lat: 39.928216 }} zoom="11"> - <Marker position={{ lng: 116.402544, lat: 39.928216 }} /> - <NavigationControl /> - <InfoWindow position={{ lng: 113.27, lat: 23.12 }} text="广州市" title="地标" /> - </Map> - </div> - ) +import { Form, Input, Button, Message } from '@arco-design/web-react'; +import { Row, Col } from 'antd'; +const FormItem = Form.Item; + +const formItemLayout = { + labelCol: { + span: 4, + }, + wrapperCol: { + span: 17, + }, +}; + +// var map = new window.BMapGL.Map("container"); +// map.centerAndZoom(new window.BMapGL.Point(116.401952, 40.032704), 12); +// map.enableScrollWheelZoom(); + + + +export default function MapView(props) { + const mapRef = useRef() + const formRef = useRef() + const [addressList, setAddressList] = useState([]) + + useEffect(() => { + if (mapRef.current) { + let geolocation = new window.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(); + + 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'); // 添加自定义样式标签 + } else { + Message.warning('您输入的地址没有解析到结果!'); + } + }, '广州市'); + } + }); + } + }; + + + + // 解析地址为中文 + const handleAnalysis = (pt, name) => { + let geoc = new window.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 surroundingPois = rs.surroundingPois; + if (name) { + addName += ` ${name}`; // 添加地点名称 + } + mapRef.current?.map.centerAndZoom(pt, 15); + mapRef.current?.map.addOverlay(new window.BMapGL.Marker(pt, { title: addName })); + setAddressList(surroundingPois); + console.log(pt, addName, 'pt', 'addName') + }); + }; + + console.log(addressList, 'addressList') + + + + + + + + + + return ( + <div> + <Row gutter={[16, 0]}> + <Col span={16}> + <Form + ref={formRef} + layout='inline' + {...formItemLayout} + style={{ marginBottom: '8px' }} + > + <FormItem + label='查询位置:' + field='name' + > + <Input placeholder='请输入' style={{ width: '522px' }} /> + </FormItem> + <Button style={{ marginRight: '20px' }}> + 重置 + </Button> + <Button + type="primary" + onClick={handleSubmit} + > + 查询 + </Button> + </Form> + <Map + ref={mapRef} + zoom="15" + enableScrollWheelZoom + onClick={(e) => { + let pt = e.latlng; + handleAnalysis(pt, null); + // addCustomLabel(pt, 'label'); // 添加自定义样式标签 + }} + > + <Marker position={{ lng: 116.402544, lat: 39.928216 }} /> + <NavigationControl /> + <InfoWindow position={{ lng: 116.402544, lat: 39.928216 }} text="内容" title="标题" /> + + </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} {item.title}</div> + ))} + </div> + </Col> + </Row> + </div> + ) } \ No newline at end of file -- Gitblit v1.8.0