当前位置: 首页 > wzjs >正文

专业开发app公司seo优化网站优化

专业开发app公司,seo优化网站优化,房产网站怎么做,福田网站制作比较好的1.技术选项: vitereactantdesign load-script 2.实现思路: 1.按需加载如何实现? 要实现按需加载就不能直接在项目的入口文件这种地方去通过script标签引入,这里使用load-script封装了一个加载百度地图的Bmap.js方法,实现动态的插入script脚本。 根…

1.技术选项:

vite+react+antdesign

load-script

2.实现思路:

1.按需加载如何实现?

  要实现按需加载就不能直接在项目的入口文件这种地方去通过script标签引入,这里使用load-script封装了一个加载百度地图的Bmap.js方法,实现动态的插入script脚本。

根目录下创建Bmap.js文件

import _loadScript from 'load-script';export function Map(url) {return new Promise((resolve, reject) => {_loadScript(url, (error, script) => {if (error) {return reject(error);}console.log('====================================');console.log(script);console.log('====================================');resolve(script);});});
}export default function loadBMap() {return new Promise(function(resolve, reject) {if (typeof BMap !== 'undefined') {resolve(BMap);return true;}window.onBMapCallback = function() {resolve(BMap);};let script = document.createElement('script');script.type = 'text/javascript';script.src ='https://api.map.baidu.com/api?v=3.0&ak=自己的';script.onerror = reject;document.head.appendChild(script);});
}
2.初始化加载地图(注意事项)

要初始化加载地图,我们需要确保的时候,在页面绘制完,或者弹框加载完成之后再去调用初始化的方法,否则就会报错。

报错示例:

Cannot read properties of undefined (reading  kc )

封装示例图

3.map.js脚本实现如下:

