根据经纬度获取百度地图的详细地址信息

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestLocation.aspx.cs" Inherits="TestLocation" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 8     <title></title>
 9     <script src="js/jquery-1.9.1.js"></script> 
10     <script> 
11         if (window.navigator.geolocation) {
12             var options = {
13                 enableHighAccuracy: true,
14             };
15             window.navigator.geolocation.getCurrentPosition(handleSuccess, handleError, options);
16         } else {
17             alert("浏览器不支持html5来获取地理位置信息");
18         }
19 
20         function handleSuccess(position) {
21             // 获取到当前位置经纬度  本例中是chrome浏览器取到的是google地图中的经纬度
22             var lng = position.coords.longitude;
23             var lat = position.coords.latitude;
24             //alert(lng + "," + lat);
25             $("#allmap").html(lng + "," + lat); //121.37460118242566,31.235583274897206
26             show(lng,lat); 
27         }
28         
29         function handleError(error) {//获取经纬度失败调用函数 如:用户拒绝授权
30 
31         } 
32         function show(lng, lat) {
33             
34             $.ajax({
35                 url: "http://api.map.baidu.com/geoconv/v1/",    //请求的url地址   将获取的经纬度转换为百度地图的经纬度,不转会有误差
36                 dataType: "jsonp",   //返回格式为json
37                 async: true, //请求是否异步,默认为异步,这也是ajax重要特性
38                 data: { "coords": lng + "," + lat, "from": "1", "to": "5", "ak": "申请的百度秘钥" },//参数值coords:纬度,精度
39                 type: "GET",   //请求方式
40                 beforeSend: function () {
41                     //请求前的处理
42                 },
43                 success: function (req) {
44                     //请求成功时处理   
45                     var lan_lon = req.result[0].y + "," + req.result[0].x;//精度,纬度 
46                     $.ajax({
47                         url: "http://api.map.baidu.com/geocoder/v2/",    //请求的url地址    
48                         dataType: "jsonp",   //返回格式为json
49                         async: true, //请求是否异步,默认为异步,这也是ajax重要特性
50                         data: { location: lan_lon, pois: 1, ak: "申请的百度秘钥",output:"json" },    //参数值
51                         type: "GET",   //请求方式
52                         beforeSend: function () {
53                             //请求前的处理
54                         },
55                         success: function (req) {
56                             //请求成功时处理  
57                             alert(req.result.formatted_address);
58                         },
59                         complete: function () {
60                             //请求完成的处理
61                         },
62                         error: function () {
63                             //请求出错处理
64                         }
65                     });
66                 },
67                 complete: function () {
68                     //请求完成的处理
69                 },
70                 error: function () {
71                     //请求出错处理
72                 }
73             });
74         }
75 
76 
77         
78            
79     </script>
80 </head>
81 <body>
82     <form id="form1" runat="server">
83    <div id="allmap"></div>
84     </form>
85 </body>
86 </html>

 

转载于:https://www.cnblogs.com/lvxiaowen/p/5282898.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值