主页   文章   ThinkPHP5  

html5获取自己定位的方法

时间:2018-10-09 22:26

第一种方法

  • html5获取自己定位的方法

  • 直接用高德地图api 不要用百度地图api 不准确

<!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="http://cache.amap.com/lbs/static/main1119.css"/>
    <script type="text/javascript"
            src="http://webapi.amap.com/maps?v=1.3&key=1ad560f64c645eaecdd611ce2facc670"></script>
    <script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
<body>

<div style="width:30px; height:30px; background: #000; position: absolute;left:10px; top:10px;z-index: 9999" class="dss" onclick="getdingwei();"></div>
  <div id="result"></div>
<div id='container' style="display: none"></div>
<div id="tip"></div>
<script type="text/javascript">

    function regeocoder(lnglatXY) {  //逆地理编码
        var geocoder = new AMap.Geocoder({
            radius: 1000,
            extensions: "all"
        });
        geocoder.getAddress(lnglatXY, function(status, result) {
            if (status === 'complete' && result.info === 'OK') {
                geocoder_CallBack(result);
            }
        });
        var marker = new AMap.Marker({  //加点
            map: map,
            position: lnglatXY
        });
    }
    function geocoder_CallBack(data) {
        var address = data.regeocode.formattedAddress; //返回地址描述
        document.getElementById("result").innerHTML = address;
    }
function getdingwei() {
    map.plugin('AMap.Geolocation', function() {
        geolocation = new AMap.Geolocation({
            enableHighAccuracy: true,//是否使用高精度定位,默认:true
            timeout: 10000,          //超过10秒后停止定位,默认:无穷大
            buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
            zoomToAccuracy: true,      //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
            buttonPosition:'RB'
        });
        map.addControl(geolocation);
        geolocation.getCurrentPosition();
        AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
        AMap.event.addListener(geolocation, 'error', onError);      //返回定位出错信息
    });
}

    /***************************************
     由于Chrome、IOS10等已不再支持非安全域的浏览器定位请求,为保证定位成功率和精度,请尽快升级您的站点到HTTPS。
     ***************************************/
    var map, geolocation;
    //加载地图,调用浏览器定位服务
    map = new AMap.Map('container', {
        resizeEnable: true
    });

    //解析定位结果
    function onComplete(data) {
        lnglatXY = [data.position.getLng(), data.position.getLat()]; //已知点坐标
        regeocoder(lnglatXY);
        var str=['定位成功'];
        str.push('经度:' + data.position.getLng());
        str.push('纬度:' + data.position.getLat());
        if(data.accuracy){
            str.push('精度:' + data.accuracy + ' 米');
        }//如为IP精确定位结果则没有精度信息
        str.push('是否经过偏移:' + (data.isConverted ? '是' : '否'));
        document.getElementById('tip').innerHTML = str.join('<br>');
    }
    //解析定位错误信息
    function onError(data) {
        document.getElementById('tip').innerHTML = '定位失败';
    }
</script>
</body>
</html>

直接点黑色方块就可以获取定位 pc和浏览器都支持

ps:别用谷歌浏览器的那个模拟手机端的机制测试 会定位失败

第二种方法

获取你想要位置的经纬度

代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>Cover Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="__TMPL__/public/assets/css/cover.css" rel="stylesheet">
    <link href="__TMPL__/public/assets/simpleboots/themes/simpleboot3/bootstrap337.min.css" rel="stylesheet">
    <script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=1ad560f64c645eaecdd611ce2facc670"></script>
</head>

<body>
<style>
    .leads:hover{
        border-radius:30px;
    }
    .leads a{
        text-align: center;
        text-decoration: none;
    }
    .leads{
        cursor: pointer;
        padding: 0 45px 0 60px;
        height: 60px;
        line-height: 60px;
        border: 1px solid #c2c2c2;
        border-color: rgba(255,255,255,.3);
        font-size: 24px;
        color: #ccc;
        transition: all .5s;
        -webkit-transition: all .5s;
    }
</style>
<div>
    <div>
        <div>
            <include file="public@sign_head"/>
            <div class="inner cover">
                <h2>{$jobtime}</h2>
                <p>上班时间: {$ba|date="H:i",###}&nbsp;&nbsp;&nbsp;&nbsp;(考勤时间:{$kaoqin[0]})</p>
                <p>下班时间:{$shier|date="H:i",###}  (考勤时间:{$kaoqin[1]})</p>
                <p>上班时间:{$shisanb|date="H:i",###}  (考勤时间:{$kaoqin[2]})</p>
                <p>下班时间:{$wub|date="H:i",###}  (考勤时间:{$kaoqin[3]})</p>
                <br>
                <p class="lead leads">
                    <if condition="$m eq 1">
                        <if condition="$time egt $qib && $time elt $shier">
                            <a class=" jobTime" signUrl="{:url('IndexSign/m_up')}" get-id="{:cmf_get_current_user_id()}" onclick="getdingwei()">上午打卡!</a>
                            <else/>
                            <a class=" jobTime" signUrl="{:url('IndexSign/m_down')}" get-id="{:cmf_get_current_user_id()}" onclick="getdingwei()">上午下班打卡!</a>
                        </if>
                        <else/>
                        <if condition="$time egt $shisan && $time elt $wub">
                            <a class=" jobTime" signUrl="{:url('IndexSign/n_up')}" get-id="{:cmf_get_current_user_id()}" onclick="getdingwei()">下午上班打卡!</a>
                            <else/>
                            <a class=" jobTime" signUrl="{:url('IndexSign/n_down')}" get-id="{:cmf_get_current_user_id()}" onclick="getdingwei()">下午下班打卡!</a>
                        </if>
                    </if>
                </p>
            </div>
            <include file="public@sign_foot"/>
        </div>
    </div>
</div>


<script src="__STATIC__/js/jquery.js"></script>
<script src="__STATIC__/js/layer/layer.js"></script>


<script>
    function getdingwei()
    {
        AMap.plugin('AMap.Geocoder', function() {
            var geocoder = new AMap.Geocoder({
                // city 指定进行编码查询的城市,支持传入城市名、adcode 和 citycode
                city: '共青城市', //全国
                enableHighAccuracy:"true",//准确定位,默认true
                radius: 1000, //范围
                extensions: "all"
            });

            var lnglat = [115.77625, 29.25000]; //获取你想要位置的经纬度

            geocoder.getAddress(lnglat, function(status, result) {
                if (status === 'complete' && result.info === 'OK') {
                    getAddres(result);
                }
            })
        })
    }
    function getAddres  (data) {
        var address = data.regeocode.formattedAddress; //返回地址描述
        var gqkj = address.indexOf("{$address}") != -1;
        if(gqkj == true){
            var url = $('.jobTime').attr('signUrl');
            var data = $('.jobTime').attr('get-id');
            $.ajax({
                url:url,
                data:{"user_id":data,"time":{$time}},
                dataType:"json",
                type:"POST",
                success:function(res){
                    if(res.status==1){
                        layer.alert(res.msg, {title: '友情提示', icon:6, closeBtn: 0}, function(){
                            window.location.reload();
                        });
                    }else{
                        layer.msg(res.msg, {anim:6});
                    }
                }
            });
        }else{
            layer.msg('请在规定范围内签到!您的签到位置是:'+address);
        }
    }
</script>
</body>
</html>


  评论





你还可以输入255

提交
返回顶部