LewaimaiCoordinate.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. class BaiduMap {
  3. public static function getKey()
  4. {
  5. $keyArray = array(
  6. "hDgtpAMI5cdojWT0L6MANfKO",
  7. "WM1tG46pMWurjR2bBXLts8Q0",
  8. "7IVEKqd3OmZTOkcMX3ASer2Q",
  9. "xLuGBeuWj1XFhUMLkb1lj8k6",
  10. "eaTsN78Z6b7Y5dbD3rTp2b1s"
  11. );
  12. $num = rand(0, 4);
  13. return $keyArray[$num];
  14. }
  15. }
  16. //这个类主要是处理与经纬度有关的功能
  17. class LewaimaiCoordinate {
  18. //将GPS坐标转换成高德地图用的坐标
  19. //微信的事件获取到的坐标均为GPS坐标
  20. //返回值数组,下标0表示经度,下标1表示维度
  21. public static function GPStoAMAP($longitude, $latitude)
  22. {
  23. $url = "http://restapi.amap.com/v3/assistant/coordinate/convert?locations=" . $longitude . "," . $latitude . "&coordsys=gps&output=json&key=d50fa09605569ecf02005844074bbd28";
  24. $res = LewaimaiHttp::GET($url);
  25. $resArray = json_decode($res, true);
  26. if ($resArray["status"] == "1" && $resArray["info"] == "ok")
  27. {
  28. $locations = $resArray["locations"];
  29. $locationArray = explode(",", $locations);
  30. return $locationArray;
  31. }
  32. return false;
  33. }
  34. //根据经纬度,获得这个经纬度对应的省份,省市,区和详细地址的信息
  35. public static function GetPositionInfo($longitude, $latitude, $is_gps = true)
  36. {
  37. if ($is_gps)
  38. {
  39. $url = "http://apis.map.qq.com/ws/geocoder/v1/?location=" . $latitude . "," . $longitude . "&key=OKJBZ-SEUWG-3NTQR-IRTQH-IVB5Z-5UFIR&coord_type=1";
  40. }
  41. else
  42. {
  43. $url = "http://apis.map.qq.com/ws/geocoder/v1/?location=" . $latitude . "," . $longitude . "&key=OKJBZ-SEUWG-3NTQR-IRTQH-IVB5Z-5UFIR";
  44. }
  45. $res = LewaimaiHttp::GET($url);
  46. $resArray = json_decode($res, true);
  47. if (isset($resArray["status"]) && $resArray["status"] == "0")
  48. {
  49. $infoArray = array();
  50. $infoArray["province"] = $resArray["result"]["address_component"]["province"];
  51. if (is_array($infoArray["province"]))
  52. {
  53. $infoArray["province"] = "";
  54. }
  55. $infoArray["city"] = $resArray["result"]["address_component"]["city"];
  56. if (is_array($infoArray["city"]))
  57. {
  58. $infoArray["city"] = "";
  59. }
  60. $infoArray["district"] = $resArray["result"]["address_component"]["district"];
  61. if (is_array($infoArray["district"]))
  62. {
  63. $infoArray["district"] = "";
  64. }
  65. $infoArray["address"] = $resArray["result"]["formatted_addresses"]["recommend"];
  66. return $infoArray;
  67. }
  68. return false;
  69. }
  70. //更新某个顾客的所有位置信息,包括经纬度,定位结果等
  71. public static function UpdateLewaimaicustomerPosition($lewaimai_customer_id, $longitude, $latitude, $is_gps = true)
  72. {
  73. $lewaimaicustomerModel = LewaimaiCustomer::model()->findByPk($lewaimai_customer_id);
  74. if ($lewaimaicustomerModel)
  75. {
  76. //首先将获取到的GPS坐标,转换成高德系统的坐标
  77. if ($is_gps)
  78. {
  79. $locationArray = LewaimaiCoordinate::GPStoAMAP($longitude, $latitude);
  80. }
  81. else
  82. {
  83. $locationArray[0] = $longitude;
  84. $locationArray[1] = $latitude;
  85. }
  86. //获取信息
  87. $infoArray = LewaimaiCoordinate::GetPositionInfo($longitude, $latitude, $is_gps);
  88. if (!is_array($infoArray))
  89. {
  90. $infoArray = array();
  91. $infoArray["province"] = "";
  92. $infoArray["city"] = "";
  93. $infoArray["district"] = "";
  94. $infoArray["address"] = "";
  95. }
  96. //LewaimaiDebug::LogArray($infoArray);
  97. if (!$locationArray || !$infoArray)
  98. {
  99. return false;
  100. }
  101. $lewaimaicustomerModel->coordinate_x = $locationArray[1];
  102. $lewaimaicustomerModel->coordinate_y = $locationArray[0];
  103. $lewaimaicustomerModel->last_position_time = date("Y-m-d H:i:s");
  104. $lewaimaicustomerModel->position_province = $infoArray["province"];
  105. $lewaimaicustomerModel->position_city = $infoArray["city"];
  106. $lewaimaicustomerModel->position_district = $infoArray["district"];
  107. $lewaimaicustomerModel->position_address = $infoArray["address"];
  108. if (!$lewaimaicustomerModel->update())
  109. {
  110. LewaimaiDebug::LogModelError($lewaimaicustomerModel);
  111. return false;
  112. }
  113. return true;
  114. }
  115. return false;
  116. }
  117. //根据ip获取位置信息
  118. /**
  119. {
  120. "address": "CN|广东|深圳|None|CHINANET|0|0",
  121. "content": {
  122. "address_detail": {
  123. "province": "广东省",
  124. "city": "深圳市",
  125. "district": "",
  126. "street": "",
  127. "street_number": "",
  128. "city_code": 340
  129. },
  130. "address": "广东省深圳市",
  131. "point": {
  132. "y": "2560682.35",
  133. "x": "12693451.44"
  134. }
  135. },
  136. "status": 0
  137. }
  138. */
  139. public static function GetPositionByIp($ip)
  140. {
  141. $key = BaiduMap::getKey();
  142. $url = file_get_contents("http://api.map.baidu.com/location/ip?ip=".$ip."&ak=".$key);
  143. $data = json_decode($url,true);
  144. if ($data && $data['status'] == 0) {
  145. return $data['content'];
  146. }
  147. return false;
  148. }
  149. }