Code Monkey home page Code Monkey logo

react-amap's Introduction

react-amap

Build Status npm version npm downloads

react-amap 是一个基于 React 封装的高德地图组件;帮助你轻松的接入地图到 React 项目中。除了必须引用的 Map 组件外,我们目前提供了最常用的 10 个地图组件,能满足大部分简单的业务场景;如果你有更复杂的需求,或者觉得默认提供的组件功能不够,你完全可以自定义一个地图组件,然后根据高德原生 API 做高德允许你做的一切事情。

最近更新

react-amap 升级到 1.1.1,支持加载 AMapUI 组件库。加载方式参考 Map 组件 关于 加载 AMapUI 组件库 的说明。(2017-11-13)

扩展组件

从版本 1.0.0 开始,react-amap 拥有了组件的扩展能力。如果 react-amap 中已有的组件不能满足你的业务需求,你可以使用自己写的地图组件;

在你的组件中,可以通过 props 访问到创建好的高德地图实例,以及地图的 div 容器;拥有访问这两个属性的能力后,你可以根据高德原生 API 做高德允许你做的一切事情。实际上,react-amap 中的其他组件就是这么做的。文档内容请参考自定义地图组件

目前已有的一些组件:

  1. react-amap-plugin-heatmap,热力图组件。
  2. react-amap-plugin-geolocation,定位组件。
  3. react-amapui-wrapper,可自定义的AMapUI组件。

如果你有写好的组件愿意开源出来的,欢迎提一个 PR 扩展这个列表。

在线实时演示

https://jsfiddle.net/ioslh/h4u8mdng/25/


如何在项目中接入 react-amap;

安装

npm install --save react-amap

npm 用法

<div id="app"></div>
#app {
  width: 600px;
  height: 400px;
}
import React from 'react';
import ReactDOM from 'react-dom';
import { Map } from 'react-amap';

ReactDOM.render(
  <Map amapkey={YOUR_AMAP_KEY} version={VERSION} />,
  document.querySelector('#app')
)

amapkey 说明见下文 version 指定高德地图版本 不填则使用默认值: 1.4.0

也可以手工引入你需要的组件:

import Map from 'react-amap/lib/map';
import Marker from 'react-amap/lib/marker';
// ... your other code

以上为简单场景的应用。 tips: Map 组件的父元素须设置高度和宽度;关于代码中的 Map 组件的 amapkey 属性见下方的说明。

CDN 用法

在 HTML 页面中加入 react-amap 库的 CDN 地址,插件会在 window 下暴露 ReactAMAP 变量。

<script src="path/to/react.js"></script>
<script src="path/to/react-dom.js"></script>
<script src="path/to/dist/react-amap.js"></script>
<script>
  var Map = ReactAMAP.Map;
  var Marker = ReactAMAP.Marker;
  
  var pos = { longitude: 120, latitude: 30 };
  var YourApp = React.createElement(
    Map,
    { center: pos },
    React.createElement(
      Marker,
      { position: pos },
      null
    )
  );
  ReactDOM.render(YourApp, document.getElementById('root'));
</script>
CDN 地址

实际应用中你可以使用下面的 CDN 地址,也可以把脚本下载下来本地部署。

tips: 记得将其中的 VERSION 替换为真实版本号,查看历史版本更新日志

关于 Key

在上面的例子中需要给 Map 组件传入 amapkey 属性,这个是高德地图给开发者分配的开发者 Key;你可以在高德开放平台申请你自己的 Key。

在 react-amap 中 Key 的传入方式有两种:

  • 给 Map 组件传入 amapkey 属性(因为 React 框架本身对 key 属性有其他作用,所以不能用 key,所以我们用 amapkey),这样的缺点是如果多个地方使用就要每次都要传入;
  • 你也可以定义一个纯组件,把 Map 组件的 amapkey 属性写好后返回新组件。
  • 直接把你的 Key 定义在全局变量 window.amapkey 上,react-amap 在调用高德接口时会尝试从这里读取。(不推荐)

组件的使用请移步组件文档

贡献指南

首先感谢你使用 react-amap,react-amap 是一个基于 React 封装的高德地图组件库。

react-amap 的成长离不开大家的支持,如果你愿意为 react-amap 贡献代码或提供建议,请阅读以下内容。

