转载--geolocation and get the address by Google Map api

link: 

http://blog.wolftankk.com/2011/10/24/geolocation-and-get-the-address-by-google-map-api/


在移动设备以及现代浏览器中可以直接同navigator.geolocation所提供的api, 获取你当前经纬度. 并且通过GoogleMapAPI, 查看你当前所在位置的地图.

一. 在客户端上获取经纬度.

获取经纬度有两个api: getCurrentPosition以及watchPosition. 从本质上来说都是获取你当前所在的位置.

getCurrentPosition(callback)

watchPosition(successCallback, failureCallback, options)

在stackoverflow上找到了一篇讨论两者不同的论题. http://stackoverflow.com/questions/1948952/watchposition-vs-getcurrentposition-w-settimeout

After some serious testing, I have verified watchPosition() will give you an accurate location much more quickly than getCurrentPostion() over and over again. When using watchPostion(), the map behaves poorly if you redraw it over and over again every time the device updates your location. To get around this, I have added a listener to the tilesloaded event, which allows me to only redraw the map if there is not already a thread trying to draw on the map. Once the user is happy with the determined location, I will clear the watch. This will get me the best of both worlds, as far as battery consumption and accuracy are concerned.

这个就看你个人喜欢用哪个api了, 我推荐后者watchPosition. 首先写一个页面用来获取经纬度

<html>
<head>
<title>Get position by geolocation</title>
</head>
<body>
<span id="coords"></span>
<script type="text/javascript">
// <![CDATA[
var geo = navigator.geolocation;
geo.watchPosition(function(position){
   var coords = position.coords;
   var latitude = coords.latitude, longitude = coords.longitude;
 
   document.getElementByID("coords").innerHTML = "latitude: " + latitude + ", longitude: " + longitude;
}, function(error){
 throw error;
}, {enableHighAccuracy:true, maximumAge: 30000, timeout: 27000})
// ]]>
</script>
</body>
</html>


二 使用GoogleMapAPI获取地址你打开网页的时候就能看到网页所取得的经纬度..

http://maps.googleapis.com/maps/api/geocode/json?latlng=31.2399484,121.3791791&sensor=true&language=zh-CN

将会返回:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "834号-970号",
               "short_name" : "834号-970号",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "真光路",
               "short_name" : "真光路",
               "types" : [ "route" ]
            },
            {
               "long_name" : "普陀区",
               "short_name" : "普陀区",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "上海",
               "short_name" : "上海",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "上海市",
               "short_name" : "上海市",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "中国",
               "short_name" : "CN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "中国上海市普陀区真光路834号-970号",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 31.24009389999999,
                  "lng" : 121.37915450
               },
               "southwest" : {
                  "lat" : 31.23713110,
                  "lng" : 121.37654240
               }
            },
            "location" : {
               "lat" : 31.24000040,
               "lng" : 121.37909690
            },
            "location_type" : "RANGE_INTERPOLATED",
            "viewport" : {
               "northeast" : {
                  "lat" : 31.24009389999999,
                  "lng" : 121.3791974302915
               },
               "southwest" : {
                  "lat" : 31.23713110,
                  "lng" : 121.3764994697085
               }
            }
         },
         "types" : [ "street_address" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "普陀区",
               "short_name" : "普陀区",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "上海",
               "short_name" : "上海",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "上海市",
               "short_name" : "上海市",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "中国",
               "short_name" : "CN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "中国上海市普陀区",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 31.30288520,
                  "lng" : 121.45146710
               },
               "southwest" : {
                  "lat" : 31.21827589999999,
                  "lng" : 121.32606960
               }
            },
            "location" : {
               "lat" : 31.2495540,
               "lng" : 121.3964010
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 31.30288520,
                  "lng" : 121.45146710
               },
               "southwest" : {
                  "lat" : 31.21827589999999,
                  "lng" : 121.32606960
               }
            }
         },
         "types" : [ "sublocality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "上海",
               "short_name" : "上海",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "上海市",
               "short_name" : "上海市",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "中国",
               "short_name" : "CN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "中国上海市",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 31.8682170,
                  "lng" : 122.24706630
               },
               "southwest" : {
                  "lat" : 30.680270,
                  "lng" : 120.85821750
               }
            },
            "location" : {
               "lat" : 31.2303930,
               "lng" : 121.4737040
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 31.66889670,
                  "lng" : 122.11379890
               },
               "southwest" : {
                  "lat" : 30.77980120,
                  "lng" : 120.83970670
               }
            }
         },
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "上海市",
               "short_name" : "上海市",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "中国",
               "short_name" : "CN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "中国上海市",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 31.8682170,
                  "lng" : 122.24706630
               },
               "southwest" : {
                  "lat" : 30.680270,
                  "lng" : 120.85821750
               }
            },
            "location" : {
               "lat" : 31.2303930,
               "lng" : 121.4737040
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 31.8682170,
                  "lng" : 122.24706630
               },
               "southwest" : {
                  "lat" : 30.680270,
                  "lng" : 120.85821750
               }
            }
         },
         "types" : [ "administrative_area_level_1", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "中国",
               "short_name" : "CN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "中国",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 53.56097399999999,
                  "lng" : 134.772810
               },
               "southwest" : {
                  "lat" : 18.15352160,
                  "lng" : 73.49941369999999
               }
            },
            "location" : {
               "lat" : 35.861660,
               "lng" : 104.1953970
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 53.56097399999999,
                  "lng" : 134.772810
               },
               "southwest" : {
                  "lat" : 18.15352160,
                  "lng" : 73.49941369999999
               }
            }
         },
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值