Mrcdh技术博客 Mrcdh技术博客
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Mrcdh

全栈开发小学生
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Html5

  • JavaScript

    • javascript常用操作
    • 安利一个网页调试开发利器vConsole
    • 各地图平台间的坐标转换
    • Viewer.js图片查看器
    • IndexedDB的增删改查的封装
    • axios常用操作
    • 前端使用js-cookie用来操作cookie
    • Symbol基本使用
    • 高德地图常用操作
      • 初始化地图
      • 坐标转换为地址信息
      • 获取当前定位信息
      • 添加Market点加信息弹窗
      • 获取省市区信息
      • 在Vue中使用高德地图
    • 使用qrcodejs2生成显示二维码
    • 常用Jquery插件
    • pushjs浏览器通知插件
  • Nodejs

  • express

  • electron

  • Android

  • 微信公众号

  • 框架

  • 其他

  • selenium

  • Sequelize

  • 大前端
  • JavaScript
mrcdh
2023-06-20
目录

高德地图常用操作

引入地图

<script src="https://webapi.amap.com/maps?v=2.0&key=key值&plugin=AMap.IndexCluster,AMap.Geocoder,AMap.DistrictSearch"></script>
1

# 初始化地图

cosnt map = new AMap.Map("map", {
    zoom: 4,
    animateEnable: true,
    isHotspot: false,
    center: [115.656358, 34.415165],
    viewMode: '3D',
});
1
2
3
4
5
6
7

# 坐标转换为地址信息


var geocoder = new AMap.Geocoder({
    city: "010", //城市设为北京,默认:“全国”
    radius: 1000 //范围,默认:500
});
geocoder.getAddress(lnglat, function(status, result) {
    if (status === 'complete'&&result.regeocode) {
        var address = result.regeocode.formattedAddress;
        document.getElementById('address').value = address;
    }else{
        log.error('根据经纬度查询地址失败')
    }
});
1
2
3
4
5
6
7
8
9
10
11
12
13

# 获取当前定位信息

AMap.plugin('AMap.Geolocation', function() {
    var geolocation = new AMap.Geolocation({
        enableHighAccuracy: true,//是否使用高精度定位,默认:true
        timeout: 10000,          //超过10秒后停止定位,默认:5s
        position:'RB',    //定位按钮的停靠位置
        offset: [10, 20], //定位按钮与设置的停靠位置的偏移量,默认:[10, 20]
        zoomToAccuracy: true,   //定位成功后是否自动调整地图视野到定位点

    });
    map.addControl(geolocation);
    geolocation.getCurrentPosition(function(status,result){
        if(status=='complete'){
            onComplete(result)
        }else{
            onError(result)
        }
    });
});
//解析定位结果
function onComplete(data) {
    document.getElementById('status').innerHTML='定位成功'
    var str = [];
    str.push('定位结果:' + data.position);
    str.push('定位类别:' + data.location_type);
    if(data.accuracy){
        str.push('精度:' + data.accuracy + ' 米');
    }//如为IP精确定位结果则没有精度信息
    str.push('是否经过偏移:' + (data.isConverted ? '是' : '否'));
    document.getElementById('result').innerHTML = str.join('<br>');
}
//解析定位错误信息
function onError(data) {
    document.getElementById('status').innerHTML='定位失败'
    document.getElementById('result').innerHTML = '失败原因排查信息:'+data.message+'</br>浏览器返回信息:'+data.originMessage;
}
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

# 添加Market点加信息弹窗

 var map = new AMap.Map("container", {resizeEnable: true});
var lnglats = [
    [116.368904, 39.923423],
    [116.382122, 39.921176],
    [116.387271, 39.922501],
    [116.398258, 39.914600]
];
var infoWindow = new AMap.InfoWindow({offset: new AMap.Pixel(0, -30)});
for (var i = 0, marker; i < lnglats.length; i++) {
    var marker = new AMap.Marker({
        position: lnglats[i],
        map: map
    });
    marker.content = '我是第' + (i + 1) + '个Marker';
    marker.on('click', markerClick);
    marker.emit('click', {target: marker});
}
function markerClick(e) {
    infoWindow.setContent(e.target.content);
    infoWindow.open(map, e.target.getPosition());
}
map.setFitView();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 获取省市区信息

