tongshanglei 2 years ago
parent
commit
beb55720d8

+ 9 - 2
catch/device/model/Station.php

@@ -57,8 +57,15 @@ class Station extends Model
             ->catchSearch()
             // ->field('mac,name,latitude,longitude,online_time')
             ->append(['is_online_text','is_online'])
-            ->select();
-            
+            ->select()
+            ->each(function($item, $key)  {
+                $wgsLoc = \algorithm\Geometry::gcj02ToWgs84((float)$item['latitude'],(float)$item['longitude']);
+                // $lngLat = \algorithm\Geometry::convertGcj02ToBd09($wgsLoc['lat'],$wgsLoc['lng']);
+                    $item['longitude']=$wgsLoc['lng'];
+                    $item['latitude']=$wgsLoc['lat'];
+                
+            })->toArray();
+        
         return $res;
     }
     /**

+ 1 - 1
catch/device/model/search/DeviceSearch.php

@@ -8,7 +8,7 @@ use think\facade\Db;
 
 trait DeviceSearch
 {
-      /**
+    /**
      * 根据部门搜索
      */
     public function searchNameAttr($query, $value, $data)

+ 30 - 0
extend/algorithm/Geometry.php

@@ -9,6 +9,36 @@ namespace algorithm;
 class Geometry
 {
 
+    
+    /**
+     * 火星坐标Gcj02 转 WGS84
+     * 
+     * @param double $lat 纬度
+     * @param double $lng 经度
+     */
+    public static function gcj02ToWgs84($lat, $lng)
+    {
+       
+        $pi = 3.14159265358979324;
+        $a = 6378245.0;
+        $ee = 0.00669342162296594323;
+        
+        $dlat = self::wgsTOgcjTransformLat($lng - 105.0, $lat - 35.0);
+        $dlng = self::wgsTOgcjTransformLon($lng - 105.0, $lat - 35.0);
+        $radlat = $lat / 180.0 * $pi;
+        $magic = sin($radlat);
+        $magic = 1 - $ee * $magic * $magic;
+        $sqrtmagic = sqrt($magic);
+        $dlat = ($dlat * 180.0) / (($a * (1 - $ee)) / ($magic * $sqrtmagic) * $pi);
+        $dlng = ($dlng * 180.0) / ($a / $sqrtmagic * cos($radlat) * $pi);
+        $mglat = $lat + $dlat;
+        $mglng = $lng + $dlng;
+        return [ 
+            'lng' => $lng * 2 - $mglng, 
+            'lat' => $lat * 2 - $mglat,
+        ];
+    }
+
     /**
      * 火星坐标Gcj02 转 百度地图坐标BD09
      * 腾讯地图用的也是GCJ02坐标