react中使用百度地图选择位置 发表于 2017-08-28 | 更新于 2023-01-11 项目需要,制作一个简单的react使用百度地图选择位置的组件 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455import React, {Component} from 'react';import PropTypes from 'prop-types';class AddressManage extends Component { static propTypes = { confirmAddress: PropTypes.func }; componentDidMount() { this.initMap(); } initMap = () => { let map = new BMap.Map('chooseAddress'); let point = new BMap.Point(116.331398,39.897445); map.centerAndZoom(point,12); map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放 let geolocation = new BMap.Geolocation(); // 开启SDK辅助定位 geolocation.enableSDKLocation(); geolocation.getCurrentPosition(function (r) { if (this.getStatus() == BMAP_STATUS_SUCCESS) { map.panTo(r.point); } else { console.log('failed' + this.getStatus()); } }); let geoc = new BMap.Geocoder(); map.addEventListener('click', function(e) { let pt = e.point; geoc.getLocation(pt, function(rs) { console.log(rs); let addComp = rs.addressComponents; this.props.confirmAddress({ point: rs.point, address: addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber }); }); }); }; render() { return ( <div className='full'> <div id='chooseAddress' className='full'> </div> </div> ); }}export default AddressManage;