干货,无话

1、react-create-app,创建新react项目;

2、npm install react-amap,引入高德地图的封装;

3、编写组件index.js:

import React from "react";
import ReactDOM from "react-dom";
import Map from "./Map3"; let mapData = {
city: "北京",
mapCenter:[116.418261, 39.921984], //城市定位,经纬度定位只能选择1个
mapZoom: 10, //地图缩放
mapKey: '12345678d98aff1166e51962f108bb24', //你的高德key
status: { //是否支持放大拖拽
zoomEnable: true,
dragEnable: true,
},
mapMaker :[ //marker标记点(list)
{lnglat:[116.401728,39.911984],text:'要显示的内容1'},
{lnglat:[116.436691,39.921984],text:'要显示的内容2'}
],
plugins:['ToolBar']
}; ReactDOM.render(
<div style ={{width:"100%",height:"100%"}}>
<Map title="map" mapData={mapData}/>
</div>, document.getElementById("root")
);

  注意render方法内引用了Map组件,因此要编写一个Map3.js,提供这个组件

4、编写Map3.js组件

import React, { Component } from "react";
import { Map, Marker } from 'react-amap';
import ZoomCtrl from './zoom'; class WebMap3 extends Component {
constructor(props) {
super(props);
this.data = props;
//地图事件
this.amapEvents = {
created: (mapInstance) => {
var marker = new AMap.Marker({
// 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
position: new AMap.LngLat(116.39, 39.9),
title: '北京!!'
}); mapInstance.add(marker);
}
}; //点位事件
this.markerEvents = {
click: (markerInstance) => {
this.Position = [markerInstance.lnglat.lng,markerInstance.lnglat.lat];
this.setState({
isShow: true,
});
}
};
} render() {
let {city, mapCenter, mapZoom, mapKey, status, plugins} = this.data.mapData;
return (
<div style ={{width:"100%",height:"95%"}}>
<Map amapkey={mapKey} city={city} zoom={mapZoom} center={mapCenter} status={status} plugins={plugins} events={this.amapEvents}>
{this.data.mapData.mapMaker.map((comment) => (
<Marker position={comment.lnglat} events={this.markerEvents}>
</Marker>
))}
<ZoomCtrl />
</Map>
</div>
);
} } export default WebMap3;

  注意标红部分,会报错

这个是关键! 有两个办法解决,分别见下面的5.1和5.2

5、解决react下找不到原生高德地图AMap类的问题

5.1 方法1

暴力手段,直接搞定。

使用注释    //eslint-disable-next-line  写在每个出现AMap类的前面一行,如下所示

原理是告诉eslint:注释下面这一行您别管。

5.2 方法2

强迫症手段,分为3步。

5.1.1 在项目根目录下新加.eslintrc.js文件,把AMap变量放到globals集合里面

module.exports = {
"env": {
"browser": true,
"es6": true
},
// 脚本在执行期间访问的额外的全局变量
// 当访问未定义的变量时,no-undef 规则将发出警告。
// 如果你想在一个文件里使用全局变量,推荐你定义这些全局变量,这样 ESLint 就不会发出警告了。
// 你可以使用注释或在配置文件中定义全局变量
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly",
"AMap":true,
"window":true,
"document":true,
},
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": [
"react"
],
"rules": {
"semi": ["error","always"],
}
};  

注意红色部分代码表示:AMap是个全局变量,是webpack我罩着的,保证能用,eslint你别管。

当然,webpack.config.js要做点修改,以支持我们刚写的.eslintrc.js文件。可是react-create-app生成的项目的webpack.config.js不好找啊,也能找到:

5.2.2 修改 node_modules\react-scripts\config\webpack.config.js文件

在这个文件搜索字符串 useEslintrc, 大概在webpack.config.js文件的第326行,把 useEslintrc: false,  改成 useEslintrc: true, 然后保存。如下所示:

5.2.3 完工

6 验收

在控制台运行npm start,然后访问http://localhost:3000,出现下图表示OK!

7 总结

此方法适用于在react中调用jquery、百度地图、高德地图、OpenLayer、echart等等使用ES5编写的各类控件库。

最新文章

  1. C# ObjectCache、OutputCache缓存
  2. 【转】 Linux chmod命令
  3. responseXML 属性
  4. fmri分析工具:spm里的统计学 Introduction to SPM statistics
  5. POJ2676Sudoku
  6. 问题-某个程序改了ICO图标后编译后还是显示老图标?
  7. Codeforces Round #335 (Div. 1)--C. Freelancer&#39;s Dreams 线性规划对偶问题+三分
  8. 2015 UESTC Winter Training #10【Northeastern Europe 2009】
  9. 使用top工具,找出消耗CPU 较多的进程
  10. iOS中如何使状态栏与下面的搜索栏或NavigationBar或toolBar颜色一致
  11. Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent
  12. js写的复制功能,只支持IE
  13. BZOJ 1083: [SCOI2005]繁忙的都市(MST)
  14. StrPos,StrScan,
  15. Bootstrap学习-排版
  16. An Introduction to Stock Market Data Analysis with R (Part 1)
  17. 1000以内完全数(完美数)获取实现---基于python
  18. 【java】io流之字符输出流:java.io.Writer类及子类的子类java.io.FileWriter
  19. NVIDIA Titan Xp Star Wars Collector&#39;s Edition显卡深度学习工作站 + Ubuntu17.10 + Tensorflow-gpu + Anaconda3 + Python 3.6 设置
  20. Learning-Python【30】:基于UDP协议通信的套接字

热门文章

  1. Delphi 中的字符串(还解释了shortstring)good
  2. dp_Pku1887
  3. Maven软件项目管理工具
  4. i/o多路复用笔记
  5. Node.js &amp;&amp; Angular &amp;&amp; TypeScript 环境安装与更新
  6. debian kill 进程等命令
  7. 在 win10 环境下,设置自己写的 程序 开机自动 启动的方法
  8. C++ Boost库简介(一些自己的感受)
  9. install windows service
  10. 字符串、数组操作函数 Copy Concat Delete Insert High MidStr Pos SetLength StrPCopy TrimLeft