React+Antd 使用百度地圖,添加地點(diǎn)輸入提示的控件

先上一個(gè)示例圖


image.png

使用React+Antd V4+百度地圖,在地圖上,添加一個(gè)可以輸入地址,檢索的控件。

先看React組件

import React from 'react';
import BMap from 'BMap';
import {AutoComplete, Button, Form, Input, InputNumber, Row} from "antd";
import Ajax from "@utils/Ajax";
interface IState {
    pointMap: any,
    autoOptions: Array<any>,
    autoValue: string
}
export default class PointMap extends React.Component<IProps, IState> {
    constructor(props: any) {
        super(props);
    }
    public readonly state: Readonly<IState> = {
        pointMap: '',
        autoOptions: [],
        autoValue: ''
    }
    componentDidMount(): void {
        this.initMap()
    }

    public initMap= () => {
        const { gpsx, gpsy } = this.props
        let map = new BMap.Map('mapContainer')
        this.setState({ pointMap: map })
        map.centerAndZoom(new BMap.Point(106.552566, 29.55642), 15);// 初始化地圖,設(shè)置中心點(diǎn)坐標(biāo)和地圖級(jí)別
        
        map.enableScrollWheelZoom(true); // 開(kāi)啟鼠標(biāo)滾輪縮放
        let overviewControl = new BMap.OverviewMapControl({ anchor: 'BMAP_ANCHOR_BOTTOM_RIGHT', isOpen: true });
        map.addControl(overviewControl);

        map.addEventListener('click', (e: any) =>{
            map.clearOverlays();
            let marker = new BMap.Marker(new BMap.Point(e.point.lng, e.point.lat));  // 創(chuàng)建標(biāo)注
            map.addOverlay(marker);
        })
    }
    handleSearch= (searchText: string) => {
//http://api.map.baidu.com/place/v2/suggestion?query=天安門(mén)&region=北京&city_limit=true&output=json&ak=你的ak //GET請(qǐng)求
//這是我去后臺(tái)服務(wù)器上請(qǐng)求的地址檢索,因?yàn)樵谇岸酥苯诱?qǐng)求會(huì)報(bào)跨域的問(wèn)題
        this.setState({ autoValue: searchText, autoOptions: [] })
        Ajax.post('/biz/restTemplate/getUrl', {query: searchText, region: '重慶'}, { dataType: 'form' }, (data: any) => {
            if (data.data.status === 0) {
                this.setState({ autoOptions: data.data.result })
            }
        })
    }
    handleSelect= (value: string) => {
        const { autoOptions, pointMap } = this.state
        this.setState({ autoValue: value })
        let select = autoOptions.filter((item: any) => item.name === value)
        if (select.length > 0 && select[0].location) {
            pointMap.clearOverlays();
            let pointItem = select[0]
            let point = {
                latitude: pointItem.location.lat,
                longitude: pointItem.location.lng
            }
            pointMap.centerAndZoom(new BMap.Point(point.longitude, point.latitude), 15);
            let marker = new BMap.Marker(new BMap.Point(point.longitude, point.latitude));  // 創(chuàng)建標(biāo)注
            pointMap.addOverlay(marker);
        }
    }
    render(): React.ReactNode {
        const { autoOptions, autoValue } = this.state
        const options = autoOptions.map((item: any, index: number) => ({ key: item.uid !== '' ? item.uid : index, value: item.name }))
        return <div>
            <Row style={{ position: 'absolute', zIndex: 10, top: 120, left: 50}} >
                <AutoComplete
                    dropdownMatchSelectWidth={252}
                    style={{ width: 300 }}
                    options={options}
                    value={autoValue}
                    onSelect={this.handleSelect}
                    onChange={this.handleSearch}
                >
                    <Input.Search placeholder="查詢地址" enterButton="查詢" />
                </AutoComplete>
            </Row>
            <div id="mapContainer" style={{ height: 600, width: '100%' }} />
        </div>;
    }
}
這里沒(méi)有使用百度地圖API中的添加自定義控件,因?yàn)楸容^麻煩的一點(diǎn)是要使用document來(lái)創(chuàng)建節(jié)點(diǎn),而且樣式也需要自己寫(xiě),點(diǎn)擊事件也要麻煩一點(diǎn)??梢灾苯釉诮M件里面寫(xiě),調(diào)整一下要添加的組件的定位就可以了
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 深入JSX date:20170412筆記原文其實(shí)JSX是React.createElement(componen...
    gaoer1938閱讀 8,190評(píng)論 2 35
  • 儒家學(xué)說(shuō)發(fā)展至隋唐,雖然有了各家更多注疏,但闡釋方式未能跟上時(shí)代發(fā)展,早已不能滿足新的時(shí)代的需求。另一方面,隨著道...
    盧sir閱讀 1,160評(píng)論 9 1
  • 傷心難過(guò) 近二十天的空歡喜、空擔(dān)憂、空計(jì)劃。 一切如夢(mèng)般的開(kāi)始如夢(mèng)般的結(jié)束。美麗活潑可愛(ài)的精靈小鹿帶我前進(jìn)的動(dòng)...
    DAN524閱讀 260評(píng)論 0 0
  • 魔笛大公舉閱讀 206評(píng)論 1 1
  • 時(shí)間如白駒過(guò)隙,轉(zhuǎn)眼又快周末了,從上周日開(kāi)始算起來(lái),今天上班上學(xué)已經(jīng)第五天了。由于工作的關(guān)系,對(duì)于我來(lái)說(shuō)...
    藍(lán)楓_78e8閱讀 334評(píng)論 0 3

友情鏈接更多精彩內(nèi)容