js配合百度API实现地理定位

先上代码吧
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=7a6QKaIilZftIMmKGAFLG7QT1GLfIncg"></script>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    html,
    body {
        width: 100%;
        height: 100%;
    }

    article {
        width: 65%;
        height: 95%;
        background-color: aquamarine;
        background-color: #efeeee;
        box-shadow: 2px 2px 2px rgba(0, 0, 0, .2),
            -2px -2px 20px rgba(225, 225, 225, 1);
    }

    body,
    article {
        display: flex;
        flex-direction: column;
        justify-content: space-evenly;
        align-items: center;
    }

    .geoInfo {
        width: 60%;
        height: 60%;
        border: 3px dashed rgb(85, 98, 110);
    }
</style>

<body>
    <article>
        <h1>地图API</h1>
        <div class="geoInfo" id="geoInfo"></div>
    </article>
</body>

<script type="text/javascript">
	// 获取存放地图的容器
    var geoInfo = document.querySelector('#geoInfo');
    // 创建地图实例
    var map = new BMap.Map("geoInfo");

    var geolocation = new BMap.Geolocation();
    // 利用浏览器的定位功能(别问原理,开发文档上就这么写的)
    geolocation.getCurrentPosition(function (e) {
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            // console.log(e.point.lng); e.point.lng: 定位得到的经度
            // console.log(e.point.lat); e.point.lat: 定位得到的纬度
            // 地图初始化,同时设置地图展示级别(15)
            map.centerAndZoom(new BMap.Point(e.point.lng, e.point.lat), 15);
        } else {
        	// 错误就在容器输出 false
            geoInfo.innerHTML = 'false';
        }
    });
</script>

</html>

需要说明一下,第八行引入百度API的代码,后面ak里的内容是我自己在百度地图开放平台申请的,大家要是练习或者做测试的话,建议是自己去申请一个。
百度地图申请ak
然后把ak后面的内容换成自己申请的,其他的就,,,还请谨慎改动。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

z_yuyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值