开发

npm install
npm start # http://localhost:9001

Issue 规范

  • issue 仅用于提交 Bug 或 Feature,其它内容可能会被直接关闭。
  • 在提交 issue 之前,请搜索相关内容是否已被提出。
  • 在提交issue时,请说明 react-amap 的版本号,并提供操作系统和浏览器信息。推荐使用 JSFiddle 生成在线 demo,这能够更直观地重现问题。

Pull Request 规范

  • 请先 fork 一份到自己的项目下,不要直接在仓库下建分支。

  • 不要提交 dist 里面打包的文件

  • 提交 PR 前请 rebase,确保 commit 记录的整洁。

  • 如果是修复 bug,请在 PR 中给出描述信息。

  • 合并代码需要两名维护人员参与:一人进行 review 后 approve,另一人再次 review,通过后即可合并。

代码规范

遵循饿了么前端的 ESLint 即可。

License

MIT License

Copyright (c) 2017 ElemeFE

react-amap's People

Contributors

f48vj avatar ioslh avatar jocs avatar joshua7v avatar sam019 avatar zhengqingxin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-amap's Issues

请问能否增加typescript支持??

在基于typescript的项目中使用react-amap,会提示找不到模块的声明文件,可能需要一个d.ts文件

error TS7016: Could not find a declaration file for module 'react-amap'. 'xxxnode_modules/react-amap/lib/index.js' implicitly has an 'any' type.
  Try `npm install @types/react-amap` if it exists or add a new declaration (.d.ts) file containing `declare module 'react-amap';`

npm上目前也没用@types/react-amap可以用

markers的useCluster属性设置为MarkerClustererOptions时,其中renderCluserMarker属性不生效

What is Expected?

在Markers的useCluster属性中,试图利用MarkerClustererOptions的renderCluserMarker属性,实现自定义点聚合时候的样式;
https://elemefe.github.io/react-amap/components/markers

What is actually happening?

MarkerClustererOptions中别的参数能生效,但是renderCluserMarker属性没有产生效果,使得目前无法实现 点聚合 时候的自定义样式;

有没有导航插件

有没有导航功能啊,比如传入起始坐标,终点坐标,交通方式等参数后,生成路线?

PolyEditor 无法切换编辑

RT
初始值设置为true

<PolyEditor active={true} ... />

然后更改active的属性,发现编辑功能依然可用

如何通过antd的行政区域联动修改默认的city?

因为采用了阿里的dva脚手架,所以状态统一在model内管理了,city变化时候,但地图无变化,应该监听什么事件或者有什么处理方式?由于本人不是专业前端,只是一个后台开发者,描述不清楚的话,见笑了。
image
image

请问如何调用高德官方的 MarkerClusterer插件自定义聚合坐标样式?

react-amap 的plugins提供的5种插件没有markClusterer,还有其他方法调用高德的这个插件吗,试过直接调用高德的api,但是如何获取高德的AMap对象?通过 events.created获取的是实例。

react-amap version

v1.1.2

OS/Browers version

chrome 53

Reproduction Link

Steps to reproduce

What is Expected?

What is actually happening?

AMap.CloudDataLayer 如何使用?

react-amap version

1.0.3

请教一下 AMap.CloudDataLayer 现在有方案能够使用么 ,浏览到#23
主要问题是 不知道在哪可以得到 AMap

plugin 的 toolbar 似乎有点问题

react-amap version

0.2.7

Edit fiddle - JSFiddle只加了

         AMap.plugin('AMap.ToolBar',() => {
          var tool = new AMap.ToolBar(); 
          _this.map.addControl(tool);
          _this.toolbar = tool;
         })

2017-05-22 7 34 00
显示的是没有问题的。

但是在自己的工程中,就有点问题了

      {
        name   : 'ToolBar',
        options: {
          visible : true,  // 不设置该属性默认就是 true
          ruler   : true,
          onCreated(ins){
            //console.log(ins);
          },
        },
      }

ruler显示不出来(而且应该默认就是true),用onCreated把toolbar取出来,然后做类似
this.mapToolbar.showRuler()
仍然显示不了。
见其中第二个图: React AMap — 基于 React 的高德地图组件
2017-05-22 7 37 51

