可以在前端实现的几个地理位置小功能

前端之家收集整理的这篇文章主要介绍了可以在前端实现的几个地理位置小功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下文转自:http://www.oncoding.cn/2010/geo-location-frontend

在Smashing Magazine上看到这篇Entering The Wonderful World of Geo Location,介绍了获取并处理用户地理位置的应用和方法, 很有意思。结合原文的内容加上之前的一些应用,整理了几个可以完全在前端实现的地理位置相关小功能

1.通过IP获取用户位置

很多时候需要通过IP判断用户的位置,通常的办法是通过自己的后台程序查询数据库得到。如果用户位置只是应用在前端,或者有其他的特殊原因(比如, 懒:),也有一些其他办法来快速获取用户位置。

maxmind.com提供了一个 服务,通过引入一个js文件(http://j.maxmind.com/app/geoip.js), 可以把他判断到的用户的国家、城市、经纬度等信息加入到页面中来。下面是从青岛访问这个js文件返回的内容

@H_404_42@functiongeoip_country_code() { return'CN'; } functiongeoip_country_name() { return'China'; } functiongeoip_city() { return'Qingdao'; } functiongeoip_region() { return'25'; } functiongeoip_region_name() { return'Shandong'; } functiongeoip_latitude() { return'36.0986'; } functiongeoip_longitude() { return'120.3719'; } functiongeoip_postal_code() { return''; }
1

我们就可以利用这些信息做很多东西了:DEMO

2.W3C共享位置接口

HTML5加入了的贡献地理位置的浏览器API接口,目前firefox3.5等浏览器已经支持这一功能

用法

@H_404_42@// if the browser supports the w3c geo api
01
@H_404_42@if(navigator.geolocation){
02
@H_404_42@ // get the current position
03
@H_404_42@ navigator.geolocation.getCurrentPosition(
04
@H_404_42@
05
@H_404_42@ // if this was successful,get the latitude and longitude
06
@H_404_42@ function(position){
07
@H_404_42@ varlat = position.coords.latitude;
08
@H_404_42@ varlon = position.coords.longitude;
09
@H_404_42@ },
10
@H_404_42@ // if there was an error
11
@H_404_42@ function(error){
12
@H_404_42@ alert('ouch');
13
@H_404_42@ });
14
@H_404_42@}
15

DEMO

3. Google Gears 的 Geolocation API

Google Gears是主 要提供本地存储功能的浏览器扩展,新版本的Gears中,加入了判断用户地理位置的API,通过IP、WIFI热点等判断位置。不过Gears的使用似乎 并不太广泛(Chrome内置了Gears,其他浏览器需要安装),国内的地理位置信息也不够丰富,所以这个应用目前只可作为参考。

使用Gears的基本方法这里,引 入gears_init.js,使用Geolocation API的程序为:

@H_404_42@vargeo = factory.create('beta.geolocation'); //创建geolocation对象
1
@H_404_42@varokCallback = function(d){
2
@H_404_42@ alert('当前位置(纬度,经度): '+ d.latitude + ','+ d.longitude);
3
@H_404_42@};
4
@H_404_42@varerrorCallback = function(err){
5
@H_404_42@ alert(err.message);
6
@H_404_42@};
7
@H_404_42@geo.getCurrentPosition(okCallback,errorCallback);
8

更多内容参考这篇文章使用Gears获取当前地理位置,以及DEMO

4.逆经纬度解析

通过浏览器API等方式得到经纬度后,有时需要得到对应的城市名,这就是逆经纬度解析。

google maps api提供了一些经纬度解析方法,如果我们不想引入庞大的api,可以直接使用他的请求地址,通过jsonp方式得到google的经纬度解析数据。jsonp请求地址形式为:

http://ditu.google.cn/maps/geo?
output=json&oe=utf-8&q=纬度%2C经度
&key=你申请到的key
& mapclient=jsapi&hl=zh-CN&callback=myfunction

参数q为经纬度,参数callback为回调函数名。

可以看下这 个地址的返回结果,数据非常丰富,并且还是中文的:

Yahoo提供的接口

雅虎提供了一些经纬度查询接口,可以使用YQL查询

查询语句为:

@H_404_42@select* fromflickr.places wherelat=36.06023 andlon=120.3024
1

用法

@H_404_42@<script type="text/javascript"charset="utf-8">
01
@H_404_42@functiongetPlaceFromFlickr(lat,lon,callback){
02
@H_404_42@ // the YQL statement
03
@H_404_42@ varyql = 'select * from flickr.places where lat='+lat+' and lon='+lon;
04
@H_404_42@
05
@H_404_42@ // assembling the YQL webservice API
06
@H_404_42@ encodeURIComponent(yql)+'&format=json&diagnostics='+
08
@H_404_42@ 'false&callback='+callback;
09
@H_404_42@
10
@H_404_42@ // create a new script node and add it to the document
11
@H_404_42@ vars = document.createElement('script');
12
@H_404_42@ s.setAttribute('src',url);
13
@H_404_42@ document.getElementsByTagName('head')[0].appendChild(s);
14
@H_404_42@};
15
@H_404_42@
16
@H_404_42@// callback in case there is a place found
17
@H_404_42@functionoutput(o){
18
@H_404_42@ if(typeof(o.query.results.places.place) != 'undefined'){
19
@H_404_42@ alert(o.query.results.places.place.name);
20
@H_404_42@ }
21
@H_404_42@}
22
@H_404_42@
23
@H_404_42@// call the function with my current lat/lon
24
@H_404_42@getPlaceFromFlickr(36.6692270,117.0198960,'output');
25
@H_404_42@</script>
26

5.经纬度解析

经纬度解析就是通过地名取得经纬度数据,同样利用google那个请求地址,可以实现这一功能,请求地址格式为:

http://ditu.google.cn/maps/geo?output=json&oe=utf-8
& q=地名的url encode编码
&key=你申请到的key
& mapclient=jsapi&hl=zh-CN&callback=myfunction

参数q为encodeURI(“中国山东省青岛市市北区”),callback是jsonp回调函数名。

举 个例子,返回结果:

6.google maps 图片接口

有时候我们只想展示简单的地图,不需要交互和拖动,可以通过google maps提供的静态地图API引入动态生成的地图图片,不过需要为你的域名申请一个key。

引入图片的url格式为:

@H_404_42@http://maps.google.com/maps/api/staticmap?
01
@H_404_42@
02
@H_404_42@sensor=false
03
@H_404_42@&size=200x200
04
@H_404_42@&maptype=roadmap
05
@H_404_42@&key=<em>你申请到的KEY</em>
06
@H_404_42@&markers=color:blue|label:1|37.4447,-122.161
07
@H_404_42@&markers=color:red|label:2|37.3385,-121.886
08
@H_404_42@&markers=color:green|label:3|37.3716,-122.038
09
@H_404_42@&markers=color:yellow|label:4|37.7792,-122.42
10

得到图片

后记

关于地图的更多操作,可以参见以前的这篇文章Google Maps Api介绍与基础操作

随着互联网的发展,越来越多的功能可以在前端实现,出现了越来越多的强大的第三方服务,我们可以很方便的在我们的网站上加入一些实用的功能。这也许 就是web2.0的魅力吧。

猜你在找的Json相关文章