antv-l7/docs/tutorial/quickstart.zh.md

177 lines
3.3 KiB
Markdown
Raw Normal View History

2019-11-21 13:06:13 +08:00
---
title: 快速上手
order: 0
redirect_from:
- /zh/docs/tutorial
---
# L7
2019-11-21 23:27:01 +08:00
2020-01-06 18:02:02 +08:00
Current version: ![L7 2.0版本号](https://badgen.net/npm/v/@antv/l7)
2019-11-21 13:06:13 +08:00
## 使用方法
2019-11-21 13:06:13 +08:00
### 通过 L7 CDN 使用
Include the L7 JS JavaScript <head> of your HTML file.
2019-11-21 13:06:13 +08:00
⚠️ 如果需要引用第三方地图API请确保在先引入第三方API然后引入L7
2019-11-21 13:06:13 +08:00
```html
<head>
<! --引入第三方地图JSAPI-->
2020-01-21 17:45:24 +08:00
<script src='https://gw.alipayobjects.com/os/antv/pkg/_antv.l7-2.0.8/dist/l7.js'>
</script>
2019-11-21 13:06:13 +08:00
</head>
```
2019-11-28 23:38:18 +08:00
- [use Gaode Map](./map/amap)
2019-11-21 13:06:13 +08:00
2019-11-28 23:38:18 +08:00
- [use Mapbox Map ](./map/mapbox)
2019-11-21 13:06:13 +08:00
## 通过 Module bundle 使用
2019-11-21 13:06:13 +08:00
Install the npm package.
2019-11-21 13:06:13 +08:00
```bash
2019-11-21 13:06:13 +08:00
2019-11-28 17:49:04 +08:00
// L7 依赖
2020-01-07 21:15:23 +08:00
npm install --save @antv/l7
2019-11-21 13:06:13 +08:00
2019-11-28 17:49:04 +08:00
// 第三方底图依赖
npm install --save @antv/l7-maps;
2019-11-21 13:06:13 +08:00
```
2019-11-28 17:49:04 +08:00
2019-11-28 17:49:04 +08:00
### 初始化地图
2019-11-28 23:38:18 +08:00
#### 使用 高德 底图
2019-11-28 17:49:04 +08:00
```javascript
import { Scene, PointLayer } from '@antv/l7';
import { GaodeMap } from '@antv/l7-maps';
const scene = new Scene({
id: 'map',
map: new GaodeMap({
pitch: 35.210526315789465,
style: 'dark',
center: [ 104.288144, 31.239692 ],
zoom: 4.4
})
});
```
#### 使用Mapbox 底图
```javascript
import { Scene, HeatmapLayer } from '@antv/l7';
import { Mapbox } from '@antv/l7-maps';
const scene = new Scene({
id: 'map',
map: new Mapbox({
style: 'dark',
pitch: 0,
center: [ 127.5671666579043, 7.445038892195569 ],
zoom: 2.632456779444394
})
});
```
### React中使用
React 组件待开发期待和大家共建l7-react 目前可以暂时以 Submodule 方式使用
```
import { Scene, PolygonLayer } from '@antv/l7';
import { AMap } from '@antv/l7-maps';
import * as React from 'react';
export default class AMapExample extends React.Component {
private scene: Scene;
public componentWillUnmount() {
this.scene.destroy();
}
public async componentDidMount() {
const response = await fetch(
'https://gw.alipayobjects.com/os/basement_prod/d2e0e930-fd44-4fca-8872-c1037b0fee7b.json',
);
const scene = new Scene({
id: 'map',
map: new AMap({
center: [110.19382669582967, 50.258134],
pitch: 0,
style: 'dark',
zoom: 3,
token: 'pg.xxx', // 高德或者 Mapbox 的 token
}),
});
const layer = new PolygonLayer({});
layer
.source(await response.json())
.size('name', [0, 10000, 50000, 30000, 100000])
.color('name', [
'#2E8AE6',
'#69D1AB',
'#DAF291',
'#FFD591',
'#FF7A45',
'#CF1D49',
])
.shape('fill')
.style({
opacity: 0.8,
});
scene.addLayer(layer);
this.scene = scene;
}
public render() {
return (
<div
id="map"
style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
}}
/>
);
}
}
```
⚠️组件 Unmount 时需要通过 scene.destroy() 手动销毁场景。
更多React使用 [示例查看](https://github.com/antvis/L7/tree/master/stories)
2020-01-06 18:02:02 +08:00
## 不同项目使用模板
不同项目模板在CodeSandbox你可以预览也可以下载到本地
### React
[地址](https://codesandbox.io/s/l720react-jfwyz)
### Vue
[地址](https://codesandbox.io/s/l720vue-uef1x)
### Angular
[地址](https://codesandbox.io/s/angulartest-chpff)
### HTML CDN
[地址](https://codesandbox.io/s/l7cdndemo-gfg9m)