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 做高德允许你做的一切事情。

最近更新

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

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

目前已有的一些组件:

  1. react-amap-plugin-heatmap,热力图组件。

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

在线实时演示

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


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

安装

npm install --save react-amap

npm 用法

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

class MapApp extends React.Component {
  // ... Your other methods
  render() {
    return <div style={{ width: '600px', height: '500px' }}>
      <Map amapkey={YOUR_AMAP_KEY} />
    </div>
  }
}

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

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's People

Contributors

ioslh avatar f48vj avatar

Watchers

James Cloos avatar

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.