另外,toolbar的默认位置变成了右下,也不是官方的左上。

jsfiddle上的react-amap是0.2.6版本,和这个有关系吗?

点聚合问题

请问,组件Markers 可以在点击聚合点时不弹窗,而是显示其下级Marker吗?谢谢

Map: `key` is not a prop.

地图是渲染出来了,但是浏览器报如下错误:

Warning: Map: `key` is not a prop. 

Trying to access it will result in `undefined` being returned.

 If you need to access the same value within the child component, you should pass it as a different prop.

我的代码如下(key-在这里我随便写了):

import React, { Component } from 'react';
import { Map } from 'react-amap';

class App extends Component {
  render() {
    return (
        <div style={{width: '600px',height: '500px'}}>
          <Map key={"dsafs34132424"} />        
        </div>
    );
  }
}
export default App;

bisheng 使用疑问

您好 我正在用您的代码学习bisheng 有个问题想请教您

image

这个里面的 highlightedCode 和 preview 是如何注入到 pageData 中的啊?查阅了相关文档和代码都没有找到相关的内容

bisheng版本:0.24.1

谢谢

【建议】删掉库中定义的地图内层div背景色

【使用场景】使用高德的自定义地图时,即mapStyle属性设置为类似amap://styles/9104b475b707060c9a89f69e10db86f4的值。如果地图背景颜色与库中定义的内层div背景色相差过大,会有一种闪的很明显的感觉。

【建议】建议将库中设置的背景色删除,或者设置为transparent。让用户设置背景颜色。类似

<div style={{backgroundColor: 'orange'}}>
  <Map></Map>
</div>

也可以给Map自定一个背景颜色属性。

【位置】库中使用的地图内层div背景色是#eee, 位置在react-amap->lib->map->index.js 452:71

Expect react-native-amap

react-amap version

OS/Browers version

Reproduction Link

Steps to reproduce

What is Expected?

What is actually happening?

使用react-map显示无法找到文件

react-amap version 1.0.3
OS/Browers version centos 7.4 版本 61.0.3163.100(正式版本) (64 位)
使用例子如下:
import { Map } from 'react-amap';
import React from 'react';
class ReactAmapShow extends React.Component{
render(){
return <div style={{width: '100%', height: '400px'}}>
<Map amapkey={'22fd6e60f6b3ca19cd2dd588b167647e'}/>

}
}

export default ReactAmapShow;
在浏览器中打开,报错如下:
bundle.js:123738 GET file://webapi.amap.com/maps?v=1.3&key=******&callback=__amap_init_callback net::ERR_FILE_NOT_FOUND
在谷歌上搜索了一下,是需要自己引入高德的js文件吗?

调用高德手机客户端

您好,我不知道怎么在咱们的插件中使用调用高德客户端的方法,您能指导一下么

怎么使用高德地图的自定义地图

react-amap version

1.0.3

OS/Browers version

Chrome 60.0.3112.113

Reproduction Link

Steps to reproduce

What is Expected?

在Map组件中使用自定义组件。

What is actually happening?

ps:文档中已表明了目前支持的样式,但是没有自定义样式。然后,使用events的created(map),然后在里面map.setMapStyle并没有生效。

window is not defined

when i use this commpont, I get a error, it's :
window is not defined

this is my code:

import React from 'react';
import { Map } from 'react-amap';


export default function MapView() {
  return (
    <div style={{ width: '600px', height: '500px' }}>
      <Map amapkey={'xxxx'} />
    </div>
  );
}

I hope you can reply to me、 Thank you.

map click event对象问题

热力图Heatmap的dateSet属性
数据源data假设是:[{"lng":120.67185,"lat":31.283164,"count":4.0,"name":"如何能拿到这个name值?"},{"lng":116.385502,"lat":39.844509,"count":1.0,"name":"如何能拿到这个name值?"},{"lng":120.714894,"lat":31.318038,"count":1.0,"name":"如何能拿到这个name值?"}]

点击热力图区域时的回调函数:
mapClickCallback(e){
let lng = e.lnglat.lng;
let lat = e.lnglat.lat;
console.log(e);
}

如何在e对象中获取数据源中的name值?

PropTypes

