百度地图的使用---1、标注(Marker)的使用以及自定义样式

先看效果:

步骤如下:

1、HTML

<div id="mapContainer" style={{width:"100%", height:"400px"}}/>

2、获取地图容器元素

const mapContainer = document.getElementById("mapContainer");

3、创建地图实例

const map = new BMapGL.Map("mapContainer");

4、设置中心点和缩放级别

map.centerAndZoom(new BMapGL.Point(116.404, 39.915), 15);

5、创建一个BMapGL.Point对象来表示标记点的位置

const markerPoint = new BMapGL.Point(116.404, 39.915);

6、使用这个位置创建一个 Marker组件

const marker = new BMapGL.Marker(markerPoint);

7、将标记点添加到地图上

map.addOverlay(marker);

8、完整代码

import React, { useEffect, useRef } from "react";

const MapContainer: React.FC = () => {
  const mapContainer = document.getElementById("mapContainer");

  useEffect(() => {
    const map = new BMapGL.Map(mapContainer);
    map.centerAndZoom(new BMapGL.Point(116.404, 39.915), 15);
    const markerPoint = new BMapGL.Point(116.404, 39.915);
    const marker = new BMapGL.Marker(markerPoint);
    map.addOverlay(marker);
  }, []);

  return <div id="mapContainer" style={{ width: "100%", height: "400px" }} />;
};

export default MapContainer;

最终效果:

9、扩展:添加多个Marker标注

map.centerAndZoom(new BMapGL.Point(116.404, 39.915), 15);
const markersData = [
    { lng: 116.404, lat: 39.915 },
    { lng: 116.414, lat: 39.915 },
    { lng: 116.424, lat: 39.915 }
];

// 循环创建标记并添加到地图上
markersData.forEach((markerData) => {
    const point = new BMapGL.Point(markerData.lng, markerData.lat);
    const marker = new BMapGL.Marker(point);
    map.addOverlay(marker);
});

最终效果:

10、扩展:自定义图标和尺寸

使用BMapGL.Icon类来创建自定义的图标,BMapGL.Size来设置图标的尺寸

const icon = new BMapGL.Icon(“path”, new BMapGL.Size(51, 51));
const point = new BMapGL.Point(116.404, 39.915);
const marker = new BMapGL.Marker(point, { icon: icon });
map.addOverlay(marker);

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
-baidu-map是一个基于Vue2的百度地图组件库,它提供了丰富的地图组件,包括地图标注、信息窗口、线、面、工具条等等。如果你想自定义地图,可以使用百度地图JavaScript API提供的个性化地图服务。具体步骤如下: 1.在百度地图开放平台申请个性化地图服务,获取ak密钥。 2.在Vue项目中安装百度地图JavaScript API,可以使用npm install baidu-map --save命令进行安装。 3.在Vue项目中引入百度地图JavaScript API,可以在index.html文件中添加如下代码: ```html <script type="text/javascript" src="http://api.map.baidu.com/api?v=3.0&ak=您的密钥"></script> ``` 4.在Vue项目中使用vue-baidu-map组件,可以在组件中添加如下代码: ```html <template> <div> <baidu-map class="map" :center="center" :zoom="zoom" :map-style="mapStyle"> <bm-marker :position="center"></bm-marker> </baidu-map> </div> </template> <script> import { BaiduMap, BmMarker } from 'vue-baidu-map' export default { components: { BaiduMap, BmMarker }, data () { return { center: { lng: 116.404, lat: 39.915 }, zoom: 15, mapStyle: { styleJson: [ { 'featureType': 'water', 'elementType': 'all', 'stylers': { 'color': '#044161' } }, { 'featureType': 'land', 'elementType': 'all', 'stylers': { 'color': '#091934' } }, { 'featureType': 'boundary', 'elementType': 'geometry', 'stylers': { 'color': '#064f85' } }, { 'featureType': 'railway', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'highway', 'elementType': 'geometry', 'stylers': { 'color': '#004981' } }, { 'featureType': 'highway', 'elementType': 'all', 'stylers': { 'color': '#005b96', 'lightness': 1.4, 'visibility': 'off' } }, { 'featureType': 'arterial', 'elementType': 'geometry', 'stylers': { 'color': '#004981', 'lightness': -39 } }, { 'featureType': 'green', 'elementType': 'all', 'stylers': { 'color': '#056197', 'visibility': 'off' } }, { 'featureType': 'subway', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'manmade', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'local', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'arterial', 'elementType': 'labels', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'boundary', 'elementType': 'geometry.fill', 'stylers': { 'color': '#029fd4' } }, { 'featureType': 'building', 'elementType': 'all', 'stylers': { 'color': '#1a5787' } }, { 'featureType': 'label', 'elementType': 'all', 'stylers': { 'visibility': 'off' } } ] } } } } </script> ``` 在上述代码中,我们使用了BaiduMap和BmMarker组件来显示地图标注,同时使用了个性化地图服务提供的样式来自定义地图样式

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值