vue项目中根据地址在百度地图上定位显示标注信息
需求:后端返回的地址,点击图标在地图上显示,并且有位置标注。
这里后端只返回了地址,获取经纬度需要自己处理。
1 2 3 | < div class="bg_first"> < div class="map" id="allmap"></ div > </ div > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | export default { data() { return { baseinfo: { address: '北京中关村南大街12号' , // address: '上海浦东新区浦东南路3993号', }, // 百度地图参数 BMap: {}, map: {}, location: { lng: 116.40400, lat: 39.92800 }, keyword: '' , myGeo: '' , map: '' , } }, created() { this .$nextTick(() => { this .shaowMap(); }) }, methods: { handleSeat(item) { this .keyword = item; this .getPointByAddress(item).then(res => { console.log( '8.28' , res) this .shaowMap(res); }); }, //根据地名获取经纬度 getPointByAddress(address) { const myGeo = new BMap.Geocoder(); return new Promise((resolve, reject) => { myGeo.getPoint(address, (point) => { //对地址进行地理编码,编码成功,则返回经纬度坐标对象 if (point) { this .location.lng = point.lng; this .location.lat = point.lat; console.log( '打印一下' , this .location) resolve(point); } else { reject( '地理编码失败' ); } }) }, '全国' ) }, shaowMap (res) { console.log(res) /* global BMap */ this .map = new BMap.Map( 'allmap' ) // 创建Map实例 this .map.centerAndZoom( new BMap.Point(res.lng, res.lat), 10) this .map.enableScrollWheelZoom( true ) this .myGeo = new BMap.Geocoder() this .geocodeSearch( this .keyword) }, geocodeSearch (add) { let that = this ; that.myGeo.getPoint(add, function (point) { if (point) { console.log( 'point' , point) var marker = new BMap.Marker(point) that.map.addOverlay(marker) var opts = { position: point, // 指定文本标注所在的地理位置 offset: new BMap.Size(1, -50) // 设置文本偏移量 } var label = new BMap.Label(add, opts) // 创建文本标注对象 label.setStyle({ color: 'red' , fontSize: '15px' , height: '20px' , lineHeight: '20px' , border: 'none' }) that.map.addOverlay(label) } else { console.log( '您选择地址没有解析到结果!' ) } }, '北京市' ) } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通