前端之家收集整理的这篇文章主要介绍了
可以在前端实现的几个地理位置小功能,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
下文转自:http://www.oncoding.cn/2010/geo-location-frontend
在Smashing Magazine上看到这篇Entering The Wonderful World of Geo Location,介绍了获取并处理用户地理位置的应用和方法, 很有意思。结合原文的内容,加上之前的一些应用,整理了几个可以完全在前端实现的地理位置相关小功能。
很多时候需要通过IP判断用户的位置,通常的办法是通过自己的后台程序查询数据库得到。如果用户位置只是应用在前端,或者有其他的特殊原因(比如, 懒:),也有一些其他办法来快速的获取用户位置。
maxmind.com提供了一个 服务,通过引入一个js文件(http://j.maxmind.com/app/geoip.js), 可以把他判断到的用户的国家、城市、经纬度等信息加入到页面中来。下面是从青岛访问这个js文件返回的内容:
1 |
@H_404_42@function
geoip_country_code() {
return
'CN'
; }
function
geoip_country_name() {
return
'China'
; }
function
geoip_city() {
return
'Qingdao'
; }
function
geoip_region() {
return
'25'
; }
function
geoip_region_name() {
return
'Shandong'
; }
function
geoip_latitude() {
return
'36.0986'
; }
function
geoip_longitude() {
return
'120.3719'
; }
function
geoip_postal_code() {
return
''
; }
我们就可以利用这些信息做很多东西了:DEMO
2.W3C共享位置接口
HTML5加入了的贡献地理位置的浏览器API接口,目前firefox3.5等浏览器已经支持这一功能。
用法:
02 |
@H_404_42@if
(navigator.geolocation){
04 |
@H_404_42@
navigator.geolocation.getCurrentPosition(
07 |
@H_404_42@
function
(position){
08 |
@H_404_42@
var
lat = position.coords.latitude;
09 |
@H_404_42@
var
lon = position.coords.longitude;
12 |
@H_404_42@
function
(error){
13 |
@H_404_42@
alert(
'ouch'
);
看 DEMO
3. Google Gears 的 Geolocation API
Google Gears是主 要提供本地存储功能的浏览器扩展,新版本的Gears中,加入了判断用户地理位置的API,通过IP、WIFI热点等判断位置。不过Gears的使用似乎 并不太广泛(Chrome内置了Gears,其他浏览器需要安装),国内的地理位置信息也不够丰富,所以这个应用目前只可作为参考。
使用Gears的基本方法看这里,引 入gears_init.js,使用Geolocation API的程序为:
1 |
@H_404_42@var
geo = factory.create(
'beta.geolocation'
);
2 |
@H_404_42@var
okCallback =
function
(d){
3 |
@H_404_42@
alert(
'当前位置(纬度,经度): '
+ d.latitude +
','
+ d.longitude);
5 |
@H_404_42@var
errorCallback =
function
(err){
6 |
@H_404_42@
alert(err.message);
8 |
@H_404_42@geo.getCurrentPosition(okCallback,errorCallback);
更多内容参考这篇文章:使用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为回调函数名。
可以看下这 个地址的返回结果,数据非常丰富,并且还是中文的:
001 |
@H_404_42@myfunction && myfunction( {
002 |
@H_404_42@
"name"
:
"36.06023,120.3024"
,
003 |
@H_404_42@
"Status"
: {
004 |
@H_404_42@
"code"
: 200,
005 |
@H_404_42@
"request"
:
"geocode"
007 |
@H_404_42@
"Placemark"
: [ {
008 |
@H_404_42@
"id"
:
"p1"
,
009 |
@H_404_42@
"address"
:
"中国山东省青岛市市南区台西三路6号-10号"
,
010 |
@H_404_42@
"AddressDetails"
: {
011 |
@H_404_42@
"Accuracy"
: 8,
012 |
@H_404_42@
"Country"
: {
013 |
@H_404_42@
"AdministrativeArea"
: {
014 |
@H_404_42@
"AdministrativeAreaName"
:
"山东省"
,
015 |
@H_404_42@
"Locality"
: {
016 |
@H_404_42@
"DependentLocality"
: {
017 |
@H_404_42@
"DependentLocalityName"
:
"市南区"
,
018 |
@H_404_42@
"Thoroughfare"
: {
019 |
@H_404_42@
"ThoroughfareName"
:
"台西三路6号-10号"
022 |
@H_404_42@
"LocalityName"
:
"青岛市"
025 |
@H_404_42@
"CountryName"
:
"中国"
,
026 |
@H_404_42@
"CountryNameCode"
:
"CN"
029 |
@H_404_42@
"ExtendedData"
: {
031 |
@H_404_42@
"north"
: 36.0633254,
032 |
@H_404_42@
"south"
: 36.0570301,
033 |
@H_404_42@
"east"
: 120.3054361,
034 |
@H_404_42@
"west"
: 120.2991409
037 |
@H_404_42@
"Point"
: {
038 |
@H_404_42@
"coordinates"
: [ 120.3024027,36.0602271,0 ]
041 |
@H_404_42@
"id"
:
"p2"
,
042 |
@H_404_42@
"address"
:
"中国山东省青岛市市南区台西三路8号阿双美容美发厅"
,
043 |
@H_404_42@
"AddressDetails"
: {
044 |
@H_404_42@
"Accuracy"
: 9,
045 |
@H_404_42@
"Country"
: {
046 |
@H_404_42@
"AdministrativeArea"
: {
047 |
@H_404_42@
"AdministrativeAreaName"
:
"山东省"
,
048 |
@H_404_42@
"Locality"
: {
049 |
@H_404_42@
"DependentLocality"
: {
050 |
@H_404_42@
"AddressLine"
: [
"阿双 美容美发厅"
],
051 |
@H_404_42@
"DependentLocalityName"
:
"市南区"
,
052 |
@H_404_42@
"Thoroughfare"
: {
053 |
@H_404_42@
"ThoroughfareName"
:
"台西三路8号"
056 |
@H_404_42@
"LocalityName"
:
"青岛市"
059 |
@H_404_42@
"CountryName"
:
"中国"
,
060 |
@H_404_42@
"CountryNameCode"
:
"CN"
063 |
@H_404_42@
"ExtendedData"
: {
065 |
@H_404_42@
"north"
: 36.0632366,
066 |
@H_404_42@
"south"
: 36.0569414,
067 |
@H_404_42@
"east"
: 120.3055696,
068 |
@H_404_42@
"west"
: 120.2992744
071 |
@H_404_42@
"Point"
: {
072 |
@H_404_42@
"coordinates"
: [ 120.3024220,36.0600890,0 ]
075 |
@H_404_42@
"id"
:
"p3"
,
076 |
@H_404_42@
"address"
:
"中国青岛市市南区台西四路站"
,
077 |
@H_404_42@
"AddressDetails"
: {
078 |
@H_404_42@
"Accuracy"
: 9,
079 |
@H_404_42@
"AddressLine"
: [
"台西四路站"
]
081 |
@H_404_42@
"ExtendedData"
: {
083 |
@H_404_42@
"north"
: 36.0630636,
084 |
@H_404_42@
"south"
: 36.0567684,
085 |
@H_404_42@
"east"
: 120.3063986,
086 |
@H_404_42@
"west"
: 120.3001034
089 |
@H_404_42@
"Point"
: {
090 |
@H_404_42@
"coordinates"
: [ 120.3032510,36.0599160,0 ]
093 |
@H_404_42@
"id"
:
"p4"
,
094 |
@H_404_42@
"address"
:
"中国青岛市市南区云南路(台西四路口)站"
,
095 |
@H_404_42@
"AddressDetails"
: {
096 |
@H_404_42@
"Accuracy"
: 9,
097 |
@H_404_42@
"AddressLine"
: [
"云南路(台西四路口)站"
]
099 |
@H_404_42@
"ExtendedData"
: {
101 |
@H_404_42@
"north"
: 36.0643586,
102 |
@H_404_42@
"south"
: 36.0580634,
103 |
@H_404_42@
"east"
: 120.3073456,
104 |
@H_404_42@
"west"
: 120.3010504
107 |
@H_404_42@
"Point"
: {
108 |
@H_404_42@
"coordinates"
: [ 120.3041980,36.0612110,0 ]
111 |
@H_404_42@
"id"
:
"p5"
,
112 |
@H_404_42@
"address"
:
"中国青岛市市南区贵州路站"
,
113 |
@H_404_42@
"AddressDetails"
: {
114 |
@H_404_42@
"Accuracy"
: 9,
115 |
@H_404_42@
"AddressLine"
: [
"贵州路站"
]
117 |
@H_404_42@
"ExtendedData"
: {
119 |
@H_404_42@
"north"
: 36.0614856,
120 |
@H_404_42@
"south"
: 36.0551904,
121 |
@H_404_42@
"east"
: 120.3036956,
122 |
@H_404_42@
"west"
: 120.2974004
125 |
@H_404_42@
"Point"
: {
126 |
@H_404_42@
"coordinates"
: [ 120.3005480,36.0583380,0 ]
129 |
@H_404_42@
"id"
:
"p6"
,
130 |
@H_404_42@
"address"
:
"中国青岛市市南区团岛站"
,
131 |
@H_404_42@
"AddressDetails"
: {
132 |
@H_404_42@
"Accuracy"
: 9,
133 |
@H_404_42@
"AddressLine"
: [
"团岛站"
]
135 |
@H_404_42@
"ExtendedData"
: {
137 |
@H_404_42@
"north"
: 36.0629146,
138 |
@H_404_42@
"south"
: 36.0566194,
139 |
@H_404_42@
"east"
: 120.3022496,
140 |
@H_404_42@
"west"
: 120.2959544
143 |
@H_404_42@
"Point"
: {
144 |
@H_404_42@
"coordinates"
: [ 120.2991020,36.0597670,0 ]
147 |
@H_404_42@
"id"
:
"p7"
,
148 |
@H_404_42@
"address"
:
"中国山东省青岛市市南区团岛四路海湾花园"
,
149 |
@H_404_42@
"AddressDetails"
: {
150 |
@H_404_42@
"Accuracy"
: 9,
151 |
@H_404_42@
"Country"
: {
152 |
@H_404_42@
"AdministrativeArea"
: {
153 |
@H_404_42@
"AdministrativeAreaName"
:
"山东省"
,
154 |
@H_404_42@
"Locality"
: {
155 |
@H_404_42@
"DependentLocality"
: {
156 |
@H_404_42@
"AddressLine"
: [
"海湾 花园"
],
157 |
@H_404_42@
"DependentLocalityName"
:
"市南区"
,
158 |
@H_404_42@
"Thoroughfare"
: {
159 |
@H_404_42@
"ThoroughfareName"
:
"团岛四 路"
162 |
@H_404_42@
"LocalityName"
:
"青岛市"
165 |
@H_404_42@
"CountryName"
:
"中国"
,
166 |
@H_404_42@
"CountryNameCode"
:
"CN"
169 |
@H_404_42@
"ExtendedData"
: {
171 |
@H_404_42@
"north"
: 36.0642706,
172 |
@H_404_42@
"south"
: 36.0579754,
173 |
@H_404_42@
"east"
: 120.3006386,
174 |
@H_404_42@
"west"
: 120.2943434
177 |
@H_404_42@
"Point"
: {
178 |
@H_404_42@
"coordinates"
: [ 120.2974910,36.0611230,0 ]
181 |
@H_404_42@
"id"
:
"p8"
,
182 |
@H_404_42@
"address"
:
"中国山东省青岛市市南区"
,
183 |
@H_404_42@
"AddressDetails"
: {
184 |
@H_404_42@
"Accuracy"
: 4,
185 |
@H_404_42@
"Country"
: {
186 |
@H_404_42@
"AdministrativeArea"
: {
187 |
@H_404_42@
"AdministrativeAreaName"
:
"山东省"
,
188 |
@H_404_42@
"Locality"
: {
189 |
@H_404_42@
"DependentLocality"
: {
190 |
@H_404_42@
"DependentLocalityName"
:
"市南区"
192 |
@H_404_42@
"LocalityName"
:
"青岛市"
195 |
@H_404_42@
"CountryName"
:
"中国"
,
196 |
@H_404_42@
"CountryNameCode"
:
"CN"
199 |
@H_404_42@
"ExtendedData"
: {
201 |
@H_404_42@
"north"
: 36.0954205,
202 |
@H_404_42@
"south"
: 36.0413849,
203 |
@H_404_42@
"east"
: 120.4266629,
204 |
@H_404_42@
"west"
: 120.2858189
207 |
@H_404_42@
"Point"
: {
208 |
@H_404_42@
"coordinates"
: [ 120.3877350,36.0667110,0 ]
211 |
@H_404_42@
"id"
:
"p9"
,
212 |
@H_404_42@
"address"
:
"中国山东省青岛市"
,
213 |
@H_404_42@
"AddressDetails"
: {
214 |
@H_404_42@
"Accuracy"
: 4,
215 |
@H_404_42@
"Country"
: {
216 |
@H_404_42@
"AdministrativeArea"
: {
217 |
@H_404_42@
"AdministrativeAreaName"
:
"山东省"
,
218 |
@H_404_42@
"Locality"
: {
219 |
@H_404_42@
"LocalityName"
:
"青岛市"
222 |
@H_404_42@
"CountryName"
:
"中国"
,
223 |
@H_404_42@
"CountryNameCode"
:
"CN"
226 |
@H_404_42@
"ExtendedData"
: {
228 |
@H_404_42@
"north"
: 36.3313685,
229 |
@H_404_42@
"south"
: 35.9407727,
230 |
@H_404_42@
"east"
: 120.6326294,
231 |
@H_404_42@
"west"
: 120.0970459
234 |
@H_404_42@
"Point"
: {
235 |
@H_404_42@
"coordinates"
: [ 120.3827710,36.0663480,0 ]
238 |
@H_404_42@
"id"
:
"p10"
,
239 |
@H_404_42@
"address"
:
"中国山东省"
,
240 |
@H_404_42@
"AddressDetails"
: {
241 |
@H_404_42@
"Accuracy"
: 2,
242 |
@H_404_42@
"Country"
: {
243 |
@H_404_42@
"AdministrativeArea"
: {
244 |
@H_404_42@
"AdministrativeAreaName"
:
"山东省"
246 |
@H_404_42@
"CountryName"
:
"中国"
,
247 |
@H_404_42@
"CountryNameCode"
:
"CN"
250 |
@H_404_42@
"ExtendedData"
: {
252 |
@H_404_42@
"north"
: 38.4055838,
253 |
@H_404_42@
"south"
: 34.3851760,
254 |
@H_404_42@
"east"
: 122.7159599,
255 |
@H_404_42@
"west"
: 114.8033683
258 |
@H_404_42@
"Point"
: {
259 |
@H_404_42@
"coordinates"
: [ 117.0198960,36.6692270,0 ]
Yahoo提供的接口
雅虎提供了一些经纬度查询接口,可以使用YQL查询。
查询语句为:
1 |
@H_404_42@select
*
from
flickr.places
where
lat=36.06023
and
lon=120.3024
用法:
01 |
@H_404_42@<script type=
"text/javascript"
charset=
"utf-8"
>
02 |
@H_404_42@
function
getPlaceFromFlickr(lat,lon,callback){
04 |
@H_404_42@
var
yql =
'select * from flickr.places where lat='
+lat+
' and lon='
+lon;
08 |
@H_404_42@
encodeURIComponent(yql)+
'&format=json&diagnostics='
+
09 |
@H_404_42@
'false&callback='
+callback;
12 |
@H_404_42@
var
s = document.createElement(
'script'
);
13 |
@H_404_42@
s.setAttribute(
'src'
,url);
14 |
@H_404_42@
document.getElementsByTagName(
'head'
)[0].appendChild(s);
18 |
@H_404_42@
function
output(o){
19 |
@H_404_42@
if
(
typeof
(o.query.results.places.place) !=
'undefined'
){
20 |
@H_404_42@
alert(o.query.results.places.place.name);
25 |
@H_404_42@
getPlaceFromFlickr(36.6692270,117.0198960,
'output'
);
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回调函数名。
举 个例子,返回结果:
01 |
@H_404_42@myfunction && myfunction( {
02 |
@H_404_42@
"name"
:
"中国山东省青岛市市北区"
,
03 |
@H_404_42@
"Status"
: {
04 |
@H_404_42@
"code"
: 200,
05 |
@H_404_42@
"request"
:
"geocode"
07 |
@H_404_42@
"Placemark"
: [ {
08 |
@H_404_42@
"id"
:
"p1"
,
09 |
@H_404_42@
"address"
:
"中国山东省青岛市市北区"
,
10 |
@H_404_42@
"AddressDetails"
: {
11 |
@H_404_42@
"Accuracy"
: 4,
12 |
@H_404_42@
"Country"
: {
13 |
@H_404_42@
"AdministrativeArea"
: {
14 |
@H_404_42@
"AdministrativeAreaName"
:
"山东省"
,
15 |
@H_404_42@
"Locality"
: {
16 |
@H_404_42@
"DependentLocality"
: {
17 |
@H_404_42@
"DependentLocalityName"
:
"市北区"
19 |
@H_404_42@
"LocalityName"
:
"青岛市"
22 |
@H_404_42@
"CountryName"
:
"中国"
,
23 |
@H_404_42@
"CountryNameCode"
:
"CN"
26 |
@H_404_42@
"ExtendedData"
: {
28 |
@H_404_42@
"north"
: 36.1237216,
29 |
@H_404_42@
"south"
: 36.0515859,
30 |
@H_404_42@
"east"
: 120.4388307,
31 |
@H_404_42@
"west"
: 120.3107713
35 |
@H_404_42@
"coordinates"
: [ 120.3748010,36.0876620,0 ]
6.google maps 图片接口
有时候我们只想展示简单的地图,不需要交互和拖动,可以通过google maps提供的静态地图API引入动态生成的地图图片,不过需要为你的域名申请一个key。
引入图片的url格式为:
03 |
@H_404_42@sensor=
false
04 |
@H_404_42@&size=200x200
05 |
@H_404_42@&maptype=roadmap
06 |
@H_404_42@&key=<em>你申请到的KEY</em>
07 |
@H_404_42@&markers=color:blue|label:1|37.4447,-122.161
08 |
@H_404_42@&markers=color:red|label:2|37.3385,-121.886
09 |
@H_404_42@&markers=color:green|label:3|37.3716,-122.038
10 |
@H_404_42@&markers=color:yellow|label:4|37.7792,-122.42
得到图片:
后记
关于地图的更多操作,可以参见以前的这篇文章:Google Maps Api介绍与基础操作。
随着互联网的发展,越来越多的功能可以在前端实现,出现了越来越多的强大的第三方服务,我们可以很方便的在我们的网站上加入一些实用的功能。这也许 就是web2.0的魅力吧。