05-HK 引入百度地图

简介: 本文是 hkzf 移动端 的系列教程,旨在通过一系列的文章来帮助初学者快速掌握基于React技术栈的项目开发。

05-HK 引入百度地图

本文是 hkzf 移动端 的系列教程,旨在通过一系列的文章来帮助初学者快速掌握基于React技术栈的项目开发。

配置AK

public/index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="logo192.png" />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=A00c8eadc65366c5c92155ebbbf535a1"></script>
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>
</html>

初始化地图功能

src/pages/BMap/index.js

    init_city = async (cityName) => {
        // 百度地图API功能
        this.map = new BMap.Map("allmap");    // 创建Map实例
        this.map.centerAndZoom(cityName, this.Sites[this.SiteIndex].zoom);  // 初始化地图,设置中心点坐标和地图级别
        //添加地图类型控件 
        this.map.addControl(new BMap.NavigationControl());
        this.map.addControl(new BMap.ScaleControl());
        this.map.setCurrentCity(cityName);          // 设置地图显示的城市 此项是必须设置的
        //获取区域的id信息
        let cityObj = (await axios.get("/area/info?name=" + cityName)).body;
        this.drawCityHouse(cityObj);

    }

设置绘制房类型

    Sites = [
        { zoom: 12, level: 1, shape: "circle", name: "城市" },
        { zoom: 14, level: 2, shape: "circle", name: "区域" },
        { zoom: 18, level: 3, shape: "rect", name: "小区" },
    ]

绘制房源

    drawCityHouse = async (cityObj) => {
        let res = (await axios.get("/area/map?id=" + cityObj.value)).body;
        this.map.clearOverlays();
        //把数据渲染到地图上面
        //已经点击过一次了
        if (this.Sites[this.SiteIndex].zoom !== this.Sites[0].zoom) {
            let point = new BMap.Point(cityObj.coord.longitude, cityObj.coord.latitude);
            this.map.centerAndZoom(point, this.Sites[this.SiteIndex].zoom)

        }
        res.forEach(e => {
            let point = new BMap.Point(e.coord.longitude, e.coord.latitude);
            let opts = {
                position: point,
                offset: new BMap.Size(0, 0)
            }
            let label = "";
            if (this.Sites[this.SiteIndex].shape === "circle")
                label = new BMap.Label("<div class='circle'><span>" + e.label + "<span><br/>" + e.count + "套</div>", opts);
            else
                label = new BMap.Label("<div class='rect'><span>" + e.label + "<span><br/>" + e.count + "套</div>", opts);
            label.setStyle({
                "background-color": "transparent",
                border: "none"
            })
            label.addEventListener("click", () => {
                if (this.SiteIndex === this.Sites.length) {
                    this.getDetail(e);
                    this.setState({
                        is_show: true
                    })
                    // 让底部的div 变化都稳定了 
                    setTimeout(() => {
                        // 再让被点击的标签 居中即可
                        this.map.panTo(point);
                    }, 500);
                } else {
                    //绘制二级房源
                    this.drawCityHouse(e);
                }
            })
            this.map.addOverlay(label);
        });
        this.SiteIndex++;
    }
目录
相关文章
|
人工智能
【AI绘画】ControlNet 之 Reference only 锁定面部跑图
【AI绘画】ControlNet 之 Reference only 锁定面部跑图
1969 0
|
9月前
|
存储 弹性计算 运维
阿里云服务器2核4G、4核8G、8核16G租用价格:2026最新收费标准活动价格参考
2026年阿里云为2核4G、4核8G、8核16G三种配置提供了多样化实例规格与特惠活动。2核4G配置轻量应用服务器和云服务器特价199元/年,适合轻量应用;4核8G配置u2i实例1252.63元/年起,满足中型应用需求;8核16G配置活动价5958.52元/年起,适合高性能计算。用户可根据性能、预算和业务特性选择实例,并通过领取优惠券进一步降低成本。阿里云提供从“普惠级”到“企业级”的丰富选择,满足不同业务需求。
1612 16
|
9月前
|
存储 人工智能 缓存
极速、稳定、丝滑:OpenClaw 接入 Mooncake 后的性能跃迁
OpenClaw 不只是更快了,更重要的是更稳了。
|
人工智能 Kubernetes Cloud Native
MSE Nacos Controller:为 Kubernetes 生态构建配置管理与服务发现的桥梁
在企业云原生转型过程中,如何实现传统微服务与 Kubernetes 服务的配置统一管理、服务互通及协议转换成为关键挑战。MSE Nacos Controller 应运而生,作为连接 Kubernetes 与 Nacos 的桥梁,支持 ConfigMap 与 Nacos 配置双向同步、服务自动注册发现,并助力 Higress 等 MCP 网关实现 REST API 向 AI 可调用 MCP 服务的转换,全面提升系统治理能力与智能化水平。
1360 32
|
存储 机器学习/深度学习 缓存
阿里云九代云服务器怎么样?计算型c9i、通用型g9i、内存型r9i实例介绍
阿里云第9代云服务器主要实例规格包括计算型c9i、通用型g9i、内存型r9i,本文将为大家介绍阿里云九代云服务器中的计算型c9i、通用型g9i、内存型r9i实例的主要性能特点,并分享最新的活动价格信息,以供参考。
1184 1
|
存储 SQL 数据库
OceanBase存储过程基本使用
OceanBase存储过程基本使用
|
机器学习/深度学习 运维 安全
MyEMS开源能源管理系统简介
MyEMS 是一款基于 Python 和 React 的开源能源管理系统,采用 MIT 协议发布,适用于建筑、工厂、商场等多种场景。系统支持电、水、气等能源数据实时采集,兼容新能源设备接入,并提供智能分析、设备管理、节能优化等功能,助力企业降低能耗与运维成本,提升能源使用效率。
442 0
|
Web App开发 缓存 编解码
FFmpeg开发笔记(三十八)APP如何访问SRS推流的RTMP直播地址
《FFmpeg开发实战》书中介绍了轻量级流媒体服务器MediaMTX,适合测试RTSP/RTMP协议,但不适用于复杂直播场景。SRS是一款强大的开源流媒体服务器,支持多种协议,起初为RTMP,现扩展至HLS、SRT等。在FFmpeg 6.1之前,推送给SRS的HEVC流不受支持。要播放RTMP流,Android应用可使用ExoPlayer,需在`build.gradle`导入ExoPlayer及RTMP扩展,并根据URL类型创建MediaSource。若SRS播放黑屏,需在配置文件中开启`gop_cache`以缓存关键帧。
1482 2
FFmpeg开发笔记(三十八)APP如何访问SRS推流的RTMP直播地址
|
NoSQL Unix Shell
gdb调试命令的使用及总结
来源:http://www.jb51.net/article/36393.htm 参考:http://www.cnblogs.com/hankers/archive/2012/12/07/2806836.html 参考:http://wiki.ubuntu.org.cn/%E7%94%A8GDB%E8%B0%83%E8%AF%95%E7%A8%8B%E5%BA%8F 100个gdb技
9905 0