高德地图定位签到功能实现

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>浏览器精确定位</title>
      <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
    <style>
        html,body,#container{
            height:100%;
        }
        .info{
            width:26rem;
        }
    </style>
<body>
<div id='container'></div>
<div class="info">
    <h4 id='status'></h4><hr>
    <p id='result'></p><hr>
    <p >由于众多浏览器已不再支持非安全域的定位请求,为保位成功率和精度,请升级您的站点到HTTPS。</p>
</div>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.13&key=您申请的key值"></script>
<script type="text/javascript">
    var map = new AMap.Map('container', {
        resizeEnable: true
    });
    AMap.plugin('AMap.Geolocation', function() {
        var geolocation = new AMap.Geolocation({
            enableHighAccuracy: true,//是否使用高精度定位,默认:true
            timeout: 10000,          //超过10秒后停止定位,默认:5s
            buttonPosition:'RB',    //定位按钮的停靠位置
            buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量,默认:Pixel(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;
    }
</script>
</body>
</html>

   

转载于:https://my.oschina.net/u/658291/blog/3021484

Vue高德地图定位按钮功能实现可以分为以下几个步骤: 1. 引入高德地图JavaScript SDK库。在Vue的项目中,可以通过npm安装amap-jsapi-loader库,并在入口文件(如main.js)中导入并使用该库加载高德地图的JS API。 2. 在Vue组件中创建地图容器。可以使用AMap.Map()方法创建一个地图的实例并指定容器的id,例如:new AMap.Map('map-container')。 3. 创建定位按钮并绑定相应的点击事件。在Vue组件的template中添加一个按钮元素,并为其添加一个@click事件监听器,用来触发定位功能实现。 4. 实现定位功能。在点击定位按钮时,触发点击事件的处理函数,在该函数中调用高德地图定位服务API,如AMap.Geolocation()来获取当前位置的经纬度。 5. 将定位的经纬度设置为地图的中心点,并在地图上显示一个标记点。可以使用AMap.Marker()创建一个标记点实例,然后设置其position为定位的经纬度,并添加到地图上。 6. 可以根据需要,将经纬度转换为具体的地址信息,并在页面中显示。可以使用AMap.Geocoder()的逆地理编码功能,将经纬度转换为地址。 具体代码示例: ```vue <template> <div> <button @click="getLocation">定位</button> <div id="map-container"></div> <div>{{location}}</div> </div> </template> <script> import AMapLoader from 'amap-jsapi-loader'; export default { data() { return { map: null, location: '' } }, mounted() { AMapLoader.load({ key: 'your-amap-api-key', version: '2.0' }).then(() => { this.map = new AMap.Map('map-container'); }); }, methods: { getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition((position) => { const { latitude, longitude } = position.coords; this.map.setCenter([longitude, latitude]); new AMap.Marker({ position: [longitude, latitude], map: this.map }); const geocoder = new AMap.Geocoder(); geocoder.getAddress([longitude, latitude]).then((result) => { this.location = result.regeocode.formattedAddress; }); }); } else { alert('浏览器不支持定位功能'); } } } }; </script> ``` 需要注意的是,代码中的'your-amap-api-key'需要替换为你自己申请的高德地图API的秘钥。另外,在使用定位功能时,需要用户授权浏览器获取其位置信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值