Several of the dependencies for this project are outdated, making this module annoying to use for React 15+ versions.

From what I can detect this are the modules:

react-router imported: 3.0.2 latest: 4.2.0
isomorphic-style-loader version imported: 1.1.0 latest: 4.0.0

should use PropTypes from 'prop-types' and not from 'react' package.

useCluster无法使用

react-amap version

0.2.7

OS/Browers version

macOS/Safari

Reproduction Link

Steps to reproduce

class AMapboxViz extends React.Component {

  constructor(props){
    super(props);

    const markers = this.props.locations;
    this.state={
      useCluster: true,
    };

    this.state = {
      markers
    };
  }

  render(){
    return (
      <Map
        amapkey={'ba035e10b09acfc492d168adfd1b97cb'}
        center={{longitude: 110, latitude: 40}}
        zoom={5}
      >
        <Markers
          markers={this.state.markers}
          useCluster={this.state.useCluster}
        />
      </Map>
    );
  }
}
function amapbox(slice, json){
  const div = d3.select(slice.selector);
  div.selectAll('*').remove();
  ReactDOM.render(
    <AMapboxViz
      {...json.data}
    />,
    div.node(),
  );
}

What is Expected?

显示的地图的marker会cluster成圆圈。

What is actually happening?

仅显示了markers,并没有cluster起来。

请问如何加载插件?

你好,我打算实现地理编码的功能,看高德的文档,说是要引入AMap.Geocoder插件。请问一下,有没有引入插件的方法?

通过events.created方法获得原始的高德实例,使用高德的API,没有生效

我想在地图中再添加热力图,发现没有生效,
参照高德官网代码热力图
打印发现热力图的代码没有执行,是我的写法有问题么?
render是这样写的

