processor.php
3.67 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* [WeEngine System] Copyright (c) 2013 WE7.CC
*/
defined('IN_IA') or exit('Access Denied');
define(EARTH_RADIUS, 6371); //地球半径,平均半径为6371km
class We7_businessModuleProcessor extends WeModuleProcessor {
public function respond() {
global $_W;
$lat = $this->message['location_x'];
$lng = $this->message['location_y'];
$range = isset($this->module['config']['range']) ? $this->module['config']['range'] : 5;
$point = $this->squarePoint($lng, $lat, $range);
$sql = "SELECT id, title, thumb, content, lat, lng FROM " . tablename('business') . " WHERE weid = '{$_W['weid']}' AND lat<>0 AND lat >= '{$point['right-bottom']['lat']}' AND
lat <= '{$point['left-top']['lat']}' AND lng >= '{$point['left-top']['lng']}' AND lng <= '{$point['right-bottom']['lng']}'";
$result = pdo_fetchall($sql);
if (empty($result)) {
$hintInfo = $this->module['config']['info'] ? $this->module['config']['info'] : '抱歉,系统中的商户不在您附近!';
return $this->respText($hintInfo);
}
$news = array();
$min = -1;
foreach ($result as &$row) {
$row['distance'] = $this->getDistance($lat, $lng, $row['lat'], $row['lng']);
if ($min < 0 || $row['distance'] < $min) {
$min = $row['distance'];
}
}
unset($row);
$temp = array();
for ($i = 0; $i < 8; $i++) {
foreach ($result as $j => $row) {
if (empty($temp['distance']) || $row['distance'] < $temp['distance']) {
$temp = $row;
$h = $j;
}
}
if (!empty($temp)) {
$news[] = array(
'title' => $temp['title'] . '(距' . $temp['distance'] . '米)',
'description' => cutstr(strip_tags($temp['content']), 300),
'picurl' => $_W['attachurl'] . $temp['thumb'],
'url' => $this->createMobileUrl('detail', array('id' => $temp['id'])),
);
unset($result[$h]);
$temp = array();
}
}
return $this->respNews($news);
}
/**
* 计算某个经纬度的周围某段距离的正方形的四个点
*
* @param lng float 经度
* @param lat float 纬度
* @param distance float 该点所在圆的半径,该圆与此正方形内切,默认值为0.5千米
* @return array 正方形的四个点的经纬度坐标
*/
public function squarePoint($lng, $lat, $distance = 0.5) {
$dlng = 2 * asin(sin($distance / (2 * EARTH_RADIUS)) / cos(deg2rad($lat)));
$dlng = rad2deg($dlng);
$dlat = $distance / EARTH_RADIUS;
$dlat = rad2deg($dlat);
return array(
'left-top' => array('lat' => $lat + $dlat, 'lng' => $lng - $dlng),
'right-top' => array('lat' => $lat + $dlat, 'lng' => $lng + $dlng),
'left-bottom' => array('lat' => $lat - $dlat, 'lng' => $lng - $dlng),
'right-bottom' => array('lat' => $lat - $dlat, 'lng' => $lng + $dlng)
);
}
function getDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2) {
$radLat1 = $lat1 * M_PI / 180;
$radLat2 = $lat2 * M_PI / 180;
$a = $lat1 * M_PI / 180 - $lat2 * M_PI / 180;
$b = $lng1 * M_PI / 180 - $lng2 * M_PI / 180;
$s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
$s = $s * EARTH_RADIUS;
$s = round($s * 1000);
if ($len_type > 1) {
$s /= 1000;
}
return round($s, $decimal);
}
}