使用百度地图定位当前位置(转载)

<!DOCTYPE html>
<html>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <style type="text/css">
            body,
            html {
                width: 100%;
                height: 100%;
                margin: 0;
                font-family: "微软雅黑";
            }
            
            #l-map {
                height: 220px;
                width: 100%;
            }
            
            #r-result,
            #r-result table {
                width: 100%;
            }
            
            .nav {
                width: 100%;
                height: 2em;
                line-height: 2em;
                background: #EDEDED;
                border: 1px solid #ADADAD;
            }
            
            .nav .nav-inner {
                width: 30%;
                margin-left: 35%;
            }
            
            .nav .nav-sub {
                float: left;
                width: 33%;
            }
            
            .nav .nav-sub a {
                text-decoration: none;
            }
            
            .nav .nav-sub a i {
                display: inline-block;
                background: url("http://webmap1.map.bdstatic.com/wolfman/static/common/images/ui3/mo_banner_ba37b5d.png")
            }
            
            .nav .nav-sub a.bus i {
                background-position: -1px -192px;
                position: relative;
                top: 2px;
                width: 13px;
                height: 16px;
            }
            
            .nav .nav-sub a.driver i {
                background-position: -29px -194px;
                width: 15px;
                height: 14px;
            }
            
            .nav .nav-sub a.walk i {
                background-position: -102px -189px;
                width: 16px;
                height: 18px;
            }
            
            .nav .nav-sub a.bus.cur i {
                background-position: -15px -192px;
            }
            
            .nav .nav-sub a.driver.cur i {
                background-position: -45px -194px;
            }
            
            .nav .nav-sub a.walk.cur i {
                background-position: -120px -189px;
            }
            
            .hide {
                display: none;
            }
            
            input {
                font-family: "micrsoft yahei";
                width: 80%;
                height: 2em;
                font-size: 1em;
                line-height: 2em;
                border: 0px;
                outline: 0px;
                padding: .2em 1em;
                margin: 0em 10%;
            }
            
            .btn-group {
                width: 100%;
                border-top: 1px solid #DDD;
                border-bottom: 2px solid #DDD;
            }
            
            button {
                width: 32%;
                text-align: center;
                border: 0;
                border-radius: 0;
                background-color: inherit;
                height: 44px;
                line-height: 44px;
                font-size: 15px;
            }
        </style>
        <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你的key"></script>
        <script type="text/javascript" src="http://libs.baidu.com/jquery/1.8.2/jquery.min.js"></script>
        <title>导航示例</title>
    </head>

    <body>
        <div id="search">
            <input type="text" id="start" placeholder="正在定位您的位置..." style="border-bottom: 1px solid #DDD; " />
            <input type="text" id="end" value="汽车西站-公交车站" readonly="true" />
            <input type="hidden" id="start_point" value="" />
            <input type="hidden" id="end_point" value="112.918571,28.214124" />
            <input type="hidden" id="start_location" value="" />
            <div class="btn-group">
                <button id="bus-search">公交</button>
                <button id="driver-search">驾车</button>
                <button id="walk-search">步行</button>
            </div>
        </div>
        <div id="showMap" class="hide">
            <div class="nav">
                <div class="nav-inner">
                    <div class="nav-sub">
                        <a href="#" class="bus"><i></i></a>
                    </div>
                    <div class="nav-sub">
                        <a href="#" class="driver cur"><i></i></a>
                    </div>
                    <div class="nav-sub">
                        <a href="#" class="walk"><i></i></a>
                    </div>
                </div>
                <!-- <a href="javascript:;" id="reLocation">重新导航</a> -->
            </div>
            <div id="l-map"></div>
            <div id="r-result"></div>
        </div>

        <script type="text/javascript">
            $(function() {


                // 定位对象
                var geoc = new BMap.Geocoder();
                var geolocation = new BMap.Geolocation();
                geolocation.getCurrentPosition(function(r) {
                    if(this.getStatus() == BMAP_STATUS_SUCCESS) {
                        //var mk = new BMap.Marker(r.point);
                        //map.addOverlay(mk);
                        //map.panTo(r.point);
                        alert(r.point.lng + ',' + r.point.lat);
                        $("#start_point").val(r.point.lng + ',' + r.point.lat);
                        setLocation(r.point);
                        showMap();

                    } else {
                        $("#start").attr("placeholder", "请输入您的当前位置")
                        alert('无法定位到您的当前位置,导航失败,请手动输入您的当前位置!' + this.getStatus());
                    }
                }, { enableHighAccuracy: true });

                function setLocation(point) {
                    geoc.getLocation(point, function(rs) {
                        var addComp = rs.addressComponents;
                        var result = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
                        $("#start").val(result);
                        $("#start_location").val(result);
                    });
                }
            })
        </script>
    </body>

</html>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值