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>