this.districtPlugin = new AMap.DistrictSearch({
    // 返回行政区边界坐标等具体信息
    extensions: 'all',
    // 设置查询行政区级别为 区
    // level: 'district'
    subdistrict:3 // 设置为3就能一直获取到区
})

this.mapData = await (new Promise((resolve, reject) => {
    this.districtPlugin.search('中国', function (status, result) {
        resolve(result.districtList[0].districtList)
    })
}))
1
2
3
4
5
6
7
8
9
10
11
12
13
  • level (string) 关键字对应的行政区级别或商圈,可选值: country:国家 province:省/直辖市 city:市 district:区/县 biz_area:商圈
  • showbiz (boolean) 是否显示商圈,默认值true 可选为true/false,为了能够精准的定位到街道,特别是在快递、物流、送餐等场景下,强烈建议将此设置为false
  • extensions (string) 是否返回行政区边界坐标点,默认值:base,不返回行政区边界坐标点,取值:all,返回完整行政区边界坐标点
  • subdistrict (number) 显示下级行政区级数(行政区级别包括:国家、省/直辖市、市、区/县4个级别),商圈为区/县下一 级,可选值:0、1、2、3,默认值:1 0:不返回下级行政区 1:返回下一级行政区 2:返回下两级行政区 3:返回下三级行政区

# 在Vue中使用高德地图

npm i @amap/amap-jsapi-loader
1
import AMapLoader from '@amap/amap-jsapi-loader'
{
    created(){
        window._AMapSecurityConfig = {
            securityJsCode: 'securityJsCode'
        }
    },
    methods:{
        // 获取地图
        getAmap(plugins = []) {
            return new Promise((resolve, reject) => {
                if (this.$options.AMap !== null && this.$options.map !== null) {
                    return resolve({
                        AMap: this.$options.AMap, map: this.$options.map
                    })
                }
                AMapLoader.load({
                    key: 'key', // 申请好的Web端开发者Key,首次调用 load 时必填
                    version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
                    plugins // 需要使用的的插件列表,如比例尺'AMap.Scale'等
                }).then((AMap) => {
                    this.$options.AMap = AMap
                    // eslint-disable-next-line no-new
                    this.$options.map = new AMap.Map('amap')
                    resolve({ AMap, map: this.$options.map })
                }).catch((e) => {
                    reject(e)
                })
            })
        },
        // 获取定位信息
        getGeoLocation() {
            return new Promise((resolve, reject) => {
                this.getAmap().then((AMap) => {
                    // eslint-disable-next-line no-new
                    AMap.plugin('AMap.Geolocation', function() {
                        const geolocation = new AMap.Geolocation({
                            needAddress: true
                        })
                        geolocation.getCurrentPosition(function(status, result) {
                            console.log('定位信息:', result)
                            if (status === 'complete') {
                                resolve(result)
                            } else {
                                reject(result)
                            }
                        })
                    })
                })
            })
        },
        // 获取区域插件
        getDistrictPlugin(level = 'district') {
            return new Promise((resolve, reject) => {
                this.getAmap().then(({ AMap }) => {
                if (this.districtPlugin) {
                    return resolve(this.districtPlugin)
                }
                AMap.plugin('AMap.DistrictSearch', () => {
                        // 创建行政区查询对象
                        this.districtPlugin = new AMap.DistrictSearch({
                        // 返回行政区边界坐标等具体信息
                        extensions: 'all',
                        // 设置查询行政区级别为 区
                        level
                    })
                    resolve(this.districtPlugin)
                })
                })
            })
        }
    }
}
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
#javascript
上次更新: 2023/09/22, 16:54:32
Symbol基本使用
使用qrcodejs2生成显示二维码

← Symbol基本使用 使用qrcodejs2生成显示二维码→

最近更新
01
uniapp常见问题
03-19
02
Vue3项目搭建
12-25
03
使用pnpm的monorepo组织项目
11-01
更多文章>
Theme by Vdoing | Copyright © 2020-2025 Mrcdh | 苏ICP备2020058908号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×