码迷,mamicode.com
首页 > 其他好文 > 详细

react amap 高德地图中多点定位,图标自定义

时间:2020-07-09 15:03:25      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:too   long   nts   render   enter   icons   mount   高德   ase   

技术图片

首先在项目里面install react-amap

我用的自定义图标是svg格式的,可以自行替换成本地icon

import React, { Component } from ‘react‘
import { Map, Markers } from ‘react-amap‘
import { Icon } from ‘antd‘;
import icons from ‘../../assets/img/yellow_icon.png‘
import Svgvector from ‘../../components/Svgvector‘;
const mapKey = process.env.REACT_APP_AMAP_KEY //需要自己去高德官网上去申请process.env.REACT_APP_BASE_URL

class Address extends Component {
    constructor(props) {
        super(props)
        this.state = {
            visible: false,
            offset: [0, 0],
            positions: [116,38],
            context: [],
            assetCount: [],
            markList: [
                {
                    context: 1,
                    position:{
                        longitude: 116,
                        latitude: 39,
                    },
                    assetCount: ‘‘,
                    address: ‘‘
                },
                {
                    context: 2,
                    position:{
                        longitude: 116,
                        latitude: 38,
                    },
                    assetCount: ‘‘,
                    address: ‘‘
                },
                {
                    context: 3,
                    position:{
                        longitude: 117,
                        latitude: 39,
                    },
                    assetCount: ‘‘,
                    address: ‘‘
                }
            ],
        };
        this.markersEvents = {
            click: (MapsOption, marker) => {
                console.log(‘map data changed, got event‘,);
                const extData = marker.getExtData();
                // console.log(extData)
                this.setState({
                    visible: !this.state.visible,
                    positions: extData.position,
                    context: extData.context,
                    assetCount: extData.assetCount
                });
            },
        };
    }
   
    UNSAFE_componentWillMount(){
        
    }
    //自定义icon代码
    renderLayout(extData) {
        if(extData.context===1){
            return (
                <div style={{position: ‘relative‘}}>
                    <div className="map_icon_num" style={{background:‘#ff0000‘}}>{extData.context}</div>
                    
                    <div style={{width:16,height:16}}>
                        <Icon style={{ ‘color‘: ‘#ff0000‘, ‘margin‘: ‘0 4px‘ }} component={Svgvector} />
                    </div>
                </div>
            );
        }
        if(extData.context===2){
            return (
                <div style={{position: ‘relative‘}}>
                    <div className="map_icon_num" style={{background:‘#FF7A00‘}}>{extData.context}</div>
                    
                    <div style={{width:16,height:16}}>
                        <Icon style={{ ‘color‘: ‘#FF7A00‘, ‘margin‘: ‘0 4px‘ }} component={Svgvector} />
                    </div>
                </div>
            );
        }
        if(extData.context===3){
            return (
                <div style={{position: ‘relative‘}}>
                    <div className="map_icon_num" style={{background:‘#0CA919‘}}>{extData.context}</div>
                    
                    <div style={{width:16,height:16}}>
                        <Icon style={{ ‘color‘: ‘#0CA919‘, ‘margin‘: ‘0 4px‘ }} component={Svgvector} />
                    </div>
                </div>
            );
        }
    }
    render() {
        return (
            <div style={{ width: ‘100%‘, height: ‘100%‘}}>
                {
                    this.state.markList.length <= 0 ? 
                    (
                        <Map amapkey={mapKey}
                        zoom={10}
                        center={this.state.positions}//初始化地图中心点
                        plugins={[‘ToolBar‘]} >
                        </Map>
                    ) :
                    (
                        <Map amapkey={mapKey}
                            zoom={10}
                            
                            center={this.state.positions}
                            plugins={[‘ToolBar‘]}>
                            
                            <Markers
                                markers={this.state.markList}
                                render={this.renderLayout}
                                // useCluster={true}
                                // events={this.markersEvents}
                            >
                            </Markers>
                        </Map>
                    )
                }
                
            </div>
        )
    }
}

export default Address

  

 

react amap 高德地图中多点定位,图标自定义

标签:too   long   nts   render   enter   icons   mount   高德   ase   

原文地址:https://www.cnblogs.com/baiyq/p/13273553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!