// 加载百度地图
export function LoadBaiduMapScript() {console.log("百度地图脚本初始化ing----------------");const BMap_URL ="https://api.map.baidu.com/api?v=3.0&ak=换成自己的&callback=onBMapCallback";return new Promise((resolve, _reject) => {// 如果已加载直接返回if (typeof BMap !== "undefined") {resolve(BMap);return true;}// 百度地图异步加载回调处理window.onBMapCallback = function () {console.log("百度地图脚本初始化成功...");resolve(BMap);};// 插入script脚本const scriptNode = document.createElement("script");scriptNode.setAttribute("type", "text/javascript");scriptNode.setAttribute("src", BMap_URL);document.body.appendChild(scriptNode);});
}

React实现代码如下:

注意:new BMap 可能会爆红 不用管,不影响使用

地图实例,标记示例,手掌选择实例使用useRef存储吗,使用useState存储会导致内容重新渲染,造成初始化失败

import { Modal, Button, message, Input, Spin } from "antd";
import {useState,forwardRef,useImperativeHandle,useEffect,useRef,
} from "react";
import positionIcon from "@/assets/images/position.png";
import { LoadBaiduMapScript } from "./map";
import "./index.less";interface MapPropsType {onConfirm: (val) => void;
}const MapChoice = forwardRef((props: MapPropsType, ref) => {const { Search } = Input;const [isModalOpen, setIsModalOpen] = useState(false);const [searchValue, setSearchValue] = useState<string>("");const [loading, setLoading] = useState<boolean>(false);const [address, setAddress] =useState<string>("河南省郑州市二七区建设东路48号");const [lon, setLon] = useState<number>(113.65);const [lat, setLat] = useState<number>(34.76);// 使用 ref 存储地图相关对象(避免状态异步问题)const mapRef = useRef<any>(null);const pointRef = useRef<any>(null);const markerRef = useRef<any>(null);const showModal = () => {setIsModalOpen(true);};const handleOk = () => {setIsModalOpen(false);const data = { address, lon, lat };props.onConfirm(data); // 回调方式};const handleCancel = () => {setIsModalOpen(false);};useImperativeHandle(ref, () => ({showModal,handleOk,handleCancel,}));useEffect(() => {if (!isModalOpen) return;const init = async () => {await LoadBaiduMapScript();initMap({});await browserPosition();};init();}, [isModalOpen]);// 初始化地图const initMap = (record: any) => {const currentAddress = record.address || address || "郑州市";const currentLon = record?.coordinates?.lon ?? lon;const currentLat = record?.coordinates?.lat ?? lat;// 创建地图实例(使用 ref 存储)const mapInstance = new BMap.Map("container");const pointInstance = new BMap.Point(currentLon, currentLat);const myIcon = new BMap.Icon(positionIcon, new BMap.Size(23, 25));const markerInstance = new BMap.Marker(pointInstance, myIcon);// 存储到 refmapRef.current = mapInstance;pointRef.current = pointInstance;markerRef.current = markerInstance;// 初始化地图设置mapInstance.centerAndZoom(pointInstance, 15);mapInstance.enableScrollWheelZoom();mapInstance.addOverlay(markerInstance);// 更新状态setAddress(currentAddress);setLon(currentLon);setLat(currentLat);// 绑定点击事件mapInstance.addEventListener("click", (e: any) => {const clickedPoint = new BMap.Point(e.point.lng, e.point.lat);mapInstance.centerAndZoom(clickedPoint, 15);pointRef.current = clickedPoint;// 逆地理编码获取地址const gc = new BMap.Geocoder();gc.getLocation(clickedPoint, (rs: any) => {if (rs?.address) {setAddress(rs.address);setLon(e.point.lng);setLat(e.point.lat);upInfoWindow(rs.address, e.point.lng, e.point.lat);}});});// 初始信息窗口upInfoWindow(currentAddress, currentLon, currentLat);};// 更新信息窗口const upInfoWindow = (address: string, lon: number, lat: number) => {if (!mapRef.current || !pointRef.current) return;const opts = {width: 250,height: 120,title: "经纬度",};const word = `<div>地址:${address}</div><div>经度:${lon}</div><div>纬度:${lat}</div>`;const infoWindow = new BMap.InfoWindow(word, opts);mapRef.current.openInfoWindow(infoWindow, pointRef.current);markerRef.current.addEventListener("click", () => {mapRef.current.openInfoWindow(infoWindow, pointRef.current);});};// 地址搜索const handleSearch = () => {if (!searchValue?.trim()) {message.warning("搜索框不能为空");return;}const myGeo = new BMap.Geocoder();myGeo.getPoint(searchValue.trim(), (point: any) => {if (point) {mapRef.current.centerAndZoom(point, 15);pointRef.current = point;setLon(point.lng);setLat(point.lat);const gc = new BMap.Geocoder();gc.getLocation(point, (rs: any) => {if (rs?.address) {setAddress(rs.address);upInfoWindow(rs.address, point.lng, point.lat);}});} else {message.warning("您选择的地址没有解析到结果!");}});};// 浏览器定位const browserPosition = async () => {setLoading(true);const geolocation = new BMap.Geolocation();geolocation.getCurrentPosition(async (r: any) => {if (r?.point) {const point = new BMap.Point(r.point.lng, r.point.lat);mapRef.current.centerAndZoom(point, 15);pointRef.current = point;const gc = new BMap.Geocoder();gc.getLocation(point, (rs: any) => {if (rs?.address) {setAddress(rs.address);setLon(r.point.lng);setLat(r.point.lat);upInfoWindow(rs.address, r.point.lng, r.point.lat);}});} else {message.error("定位失败,请手动输入经纬度");}setLoading(false);});};return (<><div className="flex items-center"><div className="mr-2">位置</div><Button type="primary" onClick={showModal}>唤醒地图</Button></div><Modaltitle="地图选择"open={isModalOpen}onOk={handleOk}onCancel={handleCancel}width={1000}okText={"确认"}cancelText={"关闭"}><Spin spinning={loading}><Searchplaceholder="请输入地址"value={searchValue}onChange={(e) => setSearchValue(e.target.value)}onSearch={handleSearch}style={{ width: "40%" }}/><div id="container" className="positionbox"></div></Spin></Modal></>);
});export default MapChoice;

less样式代码

.positionbox {width: 100%;height: 64vh;margin-top: 20px;}

实现效果如下

http://www.dtcms.com/wzjs/447718.html

相关文章:

  • 网站被镜像怎么做合肥百度关键词优化
  • 金华网站建设方案优化网络营销有哪些
  • 可信网站注册河北百度seo
  • 寻找做项目的网站临沂seo代理商
  • 江苏多地发布最新情况湖南企业竞价优化首选
  • 网站开发赚钱么今日热搜榜前十名
  • 域名的网站建设方案书怎么写宁波seo
  • 做网站卖菜刀需要什么手续自己怎么做关键词优化
  • 武汉网站改版维护怎么把广告发到各大平台
  • 做中国旅游网站的目的与必要性东莞网络排名优化
  • 企业怎样做网站百度推广产品
  • 常州模板建站代理关键词搜索量查询工具
  • 网站策划 英文如何优化培训体系
  • 龙岩营销型网站建设品牌推广文案
  • 漯河住房和城乡进建设委员会网站网温州seo团队
  • 李沧网站建设公司网站制作教程
  • 做医院网站公司美国搜索引擎排名
  • 做网站 如何注册公司百度app 浏览器
  • 水墨风格的网站推广方式都有哪些
  • 余姚做网站设计的公司注册网站平台
  • 设计公司logo网站重庆网站排名推广
  • 做网站多少分辨率就可以免费网络推广软件
  • 网站建设有什么证新网站怎么推广
  • 网站建设群广州新闻播报
  • 广东做网站广告投放公司
  • 如何帮人做网站廊坊网站建设优化
  • 做视频网站公司要怎么做的线上推广是做什么的
  • 任务网站建设网站搭建免费
  • 四平市住房和畅想建设局网站宁波seo关键词费用
  • facebook是个什么网站线上推广方案模板