forked from wecenter/wecenter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo.php
70 lines (60 loc) · 2.3 KB
/
geo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/*
+--------------------------------------------------------------------------
| WeCenter [#RELEASE_VERSION#]
| ========================================
| by WeCenter Software
| © 2011 - 2014 WeCenter. All Rights Reserved
| http://www.wecenter.com
| ========================================
| Support: [email protected]
|
+---------------------------------------------------------------------------
*/
if (!defined('IN_ANWSION'))
{
die;
}
define('GEO_EARTH_RADIUS', 6378); // 地球半径
class geo_class extends AWS_MODEL
{
/**
* 计算某个经纬度的周围某段距离的正方形的四个点
*
* @param longitude float 经度
* @param latitude float 纬度
* @param radius float 该点所在圆的半径,该圆与此正方形内切, 单位: 千米
* @return array 正方形的四个点的经纬度坐标
*/
public function get_square_point($longitude, $latitude, $radius = 1)
{
$target_longitude = rad2deg((2 * asin(sin($radius / (2 * GEO_EARTH_RADIUS)) / cos(deg2rad($latitude)))));
$target_latitude = rad2deg(($radius / GEO_EARTH_RADIUS));
return array(
'TL' => array('latitude' => $latitude + $target_latitude, 'longitude' => $longitude - $target_longitude), // Top left point
//'TR' => array('latitude' => $latitude + $target_latitude, 'longitude' => $longitude + $target_longitude), // Top right point
//'BL' => array('latitude' => $latitude - $target_latitude, 'longitude' => $longitude - $target_longitude), // Bottom left point
'BR' => array('latitude' => $latitude - $target_latitude, 'longitude' => $longitude + $target_longitude) // Bottom right point
);
}
public function set_location($item_type, $item_id, $longitude, $latitude)
{
$this->delete('geo_location', "`item_type` = '" . $this->quote($item_type) . "' AND `item_id` = " . intval($item_id));
return $this->insert('geo_location', array(
'item_type' => $item_type,
'item_id' => intval($item_id),
'longitude' => $longitude,
'latitude' => $latitude,
'add_time' => time()
));
}
public function get_distance($lon1, $lat1, $lon2, $lat2)
{
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
return ($miles * 0.8684);
}
}