根据IP获取访客所在国家/城市/经纬度
安装GeoIP扩展:
1
|
sudo apt-get install libgeoip-dev
|
1
|
pecl install geoip-1.1.0
|
注意:Beta版要指定版本号.如果是apt安装的PHP,直接安装php5-geoip这个包即可.
php.ini中加入:
1
2
|
extension=geoip.so
geoip.custom_directory="/usr/share/GeoIP"
|
免费下载GeoLiteCity数据库(解压后18MB):
http://dev.maxmind.com/geoip/legacy/install/city/
1
2
3
4
|
wget http: //geolite .maxmind.com /download/geoip/database/GeoLiteCity .dat.gz
gunzip GeoLiteCity.dat.gz
sudo mkdir - v /usr/share/GeoIP
sudo mv - v GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity .dat
|
测试:
1
|
php -a
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?php
print_r(geoip_record_by_name( '106.37.165.80' )); //回车后按Ctrl+D运行
Array
(
[continent_code] => AS
[country_code] => CN
[country_code3] => CHN
[country_name] => China //国家
[region] => 22
[city] => Beijing //城市
[postal_code] =>
[latitude] => 39.928901672363 //纬度
[longitude] => 116.38829803467 //经度
[dma_code] => 0
[area_code] => 0
)
|
在命令行用geoiplookup查看IP信息:
1
|
traceroute www.oschina.net
|
可见IP地址
1
|
61.145.122.155
|
1
2
3
|
sudo apt-get install geoip-bin geoip-database
geoiplookup 61.145.122.155 -f /usr/share/GeoIP/GeoIP .dat
GeoIP Country Edition: CN, China
|
geoip-database提供的GeoIP.dat只能精确到国家.
1
2
|
geoiplookup 61.145.122.155 -f /usr/share/GeoIP/GeoIPCity .dat
GeoIP City Edition, Rev 1: CN, 30, Guangdong, Guangzhou, N /A , 23.116699, 113.250000, 0, 0
|
从maxmind官网下的数据库GeoLiteCity则信息更详细.
geoiplookup 61.145.122.155 则同时显示上述两个数据库的信息.
根据IP确定经纬度与计算距离
可以用
1
|
geoip_record_by_name( $_SERVER [ 'REMOTE_ADDR' ])
|
根据用户IP确定经纬度.
注意:
1
|
geoip_record_by_name()
|
返回的西经和南纬是负数.
5000米转成经纬度:
纬度 Latitude: 1 deg = 110852 m
经度 Longitude: 1 deg = 111320*cos(lat) m
同一经线上,相差一纬度约为 110852 米
同一纬线上,相差一经度约为 111320*cos(lat) 米 (lat为该纬线的纬度)
1
2
3
4
5
6
7
8
9
|
<?php
//以当前用户经纬度为中心,查询5000米内的其他用户
$y = 5000 / 110852; //纬度的范围
$x = 5000 / (111320* cos ( $lat )); //经度的范围
$sql = '
select * from user where
lat >= ( $lat - $y ) and lat <= ( $lat + $y ) and
lon >= ( $lon - $x ) and lon <= ( $lon + $x );
';
|
数据库用户表中设两个字段,分别存储用户的经度lat和纬度lon.
1
2
|
($lat-$y) <= lat <= ($lat+$y)
($lon-$x) <= lon <= ($lon+$x)
|
这个范围是一个粗略的范围,下面计算距离后把超过5公里的用户去掉即可.
根据上面查询出来的用户的经纬度,
用半正矢公式(Haversine)根据经纬度计算两点间距离:
1
2
3
4
5
6
7
8
9
10
11
|
<?php
function distance( $lat1 , $lon1 , $lat2 , $lon2 ) {
$R = 6371393; //地球平均半径,单位米
$dlat = deg2rad ( $lat2 - $lat1 );
$dlon = deg2rad ( $lon2 - $lon1 );
$a = pow(sin( $dlat /2), 2) + cos ( deg2rad ( $lat1 )) * cos ( deg2rad ( $lat2 )) * pow(sin( $dlon /2), 2);
$c = 2 * atan2 (sqrt( $a ), sqrt(1- $a ));
$d = $R * $c ;
return round ( $d );
}
echo distance(0, 0, -1, 0); // 111202米
|
然后就可以用uasort或array_multisort由近到远列出用户了,比如有名为win,osx,lin这3个用户:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?php
$arr = array (
'win' => array (
'dis' => 1024,
'age' => 31
),
'osx' => array (
'dis' => 512,
'age' => 15
),
'lin' => array (
'dis' => 512,
'age' => 25
)
);
foreach ( $arr as $k => $v ) {
$sort [ 'dis' ][ $k ] = $v [ 'dis' ];
$sort [ 'age' ][ $k ] = $v [ 'age' ];
}
//先按距离升序排序,如果距离相同,则按年龄降序排序
array_multisort ( $sort [ 'dis' ], SORT_ASC, $sort [ 'age' ], SORT_DESC, $arr );
echo json_encode( $arr );
//{"lin":{"dis":512,"age":25},"osx":{"dis":512,"age":15},"win":{"dis":1024,"age":31}}
|