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" }