zoukankan      html  css  js  c++  java
  • 在react中使用原生的高德地图

    1、使用react-create-app创建一个新的react项目

    2、修改index.html,添加以下script引用:

    3、创建一个组件文件MapDemo.js,内容如下

    import React, { Component } from "react";
    
    class WebMapDemo extends Component {
        constructor(props) {
            super(props);
        }
    
        componentDidMount(){
            var map = new AMap.Map('container', {
                viewMode: '3D',
                pitch: 50,
                zoom: 11,
                center: [116.480766, 39.932931]
            });
        
            // 设置光照
            map.AmbientLight = new AMap.Lights.AmbientLight([1, 1, 1], 0.5);
            map.DirectionLight = new AMap.Lights.DirectionLight([0, 0, 1], [1, 1, 1], 1);
        
            var object3Dlayer = new AMap.Object3DLayer();
            map.add(object3Dlayer);
        
            new AMap.DistrictSearch({
                subdistrict: 0,   //返回下一级行政区
                extensions: 'all',  //返回行政区边界坐标组等具体信息
                level: 'city'  //查询行政级别为 市
            }).search('朝阳区', function (status, result) {
                var bounds = result.districtList[0].boundaries;
                var height = 5000;
                var color = '#0088ffcc'; // rgba
                var prism = new AMap.Object3D.Prism({
                    path: bounds,
                    height: height,
                    color: color
                });
        
                prism.transparent = true;
                object3Dlayer.add(prism);
        
                var text = new AMap.Text({
                    text: result.districtList[0].name + '</br>(' + result.districtList[0].adcode + ')',
                    verticalAlign: 'bottom',
                    position: [116.528261, 39.934313],
                    height: 5000,
                    style: {
                        'background-color': 'transparent',
                        '-webkit-text-stroke': 'red',
                        '-webkit-text-stroke-width': '0.5px',
                        'text-align': 'center',
                        'border': 'none',
                        'color': 'white',
                        'font-size': '24px',
                        'font-weight': 600
                    }
                });
        
                text.setMap(map);
            });
        }
    
        render() {
            return (
                <div id="container" style ={{"100%",height:"95%"}}>                
                </div>
            );
        }
    
    }
    
    export default WebMapDemo;
    

    注意AMap类不是react中定义的类,会在运行时报错,因此需要参考我的另一篇随笔 《配合react-amap使用原生高德地图》, 将AMap类排除在eslint语法检查之外。

    4、编写index.js,加载在3里编写的这个地图组件

    import React from "react";
    import ReactDOM from "react-dom";
    import Map from "./MapDemo";
    
    ReactDOM.render(
        <div style ={{"100%",height:"100%"}}>    
            <Map />
        </div>,
    
        document.getElementById("root")
    );
    

      

    5、执行 npm start运行项目:

    完工。

    6 说明

    为了讲清逻辑, 地图的创建和操作都写在了componentDidMount里面。各位自行进行性能优化。

  • 相关阅读:
    四则运算二
    学习进度
    软件工程个人作业01
    观《构建之法》有感
    软件工程概论课程引言课后作业
    编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。
    软件需求与分析课堂讨论一
    软件需求模式阅读笔记一
    我们应当怎么做需求分析
    问题账户需求分析
  • 原文地址:https://www.cnblogs.com/zjw0901/p/11104292.html
Copyright © 2011-2022 走看看