之前碰到的这样一个需求,要将公司的服务在地图中显示出来,并将用户每天的访问坐标进行统计看有多少用户是在所能达到的服务范围半径内。
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
|
<?php
/**
* 验证坐标点是否在某区域内
* @author xiaoliang <1058436713@qq.com>
* class validationmap
*/
class validationmap{
private static $coordarray ;
private static $vertx = [];
private static $verty = [];
/**
* 设置坐标区域
* @param mixed $coordarray
*/
public static function setcoordarray( array $coordarray )
{
self:: $coordarray = $coordarray ;
}
/**
* 验证区域范围
* @param array $coordarray
* @return bool
*/
public static function iscitycenter( array $coordarray ){
if (!self::vaildatepoint( $coordarray )){
return false;
}
return self::pnpoly( count (self:: $coordarray ), $coordarray [ 'lng' ], $coordarray [ 'lat' ]);
}
/**
* 比较区域坐标
* @param $nvert
* @param $testx
* @param $testy
* @return bool
*/
private static function pnpoly( $nvert , $testx , $testy )
{
$c = false;
for ( $i = 0, $j = $nvert -1; $i < $nvert ; $j = $i ++) {
if ( ( (self:: $verty [ $i ]> $testy ) != (self:: $verty [ $j ]> $testy ) ) && ( $testx < (self:: $vertx [ $j ]-self:: $vertx [ $i ]) * ( $testy -self:: $verty [ $i ]) / (self:: $verty [ $j ]-self:: $verty [ $i ]) + self:: $vertx [ $i ]) )
$c = ! $c ;
}
return $c ;
}
/**
* 验证坐标
* @param array $pointarray
* @return bool
*/
private static function vaildatepoint( array $pointarray ){
$maxy = $maxx = 0;
$miny = $minx = 9999;
foreach (self:: $coordarray as $item ){
if ( $item [ 'lng' ]> $maxx ) $maxx = $item [ 'lng' ];
if ( $item [ 'lng' ] < $minx ) $minx = $item [ 'lng' ];
if ( $item [ 'lat' ]> $maxy ) $maxy = $item [ 'lat' ];
if ( $item [ 'lat' ] < $miny ) $miny = $item [ 'lat' ];
self:: $vertx [] = $item [ 'lng' ];
self:: $verty [] = $item [ 'lat' ];
}
if ( $pointarray [ 'lng' ] < $minx || $pointarray [ 'lng' ] > $maxx || $pointarray [ 'lat' ] < $miny || $pointarray [ 'lat' ] > $maxy ) {
return false;
}
return true;
}
}
/**************************** test *************************************/
$map = [ //上海
[ "lng" => 121.488286, "lat" => 31.420147],
[ "lng" => 121.702154, "lat" => 31.294828],
[ "lng" => 121.780918, "lat" => 31.141157],
[ "lng" => 121.782068, "lat" => 30.941157],
[ "lng" => 121.492885, "lat" => 30.909931],
[ "lng" => 121.22325, "lat" => 30.890099],
[ "lng" => 121.161482, "lat" => 31.015526],
[ "lng" => 121.076395, "lat" => 31.226239],
[ "lng" => 121.189873, "lat" => 31.339688],
[ "lng" => 121.459509, "lat" => 31.41368],
];
$array = [ "lat" =>31.218681, "lng" =>121.08604]; //进行验证的区域
validationmap::setcoordarray( $map );
var_dump(validationmap::iscitycenter( $array ));
|
在地图中的运用:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。