@react-google-maps/api实现谷歌地图中添加多边围栏,并可编辑,编辑后可获得围栏各个点的经纬度

先上一张效果图 看看是不是大家想要的效果~ ❤️

由于该功能微微复杂一点,为了让大家精准了解

我精简了一下地图代码

大家根据自己的需求将center值和paths,用setState做活就可以了

1.第一步要加入项目package.json中或者直接yarn install它都可以

cpp 复制代码
"@react-google-maps/api": "^2.19.3",

2.加入项目中

cpp 复制代码
import React, { PureComponent } from 'react';
import { GoogleMap, LoadScript, PolygonF } from '@react-google-maps/api';

export class Geofences extends PureComponent<{}, {}> {
    render() {
        const key = ''; //谷歌申请的key
        const paths = [[
            { lat: -34.397, lng: 150.644 },
            { lat: -34.297, lng: 150.744 },
            { lat: -34.197, lng: 150.644 },
        ],
            [
                { lat: -34.397, lng: 150.644 },
                { lat: -34.297, lng: 150.744 },
                { lat: -34.197, lng: 150.644 },
            ]];
        return (
            <div style={{ width: '100%', height: '400px' }}>
                <LoadScript googleMapsApiKey={key}>
                    <GoogleMap
                        mapContainerStyle={{ width: '100%', height: '400px' }}
                        center={{ lat: -34.397, lng: 150.644 }}
                        zoom={13}
                    >
                        {paths.map((path, index) => {
                            return (
                                <PolygonF
                                    path={path}
                                    editable={true}
                                    draggable={true}
                                    onEdit={(e)=>{
                                        const paths = e.getPaths().getArray();
                                        const coordinates = paths.map((path:any) => {
                                            return path.getArray().map((latLng:any) => {
                                                return {
                                                    lat: latLng.lat(),
                                                    lng: latLng.lng(),
                                                };
                                            });
                                        });
                                        //这就是改变完后的各个点的坐标
                                        console.log(coordinates);
                                    }}
                                />
                            );
                        })}
                    </GoogleMap>
                </LoadScript>
            </div>
        );
    }
}

运行起来的效果是有两个多边栏,

你拖债任意一个点,都能获得多边围栏每个点的坐标

在做这个需求时有一个小点就是添加一个配送区域(5公里直径内的)矩形围栏

我做的比较简单 大家看看有没有帮助, 也是精简代码。测试效果上相对是精准的

cpp 复制代码
//谷歌,根据经纬度获取以它为中心半径为5公里内的矩形的四个点经纬度
    getDefalutPoints = (lng: number, lat: number) => {
        //方法一:不精准
        // const num = 0.014607; //5公里半径维度
        // const path1 = `${lng - num},${lat + num}`;
        // const path2 = `${lng + num},${lat + num}`;
        // const path3 = `${lng + num},${lat - num}`;
        // const path4 = `${lng - num},${lat - num}`;
        // return `${path1};${path2};${path3};${path4}`;
        
        //方法二
        //数字 111 代表的是地球表面上每度纬度大约对应的公里数。这是一个常用的近似值,用于简化地球表面的计算,尤其是当需要快速估算或不需要非常高精度的场合。
        const radiusKm = 5;
        const latI = radiusKm / 111; //维度增量
        const lngI = radiusKm / (111 *Math.cos(lat* Math.PI/180));
        const zs = `${lng+lngI},${lat+latI}`;
        const ys = `${lng-lngI},${lat+latI}`;
        const zx = `${lng-lngI},${lat-latI}`;
        const yx = `${lng+lngI},${lat-latI}`;
        const points = `${zs};${ys};${zx};${yx}`;
        return points;
    };
    
const lng = 150.644;
const lat =  -34.397;
const defalutPoints = this.getDefalutPoints(lng, lat);
console.log(defalutPoints);

附上效果图一张

温馨提示!!!:在自己开发环境可以正常渲染,然后正式部署到环境上的时候渲染不出来,有个错误提示

解决方式是:找后端人员

设置Content-Security-Policy 允许可以要加载的外部脚本 add_header Content-Security-Policy "script-src 'self' https://maps.googleapis.com 'unsafe-inline' 'unsafe-eval' blob: data:;";

亲测有效~ ❤️

相关推荐
008爬虫实战录7 分钟前
【最新猿人学】 验证码 - 图文点选 文字验证码识别
前端·javascript
一叶飘零晋29 分钟前
【(一)Electron 使用之如何用vite+vue3搭建初始框架】
前端·javascript·electron
光影少年41 分钟前
前端SSR和ssg区别
前端·vue.js·人工智能·学习·react.js
祖国的好青年1 小时前
VS Code 搭建 React Native 开发环境(Windows 实战指南)
android·windows·react native·react.js
恶猫1 小时前
网页自动化模拟操作时,模拟真实按键触发事件【终级方案】
前端·javascript·自动化·vue·网页模拟
ZC跨境爬虫2 小时前
跟着 MDN 学 HTML day_2:(表单分组与高级输入控件实战)
前端·javascript·css·ui·html
千寻girling3 小时前
滑动窗口刷了快一个月(26天)了 , 还没有刷完. | 含(操作系统学什么的Java 后端)
java·开发语言·javascript·c++·人工智能·后端·python
一袋米扛几楼983 小时前
【报错问题】彻底解决 TypeScript 报错 TS2769: No overload matches this call (JWT 篇)
linux·javascript·typescript
天天进步20153 小时前
魔音漫创源码解析:架构总览:Electron 30 + React 18 + Zustand,构建桌面级影视生产工具
react.js·架构·electron
牧码岛4 小时前
Web前端之JavaScrip中的Array、Object、Map和Set详解
前端·javascript·web·web前端