Map.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Jun.peng
  5. * Date: 2019/6/19
  6. * Time: 15:53
  7. */
  8. namespace map;
  9. class Map
  10. {
  11. //根据地址获取经纬度
  12. public static function getLngLat($address)
  13. {
  14. // http://api.map.baidu.com/geocoder/v2/?address=北京市海淀区上地十街10号&output=json&ak=您的ak&callback=showLocation //GET请求
  15. $data = [
  16. 'address' => $address,
  17. 'ak' => 'EglSDP3UNYtT2GsjGsoBFrHYBfixPrDv',
  18. 'output' => 'json',
  19. ];
  20. //转化为网址形式
  21. $url = 'http://api.map.baidu.com/geocoder/v2/?' . http_build_query($data);
  22. $res = self::doCurl($url);
  23. $res = json_decode($res,true);
  24. return $res['result'];
  25. }
  26. //根据经纬度或者地址获取百度地图
  27. public static function staticimage($center)
  28. {
  29. if (!$center) {
  30. return '';
  31. }
  32. $data = [
  33. 'ak' => 'EglSDP3UNYtT2GsjGsoBFrHYBfixPrDv',
  34. 'width' => '100',
  35. 'height' => '200',
  36. 'center' => $center,
  37. 'markers' => $center,
  38. ];
  39. $url = 'http://api.map.baidu.com/geocoder/v2/?' . http_build_query($data);
  40. $res = self::doCurl($url);
  41. return $res;
  42. }
  43. public static function doCurl($url, $type = 0, $data = [])
  44. {
  45. $ch = curl_init(); //初始化
  46. //设置选项
  47. curl_setopt($ch, CURLOPT_URL, $url);
  48. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  49. curl_setopt($ch, CURLOPT_HEADER, 0);
  50. if ($type == 1) {
  51. //post
  52. curl_setopt($ch, CURLOPT_PORT, 1);
  53. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  54. }
  55. //执行获取内容
  56. $output = curl_exec($ch);
  57. //释放curl句柄
  58. curl_close($ch);
  59. return $output;
  60. }
  61. /**
  62. * 【腾讯地图】
  63. * 【根据详细地址获取经纬度】
  64. * 20170920
  65. *
  66. * @param $address
  67. * @return array
  68. */
  69. public static function getPoint($address)
  70. {
  71. $url = "https://apis.map.qq.com/ws/geocoder/v1/?address=" . $address . "&key=5ULBZ-2M4LJ-QS3FO-F6SSW-X3CRK-WABPO";
  72. $res = self::doCurl($url);
  73. return $res;
  74. }
  75. }