• html定位城市


      1. <html>  
      2. <head>  
      3.     <meta charset="UTF-8" />  
      4.     <title>js 百度 geolocation 定位当前城市</title>  
      5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
      6. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
      7. <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>   
      8. <script src="http://api.map.baidu.com/api?v=2.0&ak=abcs"></script>  
      9. <script type="text/javascript">  
      10.   
      11. var geolocation = new BMap.Geolocation();    
      12. var gc = new BMap.Geocoder();     
      13.   
      14. geolocation.getCurrentPosition( function(r) {   //定位结果对象会传递给r变量  
      15.   
      16.         if(this.getStatus() == BMAP_STATUS_SUCCESS)  
      17.         {  //通过Geolocation类的getStatus()可以判断是否成功定位。  
      18.             var pt = r.point;    
      19.             gc.getLocation(pt, function(rs){    
      20.                 var addComp = rs.addressComponents;    
      21.                 alert(addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber);    
      22.             });  
      23.         }  
      24.         else   
      25.         {  
      26.             //关于状态码    
      27.             //BMAP_STATUS_SUCCESS   检索成功。对应数值“0”。    
      28.             //BMAP_STATUS_CITY_LIST 城市列表。对应数值“1”。    
      29.             //BMAP_STATUS_UNKNOWN_LOCATION  位置结果未知。对应数值“2”。    
      30.             //BMAP_STATUS_UNKNOWN_ROUTE 导航结果未知。对应数值“3”。    
      31.             //BMAP_STATUS_INVALID_KEY   非法密钥。对应数值“4”。    
      32.             //BMAP_STATUS_INVALID_REQUEST   非法请求。对应数值“5”。    
      33.             //BMAP_STATUS_PERMISSION_DENIED 没有权限。对应数值“6”。(自 1.1 新增)    
      34.             //BMAP_STATUS_SERVICE_UNAVAILABLE   服务不可用。对应数值“7”。(自 1.1 新增)    
      35.             //BMAP_STATUS_TIMEOUT   超时。对应数值“8”。(自 1.1 新增)    
      36.             switch( this.getStatus() )  
      37.             {  
      38.                 case 2:  
      39.                     alert( '位置结果未知 获取位置失败.' );  
      40.                 break;  
      41.                 case 3:  
      42.                     alert( '导航结果未知 获取位置失败..' );  
      43.                 break;  
      44.                 case 4:  
      45.                     alert( '非法密钥 获取位置失败.' );  
      46.                 break;  
      47.                 case 5:  
      48.                     alert( '对不起,非法请求位置  获取位置失败.' );  
      49.                 break;  
      50.                 case 6:  
      51.                     alert( '对不起,当前 没有权限 获取位置失败.' );  
      52.                 break;  
      53.                 case 7:  
      54.                     alert( '对不起,服务不可用 获取位置失败.' );  
      55.                 break;  
      56.                 case 8:  
      57.                     alert( '对不起,请求超时 获取位置失败.' );  
      58.                 break;  
      59.                   
      60.             }  
      61.         }          
      62.   
      63.     },  
      64.     {enableHighAccuracy: true}  
      65. )  
      66.   
      67. </script>  
      68.       
      69. </head>  
      70. <body>  
      71.   
      72. </body>  
      73. </html>  

    另附 html5 geolocation 定位( 获取当前经纬度 ):

     
    1. <html>  
    2. <head>  
    3.     <meta charset="UTF-8" />  
    4.     <title>js geolocation 定位当前城市</title>  
    5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    6. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
    7. <script type="text/javascript">  
    8. if (navigator.geolocation)   
    9. {  
    10.     navigator.geolocation.getCurrentPosition(  
    11.           
    12.         function( position ) {    
    13.             show_msg( position );  
    14.          },  
    15.            
    16.         function( err ) {  
    17.                
    18.             switch(error.code)  
    19.             {  
    20.             case 0:  
    21.               alert("尝试获取您的位置信息时发生错误:" + error.message);  
    22.               break;  
    23.             case 1:  
    24.                 alert("用户拒绝了获取位置信息请求。");  
    25.               break;  
    26.             case 2:  
    27.                 alert("浏览器无法获取您的位置信息:" + error.message);  
    28.               break;  
    29.             case 3:  
    30.                 alert("获取您位置信息超时。");  
    31.               break;  
    32.             }  
    33.         }  
    34.     )  
    35. }  
    36. else  
    37.       alert( '您当前使用的浏览器不支持Geolocation服务' );  
    38.         
    39.         
    40. function show_msg( position )  
    41. {  
    42.     var lat = position.coords.latitude;  
    43.     var lng = position.coords.longitude;  
    44.     alert( "您所在的位置: 经度" + lat + ",纬度" + lng );  
    45.       
    46.     if( typeof position.address != "undefined" )  
    47.     {  
    48.         var country = position.address.country;  
    49.         var province = position.address.region;  
    50.         var city = position.address.city;  
    51.         alert(' 您位于 ' + country + province + '省' + city +'市');  
    52.     }  
    53. }  
    54. </script>  
    55.       
    56. </head>  
    57. <body>  
    58.     <id="geo_loc"><p>  
    59. </body>  
    60. </html>  
  • 相关阅读:
    zabbix 监控机器监听的端口 + 触发器 表达式理解
    php关于文件上传的两个配置项说明
    linux cron计划任务防止多个任务同时运行
    php注册自动加载函数
    linux 下查看机器是cpu是几核的
    laravel容器类make方法解释
    laravel php门面模式
    js获取当前页面的url地址
    php编码规范
    laravel console handle 传参方法
  • 原文地址:https://www.cnblogs.com/qjyking/p/5595476.html
Copyright © 2020-2023  润新知