|
@@ -7,12 +7,16 @@ import { loadMap } from './api';
|
|
|
import Map from './Map';
|
|
|
import Marker from './Marker';
|
|
|
import MassMarks from './MassMarks';
|
|
|
+import Polygon from './Polygon';
|
|
|
+import Polyline from './Polyline';
|
|
|
|
|
|
class MarkerTest extends Component {
|
|
|
constructor() {
|
|
|
super();
|
|
|
this.state = {};
|
|
|
this._mapDblclick = this._mapDblclick.bind(this);
|
|
|
+ this._carMoving = this._carMoving.bind(this);
|
|
|
+ this._logging = this._logging.bind(this);
|
|
|
}
|
|
|
|
|
|
componentDidMount() {
|
|
@@ -23,13 +27,34 @@ class MarkerTest extends Component {
|
|
|
autoRefresh: true, //是否自动刷新,默认为false
|
|
|
interval: 5 //刷新间隔,默认180s
|
|
|
});
|
|
|
- this.setState({ AMap, layers: [roadNet, traffic] });
|
|
|
+
|
|
|
+ let lineArr = [];
|
|
|
+ let lngX = 116.397428, latY = 39.90923;
|
|
|
+ lineArr.push(new AMap.LngLat(lngX, latY));
|
|
|
+ for (let i = 1; i < 4; i++) {
|
|
|
+ lngX = lngX + Math.random() * 0.05;
|
|
|
+ if (i % 2) {
|
|
|
+ latY = latY + Math.random() * 0.0001;
|
|
|
+ } else {
|
|
|
+ latY = latY + Math.random() * 0.06;
|
|
|
+ }
|
|
|
+ lineArr.push(new AMap.LngLat(lngX, latY));
|
|
|
+ }
|
|
|
+
|
|
|
+ let carOffset = new AMap.Pixel(-26, -13);
|
|
|
+ this.setState({ AMap, layers: [roadNet, traffic], lineArr, carOffset });
|
|
|
});
|
|
|
}
|
|
|
|
|
|
_mapDblclick() {
|
|
|
this.setState({msg:'双击了Map!'});
|
|
|
}
|
|
|
+ _carMoving(e) {
|
|
|
+ this.setState({msg: '车在开', passedPath: e.passedPath});
|
|
|
+ }
|
|
|
+ _logging(e) {
|
|
|
+ this.setState({msg: 'log:'+JSON.stringify(e.data)});
|
|
|
+ }
|
|
|
|
|
|
render() {
|
|
|
return (
|
|
@@ -62,14 +87,28 @@ class MarkerTest extends Component {
|
|
|
</span>
|
|
|
</div>
|
|
|
<div style={{margin:2}}>
|
|
|
+ <input style={{margin:2, padding:2}} type='button' onClick={() => {
|
|
|
+ if (this.state.carEntity) this.state.carEntity.moveAlong(this.state.lineArr, 500);
|
|
|
+ }} value='开始' />
|
|
|
+ <input style={{margin:2, padding:2}} type='button' onClick={() => {
|
|
|
+ if (this.state.carEntity) this.state.carEntity.pauseMove();
|
|
|
+ }} value='暂停' />
|
|
|
+ <input style={{margin:2, padding:2}} type='button' onClick={() => {
|
|
|
+ if (this.state.carEntity) this.state.carEntity.resumeMove();
|
|
|
+ }} value='继续' />
|
|
|
+ <input style={{margin:2, padding:2}} type='button' onClick={() => {
|
|
|
+ if (this.state.carEntity) this.state.carEntity.stopMove();
|
|
|
+ }} value='停止' />
|
|
|
+ </div>
|
|
|
+ <div style={{margin:2}}>
|
|
|
{'消息:'+this.state.msg}
|
|
|
</div>
|
|
|
<Map
|
|
|
AMap={this.state.AMap}
|
|
|
- style={{ width: 700, height: 800 }}
|
|
|
- options={{ center: this.state.center, layers: this.state.layers }}
|
|
|
+ style={{ width: 1200, height: 800 }}
|
|
|
+ options={{ center: this.state.center, layers: this.state.layers, zoom:13 }}
|
|
|
events={{
|
|
|
- click:e=>this.setState({msg: '点击了Map'}),
|
|
|
+ click:this._logging,
|
|
|
dblclick: this._mapDblclick,
|
|
|
}}
|
|
|
>
|
|
@@ -81,7 +120,7 @@ class MarkerTest extends Component {
|
|
|
position: this.state.position || [116.405467, 39.907761]
|
|
|
}}
|
|
|
events={{
|
|
|
- click:e=>this.setState({msg: '点击了Marker1'}),
|
|
|
+ click:this._logging
|
|
|
}}
|
|
|
>
|
|
|
</Marker>
|
|
@@ -94,10 +133,49 @@ class MarkerTest extends Component {
|
|
|
content:'<div class="marker-route marker-marker-bus-from"></div>'
|
|
|
}}
|
|
|
events={{
|
|
|
- click:e=>this.setState({msg: '点击了Marker2'})
|
|
|
+ click:this._logging
|
|
|
}}
|
|
|
- >
|
|
|
- </Marker>
|
|
|
+ />
|
|
|
+ <Marker
|
|
|
+ AMap={this.state.AMap}
|
|
|
+ refer={(entity) => this.setState({carEntity: entity})}
|
|
|
+ options={{
|
|
|
+ position: [116.397428, 39.90923],
|
|
|
+ icon: "https://webapi.amap.com/images/car.png",
|
|
|
+ offset: this.state.carOffset,
|
|
|
+ autoRotation: true
|
|
|
+ }}
|
|
|
+ events={{
|
|
|
+ moving:this._carMoving
|
|
|
+ }}
|
|
|
+ />
|
|
|
+ <Polyline
|
|
|
+ AMap={this.state.AMap}
|
|
|
+ options={{
|
|
|
+ path: this.state.lineArr,
|
|
|
+ strokeColor: "#00A", //线颜色
|
|
|
+ // strokeOpacity: 1, //线透明度
|
|
|
+ strokeWeight: 3, //线宽
|
|
|
+ // strokeStyle: "solid" //线样式
|
|
|
+ }}
|
|
|
+ events={{
|
|
|
+ click:this._logging
|
|
|
+ }}
|
|
|
+ />
|
|
|
+ <Polyline
|
|
|
+ AMap={this.state.AMap}
|
|
|
+ options={{
|
|
|
+ // path: lineArr,
|
|
|
+ path:this.state.passedPath,
|
|
|
+ strokeColor: "#F00", //线颜色
|
|
|
+ // strokeOpacity: 1, //线透明度
|
|
|
+ strokeWeight: 3, //线宽
|
|
|
+ // strokeStyle: "solid" //线样式
|
|
|
+ }}
|
|
|
+ events={{
|
|
|
+ click:this._logging
|
|
|
+ }}
|
|
|
+ />
|
|
|
</Map>
|
|
|
</div>
|
|
|
</div>
|
|
@@ -212,6 +290,94 @@ class MassMarkTest extends Component {
|
|
|
|
|
|
}
|
|
|
|
|
|
+class PolygonTest extends Component {
|
|
|
+ constructor() {
|
|
|
+ super();
|
|
|
+ this.state = {};
|
|
|
+ this._mapDblclick = this._mapDblclick.bind(this);
|
|
|
+ this._changeStyle = this._changeStyle.bind(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ componentDidMount() {
|
|
|
+ loadMap('0325e3d6d69cd56de4980b4f28906fd8').then(AMap => {
|
|
|
+ let roadNet = new AMap.TileLayer.RoadNet();
|
|
|
+ let traffic = new AMap.TileLayer.Traffic({
|
|
|
+ autoRefresh: true, //是否自动刷新,默认为false
|
|
|
+ interval: 15 //刷新间隔,默认180s
|
|
|
+ });
|
|
|
+
|
|
|
+ this.setState({ AMap, layers: [roadNet, traffic], poly: 0 });
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ _mapDblclick() {
|
|
|
+ this.setState({msg:'双击了Map!'});
|
|
|
+ }
|
|
|
+ _changeStyle() {
|
|
|
+ this.setState({poly:1})
|
|
|
+ }
|
|
|
+
|
|
|
+ render() {
|
|
|
+ let poly0 = [//多边形覆盖物节点坐标数组
|
|
|
+ [116.403322, 39.920255],
|
|
|
+ [116.410703, 39.897555],
|
|
|
+ [116.402292, 39.892353],
|
|
|
+ [116.389846, 39.891365]
|
|
|
+ ];
|
|
|
+ let poly1 = [//多边形覆盖物节点坐标数组
|
|
|
+ [116.404322, 39.920355],
|
|
|
+ [116.411703, 39.897355],
|
|
|
+ [116.402392, 39.892453],
|
|
|
+ [116.381946, 39.881465]
|
|
|
+ ];
|
|
|
+ let poly = (this.state.poly == 0) ? poly0: poly1;
|
|
|
+
|
|
|
+ return (
|
|
|
+ <div>
|
|
|
+ <div>
|
|
|
+ <div style={{margin:2}}>
|
|
|
+ <span style={{marginRight:2}}>改Map位置:</span>
|
|
|
+ <input type='button' onClick={() => this.setState({ poly:0 })} value='poly0' />
|
|
|
+ <input type='button' onClick={() => this.setState({ poly:1 })} value='poly1' />
|
|
|
+ </div>
|
|
|
+ <div style={{margin:2}}>
|
|
|
+ {'消息:'+this.state.msg}
|
|
|
+ </div>
|
|
|
+ <Map
|
|
|
+ AMap={this.state.AMap}
|
|
|
+ style={{ width: 1100, height: 800 }}
|
|
|
+ options={{ center: this.state.center, layers: this.state.layers }}
|
|
|
+ events={{
|
|
|
+ // click:e=>this.setState({msg: '点击了Map'}),
|
|
|
+ dblclick: this._mapDblclick,
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ <Polygon
|
|
|
+ AMap={this.state.AMap}
|
|
|
+ options={{
|
|
|
+ path: poly,
|
|
|
+ strokeColor: "#FF33FF", //线颜色
|
|
|
+ strokeOpacity: 0.2, //线透明度
|
|
|
+ strokeWeight: 3, //线宽
|
|
|
+ fillColor: "#1791fc", //填充色
|
|
|
+ fillOpacity: 0.35,//填充透明度
|
|
|
+ extData: {poly:this.state.poly}
|
|
|
+ }}
|
|
|
+ events={{
|
|
|
+ click:e=> {
|
|
|
+ console.log('e:', e);
|
|
|
+ this._changeStyle(e.data);
|
|
|
+ this.setState({msg: '点击了Poly:'+JSON.stringify(e.data)})
|
|
|
+ }
|
|
|
+ }}
|
|
|
+ />
|
|
|
+ </Map>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
class App extends Component {
|
|
|
constructor() {
|
|
|
super();
|
|
@@ -224,7 +390,7 @@ class App extends Component {
|
|
|
<div>
|
|
|
<span style={{padding:5, margin: 5, backgroundColor:(this.state.test === 'marker')?'#ff0':'#fff'}} onClick={()=>this.setState({test: 'marker'})}> MarkerTest </span>
|
|
|
<span style={{padding:5, margin: 5, backgroundColor:(this.state.test === 'massmarks')?'#ff0':'#fff'}} onClick={()=>this.setState({test: 'massmarks'})}> MassMarksTest </span>
|
|
|
- <span style={{padding:5, margin: 5, backgroundColor:(this.state.test === 'mess')?'#ff0':'#fff'}} onClick={()=>this.setState({test: 'mess'})}> MessMarkerTest </span>
|
|
|
+ <span style={{padding:5, margin: 5, backgroundColor:(this.state.test === 'polygon')?'#ff0':'#fff'}} onClick={()=>this.setState({test: 'polygon'})}> PolygonTest </span>
|
|
|
</div>
|
|
|
<div>
|
|
|
{this.state.test === 'marker' &&
|
|
@@ -233,6 +399,9 @@ class App extends Component {
|
|
|
{this.state.test === 'massmarks' &&
|
|
|
<MassMarkTest />
|
|
|
}
|
|
|
+ {this.state.test === 'polygon' &&
|
|
|
+ <PolygonTest />
|
|
|
+ }
|
|
|
|
|
|
</div>
|
|
|
</div>
|