zoukankan      html  css  js  c++  java
  • 百度地图获取位置

    <!DOCTYPE html>  
    <html>  
    <head>  
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    <title>Hello, World</title>  
    <style type="text/css">  
    html{height:100%}  
    body{height:100%;margin:0px;padding:0px}  
    #container{height:100%}  
    </style>  
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.4"></script>
    </head>  
     
    <body>  
    
    <div id="allmap"></div> 
    <div id="input" style="height: 100px;"></div>
    <script type="text/javascript"> 
    
        // 百度地图API功能
        var input = document.getElementById("input");
        getLocationText(function(text){
            input.innerHTML = text;
        })
    
        function getLocationText(fn){
    
            var map = new BMap.Map("allmap");
    
            var point = new BMap.Point(116.331398,39.897445);
    
            map.centerAndZoom(point,20);
    
            var geolocation = new BMap.Geolocation();
            
            var geoc = new BMap.Geocoder();
    
            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);
    
                    point = new BMap.Point(r.point.lng,r.point.lat);
    
                    geoc.getLocation(point, function(rs){
                        var addComp = rs.addressComponents;
                        var location = addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber;
    
                        fn(location);
                    });
                }
                else {
                    alert('failed'+this.getStatus());
                }        
            },{enableHighAccuracy: true});
        }
    
              
    
    
    </script>  
    </body>  
    </html>
  • 相关阅读:
    成为明星程序员的10个提示
    使用命令时一些快捷的方法
    mysql字符串截取
    MFGTool2批量操作
    busybox microcom Segmentation fault
    Linux 定制X86平台操作系统
    Buildroot MariaDB替代MySQL
    arcotg_udc: exports duplicate symbol imx_usb_create_charger (owned by kernel)
    create newline in Github Bio
    BusyBox ifup udhcpc后台运行
  • 原文地址:https://www.cnblogs.com/dexin/p/6489968.html
Copyright © 2011-2022 走看看