高德地图

首先引入高德地图JSAPI

 <script src="https://webapi.amap.com/maps?v=1.3&key=你在高德官网申请的密钥"></script>
// 初始化地图
const mapObj = new AMap.Map('map-container', {
     zoom: 17
     // center: [lng, lat]
 });

 // 获取当前城市信息
 mapObj.getCity((data) => {
     console.log(data.citycode);
 });

 // 获取定位
 mapObj.plugin('AMap.Geolocation', () => {
                mapObj.clearMap();
                const geolocation = new AMap.Geolocation({
                    enableHighAccuracy: true, // 是否使用高精度定位,默认:true
                    timeout: 10000, // 超过2秒后停止定位,默认:无穷大
                    // maximumAge: 0, // 定位结果缓存0毫秒,默认:0
                    // convert: true, // 自动偏移坐标,偏移后的坐标为高德坐标,默认:true
                    showButton: true, // 显示定位按钮,默认:true
                    // buttonPosition: 'LB', // 定位按钮停靠位置,默认:'LB',左下角
                    buttonOffset: new AMap.Pixel(10, 20), // 定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
                    showMarker: true, // 定位成功后在定位到的位置显示点标记,默认:true
                    showCircle: false, // 定位成功后用圆圈表示定位精度范围,默认:true
                    panToLocation: true, // 定位成功后,是否把定位得到的坐标设置为地图中心点坐标默认值:true
                    zoomToAccuracy: false // 定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
                });
                geolocation.getCurrentPosition((status, result) => {
                    if (status === 'complete') {
                        this.center = [result.position.lng, result.position.lat];
                        this.lng = result.position.lng.toString();
                        this.lat = result.position.lat.toString();
                        console.log(`getGeolocationPosition定位成功lng:${this.lng}---lat${this.lat}`);
                        mapObj.setCenter(this.center);
                    } else {
                        // 获取定位失败时可能需要重新定位
                        // 此处可以尝试递归调用
                    }
                });

                // 添加地图控件
                mapObj.addControl(geolocation);

                // 地图点击事件监听
                // AMap.event.addListener(geolocation, 'complete', (data) => {
                //     this.center = [data.position.getLng(), data.position.getLat()]; // 当前中心点
                //     console.log(`addListener--complete---${this.center}`);
                // }); // 返回定位信息
                // AMap.event.addListener(geolocation, 'error', (data) => {
                //     console.log(`addListener--complete---${data}`);
                // }); // 返回定位出错信息
            });


            // 拖拽地图---标记目标位置
            AMapUI.loadUI(['misc/PositionPicker'], (PositionPicker) => {
                const positionPicker = new PositionPicker({
                    mode: 'dragMap',
                    map: mapObj,
                    iconStyle: {
                        url: 'static/assets/nail.png', // 标记图标
                        size: [15, 35],
                        ancher: [7.5, 35]
                    }
                });
                positionPicker.on('success', (positionResult) => {
                    // mapObj.remove(this.markers); // 拖拽时重新展示当前位置附近多车辆
                    console.log('delete markers');
                    console.log(positionResult.position);
                    // positionPicker.start(mapObj.getBounds().getSouthWest());
                });
                positionPicker.on('fail', (positionResult) => {
                    console.log(positionResult);
                });
                positionPicker.start(); // 开始拖拽选址
                mapObj.panBy(0, 1);
            });

            // 地图显示圆圈区域
            const Circle = new AMap.Circle({
                center: [lng, lat],
                radius: 10,
                strokeColor: 'white',
                strokeWeight: 2,
                strokeOpacity: 0.5,
                fillColor: 'rgba(50,140,250,1)',
                fillOpacity: 0.6,
                zIndex: 10
            });
            Circle.setMap(mapObj);

            // 根据坐标点设置标记
            const marker = new AMap.Marker({
                 position: [lng, lat],
                 zIndex: 1000,
                 offset: new AMap.Pixel(-8, -30),
                 icon: new AMap.Icon({
                     size: new AMap.Size(15, 30),
                     image: 'static/assets/nail.png',
                     imageSize: new AMap.Size(15, 30)
                     // offset: new AMap.Pixel(-10, -34)
                     // icon可缺省,缺省时为默认的蓝色水滴图标,
                 })
             });
             marker.setMap(mapObj);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值