return (
      <div style={{ paddingTop: 30, display: 'flex'}} >
        <div style={{ width: '60%', height: 800, margin: 'auto' }}>
          <Map zoom={12} plugins={["ToolBar"]} events={this.mapEvents}>
              {/*{this.props.locations}*/}
          </Map>
        </div>
      </div>

mapEvents是这样的

this.amapEvents = {
      created(map) {
        _this.map = map;
        var points = [
          { "lng": 116.191031, "lat": 39.988585, "count": 10 },
          { "lng": 116.389275, "lat": 39.925818, "count": 11 },
          { "lng": 116.287444, "lat": 39.810742, "count": 12 },
          { "lng": 116.481707, "lat": 39.940089, "count": 13 },
          { "lng": 116.410588, "lat": 39.880172, "count": 14 },
          { "lng": 116.394816, "lat": 39.91181, "count": 15 },
          { "lng": 116.416002, "lat": 39.952917, "count": 16 }
        ];
        console.log('2');
        map.plugin(["AMap.Heatmap"], () => {
          _this.heatmap = new AMap.Heatmap(map);
          console.log('222')

          _this.heatmap.setDataSet({
            data: points,
            max: 100
          });
          console.log('333', _this.heatmap);
          _this.heatmap.show();
        })
        console.log('3', _this.heatmap);
      }
    };

如何创建一个自定义的组建?

const ComponentList = [
  Circle,
  GroundImage,
  InfoWindow,
  Markers,
  Marker,
  Polyline,
  Polygon,
  MouseTool
];
// ...
renderChildren() {
    return Children.map(this.props.children, (child) => {
      if (child) {
        if (ComponentList.indexOf(child.type) === -1) {
          return child;
        }
        return React.cloneElement(child, {
          __map__: this.map,
          __ele__: this.mapWrapper
        });
      }
      return child;
    });
  }

看源码中似乎规定写死了这几种组件,那如何自定义/扩充一个组件?

如何修改源码使其支持AMapUI组件库(因为需要轨迹展示)(自己尝试修改,但未成功)

版本 1.0.1
macOS/Chrome 53

我看了没有提供 UI组件库API (没有引用);我想尝试修改源码,增加创建一个script元素
<script src="//webapi.amap.com/ui/1.0/main.js?v=1.0.11"></script>
因此我做了一下尝试:
一、直接在index.html文件上增加以下标签,原生增加
`<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.0&key="></script>

<script src="//webapi.amap.com/ui/1.0/main.js?v=1.0.11"></script>`

AMapUi可以正常使用,轨迹展示成功运行,但是react-amap提供的能力全部失效,应该是变量被覆盖了

二、深入react-amap源码,寻找创建script元素的地方,增加一个方法 创建引入ui库的script
源码位置

新增如下:
var script = document.createElement('script'); script.type = 'text/javascript'; script.async = true; script.defer = true; script.src = this.getScriptSrc(_config); //创建引入 amapui api (新增) const amapui = () => { var script = document.createElement('script'); script.type = 'text/javascript'; script.src = "//webapi.amap.com/ui/1.0/main.js?v=1.0.11"; console.log(script); } var scriptLoadingPromise = new Promise(function (resolve, reject) { window[_config.callback] = function () { queuedLoader.forEach(function (event) { return event(); }); //amap成功创建后,创建amapui的 console.log(script); amapui(); resolve(); }; script.onerror = function (error) { return reject(error); }; }); document.head.appendChild(script); __script_loaded = true; return scriptLoadingPromise; }

结果:react-amap正常使用,AMapUI依然没有定义(但是我已经创建了啊)
e0fcf545-d5a3-48ef-940d-9e1ae6119527

我看的你vue 相关的创建方式
vue创建amapui的方式

需求:请问我应该如何创建引入AMapUi才能正常使用?

我已经找到问题了,低级错误,创建了script DOM元素,却没有插入页面
修改如下:
const amapui = () => { var script = document.createElement('script'); script.type = 'text/javascript'; script.src = "//webapi.amap.com/ui/1.0/main.js?v=1.0.11"; document.head.appendChild(script) //插入页面 console.log(script); }
存在的问题:AMapUi不在受react生命周期控制,无法时时更新数据
期望:希望维护者提供更加优雅的方式,谢谢

关于参数一致性的建议

hi,

地图中心:

      <Map 
        plugins={['ToolBar']} 
        center={{longitude: 120, latitude: 35}} 
      >

但高德官方 [地图显示-地图显示-示例中心-JavaScript API | 高德地图API](http://lbs.amap.com/api/javascript-api/example/map/map-show)是:

        center: [116.397428, 39.90923]

虽然用起来没有问题,但估计有的人还是会有困扰吧....

个人建议仅供参考

Polyline 组件数组 重绘时未完全清除上一次绘制的线条

react-amap version

  • react-amap 1.0.2

OS/Browers version

  • macOS Sierra 10.12.5
  • Chrome 版本 60.0.3112.90(正式版本) (64 位)

Reproduction Link

  • 因为一些原因不方便贴链接,直接放代码和截图好了

Steps to reproduce

  • 同上

Code

export default class MapArea extends Component {
  constructor(){
    super();

    this.mapPlugins = ['ToolBar'];
    this.mapEvents = {
      click: (e) => {
        this.props.changePosition(e.lnglat);
      }
    }
  }

  render(){
    const polylineArr = this.props.roads.map((item, index) => {
      return(
        <Polyline
          key={`polyline${index}`}
          path={[{
              longitude:item.coordinates[0][0],
              latitude: item.coordinates[0][1],
            }, {
              longitude:item.coordinates[1][0],
              latitude: item.coordinates[1][1],
            }]}
          style={{strokeColor: '#f00', strokeOpacity: 0.5+0.5*Number.parseFloat(item.probability), strokeWeight: 7}}
        />
      )
    });

    return (
      <div>
        <div style={style.mapArea}>
          <Map plugins={this.mapPlugins} center={this.props.position} events={this.mapEvents} zoom={17}>
            <Marker
              position={this.props.position}
            />
            {polylineArr}
          </Map>
        </div>
      </div>
    )
  }
}

What is Expected?

  • 一个 react-amap,在上面画一些线条
  • 上一轮查询 this.props.roads.length = 16,画出 16 条线
  • 本轮查询 this.props.roads.length = 1,画出 1 条线,同时清除掉上一轮的 16 条线。

What is actually happening?

  • 上一轮的 16 条线只有第 1 条被清除且重绘了,其他 15 条没有被清除,仍在地图上。

Screenshot

上一轮查询,ui + 控制台

2017-08-13 4 24 50

本轮查询,ui + 控制台

2017-08-13 4 25 10

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.