高德地图key

news/2024/10/4 21:52:13/

第一步:加载地图组件 AMapLoader.load
第二步:初始化地图组件,在地图上做标记
第三步:打开地图弹窗,new AMap.InfoWindow
第四步:搜索弹窗上的select,调用接口,返回数据展示在select上(后端模糊搜索 + 防抖)
第五步:选择心仪地址,进行回调

javascript">import AMapLoader from '@amap/amap-jsapi-loader'
import React, { useEffect, useRef, useState } from 'react'
import debounce from 'lodash/debounce'const MapSelect = (props) => {const { value } = propsconst geocoder = useRef<any>()const map = useRef<any>()const [AMap, setAMap] = useState(null)const mapRef = useRef<any>()useEffect(() => {AMapLoader.load({//首次调用 loadkey: 'xxx', //首次load key为必填version: '2.0',plugins: ['AMap.Geocoder'],}).then((_AMap) => {// 组件初始化之后将获取到的AMap对象setAMap(_AMap)const AMap = _AMapgeocoder.current = new AMap.Geocoder({})initMap()})}, [])//初始化地图const initMap = () => {const { longitude, latitude, text } = value || {}const center =longitude && latitude ? new AMap.LngLat(longitude, latitude) : nullmap.current = new AMap.Map(mapRef.current, {resizeEnable: true,jogEnable: false,zoom: 14,center,})center && updateMark(center, text) // 地图上做标记}// 更新markerconst updateMark = (center, text) => {}const debounceSearchApi = debounce((value: string) => {searchByApi(value)}, 300)// 使用api查询搜索出来的地址const searchByApi = async (value: string) => {}// 打开地图弹窗,展示地图组件const setInfoWindowContent = (e) => {infoWindow = new AMap.InfoWindow({isCustom: true,content,offset: new AMap.Pixel(0, -40),})infoWindow?.open(map.current, location)map?.current?.setCenter(center)map?.current?.setZoom(14)}return (<><SearchonSearch={openSelectLngLat}placeholder="请输入地址"value={text}onChange={handleInputChange}onCompositionStart={handleCompositionStart}onCompositionEnd={handleCompositionEnd}style={{ width, ...style }}/><divonMouseUp={onMouseUp}onMouseMove={onMouseMove}onMouseDown={onMouseDown}onClick={() => {if (!isMove) {openSelectLngLat()}}}><div ref={mapRef} />{!(longitude && latitude) && (<div>{icon && <img style={{ width: 28, height: 28 }} src={icon} />}<span>{markTitle || '去标注'}</span></div>)}</div><Modaltext={selectLngLatModalText}visible={selectLngLatModalVisible}isMarkTextUseSelectAddress={isMarkTextUseSelectAddress}info={selectLngLatModalLngLat}onClose={closeSelectLngLatModal}onChange={ModalGetLngLat}bindLatestAddress={(getLatestAddress && (updateAddress as any)) || null}/></>)
}export default MapSelect

弹框

javascript">    <Modalvisible={visible}destroyOnCloseonCancel={handleClose}onOk={handleSubmit}okText="保存位置"title="设置定位"><div><Selectstyle={{ width: '720px', marginBottom: '10px' }}showSearchdefaultValue={text}placeholder="请输入需要查找的位置"onSearch={(value) => {debounceSearchApi(value)}}options={addressOptions}filterOption={false}onChange={handleChangeOptions}/></div><div><div style={{ width: '720px', height: '600px' }} ref={modalMapRef} /></div></Modal>

http://www.ppmy.cn/news/1534612.html

相关文章

什么是 HTTP 请求中的 options 请求?

在 Chrome 开发者工具中的 Network 面板看到的 HTTP 方法 OPTIONS&#xff0c;其实是 HTTP 协议的一部分&#xff0c;用于客户端和服务器之间进行“预检”或“协商”。OPTIONS 请求的作用是让客户端能够获取关于服务器支持的 HTTP 方法和其他跨域资源共享 (CORS) 相关的信息&am…

IDE 使用技巧与插件推荐(含例说明)

在使用集成开发环境&#xff08;IDE&#xff09;进行编程时&#xff0c;掌握一些技巧和使用高效的插件可以显著提高开发效率。以下是一些通用的IDE使用技巧和插件推荐&#xff0c;适用于多种流行的IDE&#xff0c;如IntelliJ IDEA、Visual Studio Code、PyCharm等。每个技巧和插…

使用CSS实现酷炫加载

使用CSS实现酷炫加载 效果展示 整体页面布局 <div class"container"></div>使用JavaScript添加loading加载动画的元素 document.addEventListener("DOMContentLoaded", () > {let container document.querySelector(".container&q…

SpringCloud 配置 feign.hystrix.enabled: true 不生效

SpringCloud 配置 feign.hystrix.enabled: true 不生效的原因 feign 启用 hystrix feign 默认没有启用 hystrix&#xff0c;添加配置&#xff0c;启用 hystrix feign.hystrix.enabledtrue application.yml 添加配置 feign:hystrix:enabled: true启用 hystrix 后&#xff0c;访…

初识算法 · 双指针(4)

目录 前言&#xff1a; 复写零 题目解析 算法原理 算法编写 四数之和 题目解析 算法原理 算法编写 前言&#xff1a; 本文是双指针算法的最后一文&#xff0c;以复写零和四数之和作为结束&#xff0c;介绍方式同样是题目解析&#xff0c;算法原理&#xff0c;算法编写…

TIM(Timer)定时器的原理

一、介绍 硬件定时器的工作原理基于时钟信号源提供稳定的时钟信号作为计时器的基准。计数器从预设值开始计数&#xff0c;每当时钟信号到达时计数器递增。当计数器达到预设值时&#xff0c;定时器会触发一个中断信号通知中断控制器处理相应的中断服务程序。在中断服务程序中&a…

云服务架构与华为云架构

目录 1.云服务架构是什么&#xff1f; 1.1 云服务模型 1.2 云部署模型 1.3 云服务架构的组件 1.4 云服务架构模式 1.5 关键设计考虑 1.6 优势 1.7 常见的云服务架构实践 2.华为云架构 2.1 华为云服务模型 2.2 华为云部署模型 2.3 华为云服务架构的核心组件 2.4 华…

C++游戏开发详解:从入门到实践

目录 引言 使用C进行游戏开发的优势 常用的C游戏引擎和工具 C游戏引擎比较 开发工具 C游戏开发核心概念与代码示例 面向对象编程&#xff08;OOP&#xff09; 封装 继承 多态 内存管理 手动内存管理 智能指针 内存池 并发编程 多线程 同步机制 游戏开发流程 …