| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- class BaiduMap {
- public static function getKey()
- {
- $keyArray = array(
- "hDgtpAMI5cdojWT0L6MANfKO",
- "WM1tG46pMWurjR2bBXLts8Q0",
- "7IVEKqd3OmZTOkcMX3ASer2Q",
- "xLuGBeuWj1XFhUMLkb1lj8k6",
- "eaTsN78Z6b7Y5dbD3rTp2b1s"
- );
-
- $num = rand(0, 4);
-
- return $keyArray[$num];
- }
- }
- //这个类主要是处理与经纬度有关的功能
- class LewaimaiCoordinate {
- //将GPS坐标转换成高德地图用的坐标
- //微信的事件获取到的坐标均为GPS坐标
- //返回值数组,下标0表示经度,下标1表示维度
- public static function GPStoAMAP($longitude, $latitude)
- {
- $url = "http://restapi.amap.com/v3/assistant/coordinate/convert?locations=" . $longitude . "," . $latitude . "&coordsys=gps&output=json&key=d50fa09605569ecf02005844074bbd28";
- $res = LewaimaiHttp::GET($url);
- $resArray = json_decode($res, true);
- if ($resArray["status"] == "1" && $resArray["info"] == "ok")
- {
- $locations = $resArray["locations"];
- $locationArray = explode(",", $locations);
- return $locationArray;
- }
-
- return false;
- }
-
- //根据经纬度,获得这个经纬度对应的省份,省市,区和详细地址的信息
- public static function GetPositionInfo($longitude, $latitude, $is_gps = true)
- {
- if ($is_gps)
- {
- $url = "http://apis.map.qq.com/ws/geocoder/v1/?location=" . $latitude . "," . $longitude . "&key=OKJBZ-SEUWG-3NTQR-IRTQH-IVB5Z-5UFIR&coord_type=1";
- }
- else
- {
- $url = "http://apis.map.qq.com/ws/geocoder/v1/?location=" . $latitude . "," . $longitude . "&key=OKJBZ-SEUWG-3NTQR-IRTQH-IVB5Z-5UFIR";
- }
-
- $res = LewaimaiHttp::GET($url);
- $resArray = json_decode($res, true);
- if (isset($resArray["status"]) && $resArray["status"] == "0")
- {
- $infoArray = array();
-
- $infoArray["province"] = $resArray["result"]["address_component"]["province"];
- if (is_array($infoArray["province"]))
- {
- $infoArray["province"] = "";
- }
-
- $infoArray["city"] = $resArray["result"]["address_component"]["city"];
- if (is_array($infoArray["city"]))
- {
- $infoArray["city"] = "";
- }
-
- $infoArray["district"] = $resArray["result"]["address_component"]["district"];
- if (is_array($infoArray["district"]))
- {
- $infoArray["district"] = "";
- }
-
- $infoArray["address"] = $resArray["result"]["formatted_addresses"]["recommend"];
-
- return $infoArray;
- }
-
- return false;
- }
-
- //更新某个顾客的所有位置信息,包括经纬度,定位结果等
- public static function UpdateLewaimaicustomerPosition($lewaimai_customer_id, $longitude, $latitude, $is_gps = true)
- {
- $lewaimaicustomerModel = LewaimaiCustomer::model()->findByPk($lewaimai_customer_id);
- if ($lewaimaicustomerModel)
- {
- //首先将获取到的GPS坐标,转换成高德系统的坐标
- if ($is_gps)
- {
- $locationArray = LewaimaiCoordinate::GPStoAMAP($longitude, $latitude);
- }
- else
- {
- $locationArray[0] = $longitude;
- $locationArray[1] = $latitude;
- }
-
- //获取信息
- $infoArray = LewaimaiCoordinate::GetPositionInfo($longitude, $latitude, $is_gps);
- if (!is_array($infoArray))
- {
- $infoArray = array();
- $infoArray["province"] = "";
- $infoArray["city"] = "";
- $infoArray["district"] = "";
- $infoArray["address"] = "";
- }
-
- //LewaimaiDebug::LogArray($infoArray);
-
- if (!$locationArray || !$infoArray)
- {
- return false;
- }
-
- $lewaimaicustomerModel->coordinate_x = $locationArray[1];
- $lewaimaicustomerModel->coordinate_y = $locationArray[0];
- $lewaimaicustomerModel->last_position_time = date("Y-m-d H:i:s");
- $lewaimaicustomerModel->position_province = $infoArray["province"];
- $lewaimaicustomerModel->position_city = $infoArray["city"];
- $lewaimaicustomerModel->position_district = $infoArray["district"];
- $lewaimaicustomerModel->position_address = $infoArray["address"];
- if (!$lewaimaicustomerModel->update())
- {
- LewaimaiDebug::LogModelError($lewaimaicustomerModel);
-
- return false;
- }
-
- return true;
- }
-
- return false;
- }
-
- //根据ip获取位置信息
- /**
- {
- "address": "CN|广东|深圳|None|CHINANET|0|0",
- "content": {
- "address_detail": {
- "province": "广东省",
- "city": "深圳市",
- "district": "",
- "street": "",
- "street_number": "",
- "city_code": 340
- },
- "address": "广东省深圳市",
- "point": {
- "y": "2560682.35",
- "x": "12693451.44"
- }
- },
- "status": 0
- }
- */
- public static function GetPositionByIp($ip)
- {
- $key = BaiduMap::getKey();
- $url = file_get_contents("http://api.map.baidu.com/location/ip?ip=".$ip."&ak=".$key);
- $data = json_decode($url,true);
- if ($data && $data['status'] == 0) {
- return $data['content'];
- }
- return false;
- }
-
- }
|