1. <?php
  2. /**
  3. * 根据地理坐标获取国家、省份、城市,及周边数据类(利用百度Geocoding API实现)
  4. * 百度密钥获取方法:http://lbsyun.baidu.com/apiconsole/key?application=key(需要先注册百度开发者账号)
  5. * Date: 2015-07-30
  6. * Author: fdipzone
  7. * Ver: 1.0
  8. *
  9. * Func:
  10. * Public getAddressComponent 根据地址获取国家、省份、城市及周边数据
  11. * Private toCurl 使用curl调用百度Geocoding API
  12. */
  13.  
  14. class Geocoding {
  15.  
  16. // 百度Geocoding API
  17. const API = 'http://api.map.baidu.com/geocoder/v2/';
  18.  
  19. // 不显示周边数据
  20. const NO_POIS = 0;
  21.  
  22. // 显示周边数据
  23. const POIS = 1;
  24.  
  25. /**
  26. * 根据地址获取国家、省份、城市及周边数据
  27. * @param String $ak 百度ak(密钥)
  28. * @param Decimal $longitude 经度
  29. * @param Decimal $latitude 纬度
  30. * @param Int $pois 是否显示周边数据
  31. * @return Array
  32. */
  33. public static function getAddressComponent($ak, $longitude, $latitude, $pois=self::NO_POIS){
  34.  
  35. $param = array(
  36. 'ak' => $ak,
  37. 'location' => implode(',', array($latitude, $longitude)),
  38. 'pois' => $pois,
  39. 'output' => 'json'
  40. );
  41.  
  42. // 请求百度api
  43. $response = self::toCurl(self::API, $param);
  44.  
  45. $result = array();
  46.  
  47. if($response){
  48. $result = json_decode($response, true);
  49. }
  50.  
  51. return $result;
  52.  
  53. }
  54.  
  55. /**
  56. * 使用curl调用百度Geocoding API
  57. * @param String $url 请求的地址
  58. * @param Array $param 请求的参数
  59. * @return JSON
  60. */
  61. private static function toCurl($url, $param=array()){
  62.  
  63. $ch = curl_init();
  64.  
  65. if(substr($url,0,5)=='https'){
  66. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  67. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
  68. }
  69.  
  70. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  71. curl_setopt($ch, CURLOPT_URL, $url);
  72. curl_setopt($ch, CURLOPT_POST, true);
  73. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));
  74.  
  75. $response = curl_exec($ch);
  76.  
  77. if($error=curl_error($ch)){
  78. return false;
  79. }
  80.  
  81. curl_close($ch);
  82.  
  83. return $response;
  84.  
  85. }
  86.  
  87. }
  88.  
  89. ?>

 使用

  1. <?php
  2. header("Content-type: text/html; charset=utf-8");
  3. header('Access-Control-Allow-Origin:*');
  4. require "Geocoding.class.php";//引入配置类
  5. $ak = '2q6OVS3LlFuZcoXGtXvqxAYq';//百度申请的秘钥,这里是错误的秘钥
  6. class Location
  7. {
  8. public $province;
  9. public $city;
  10. public $district;
  11. public $street;
  12. public $address;
  13. private static $_instance;
  14. public static function getInstance()
  15. {
  16. if(! (self::$_instance instanceof self) )
  17. {
  18. self::$_instance = new self();
  19. }
  20. return self::$_instance;
  21. }
  22. private function __construct(){}
  23. private function __clone(){}
  24. }
  25. function test_input($data)
  26. {
  27. $data = trim($data);
  28. $data = stripslashes($data);
  29. $data = htmlspecialchars($data);
  30. return $data;
  31. }
  32.  
  33. //默认以GET方式传送
  34. $longitude = test_input($_GET["long"]);
  35. $latitude = test_input($_GET["lat"]);
  36. //$longitude=113.327782;
  37. //$latitude=23.137202;
  38. $result = Geocoding::getAddressComponent($ak, $longitude, $latitude, Geocoding::NO_POIS);
  39. $locat=Location::getInstance() ;
  40.  
  41. $address=$result["result"]["addressComponent"];
  42. if($address["province"]!=$address["city"])
  43. {
  44. $location=$address["province"].$address["city"].$address["district"].$address["street"];
  45. }
  46. else
  47. {
  48. $location=$address["city"].$address["district"].$address["street"];
  49. }
  50. $locat->province=$address["province"];
  51. $locat->city=$address["city"];
  52. $locat->district=$address["district"];
  53. $locat->street=$address["street"];
  54. $locat->address=$location;
  55. $json = json_encode($locat);
  56. echo "$json";//返回类似地址字符串也可根据自己需要返回地址
  57.  
  58. ?>

  

 

 

获取秘钥地址http://developer.baidu.com/map/

参考链接http://my.oschina.net/xialeistudio/blog/366347

php用百度地图API进行逆地址解析的更多相关文章

  1. 【百度地图API】批量地址解析与批量反地址解析(带商圈数据)

    原文:[百度地图API]批量地址解析与批量反地址解析(带商圈数据) 摘要:因为地址解析的webserives方式还没有开通,所以先用JS版本的地址解析接口来批量获取地址解析数据吧,同时还能得到商圈的数 ...

  2. 【百度地图API】当地址解析失败时,如何调用search方法查找地址

    原文:[百度地图API]当地址解析失败时,如何调用search方法查找地址 有个朋友问我,当地址解析失败时,应该如何处理呢?比如,他想搜索“南宁市青秀区”. --------------------- ...

  3. 百度地图API和高德地图API资料集锦

    [高德地图API]从零开始学高德JS API(五)路线规划——驾车|公交|步行   [高德地图API]从零开始学高德JS API(四)搜索服务——POI搜索|自动完成|输入提示|行政区域|交叉路口|自 ...

  4. 【百度地图API】如何区分地址解析和智能搜索?

    原文:[百度地图API]如何区分地址解析和智能搜索? 摘要: 很多用户一直无法区分地址解析geocoder和智能搜索localsearch的使用场景.该文章用一个详尽的示例,充分展示了这两个类,共5种 ...

  5. 【百度地图API】如何进行地址解析与反地址解析?——模糊地址能搜索到精确地理信息!

    原文:[百度地图API]如何进行地址解析与反地址解析?--模糊地址能搜索到精确地理信息! 摘要: 什么是地址解析? 什么是反地址解析? 如何运用地址解析,和反地址解析? 可以同时运用地址解析,和反地址 ...

  6. 百度地图API提供Geocoder类进行地址解析

    根据地址描述获得坐标百度地图API提供Geocoder类进行地址解析,您可以通过Geocoder.getPoint()方法来将一段地址描述转换为一个坐标. // 创建地址解析器实例var myGeo ...

  7. 百度地图api实例

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm11.aspx ...

  8. 微信小程序城市定位(借助百度地图API判断城市)

    概述 微信小程序提供一些API(地址)用于获取当前用户的地理位置等信息,但无论是wx.getLocation,还是wx.chooseLocation均没有单独的字段表示国家与城市信息,仅有经纬度信息. ...

  9. 微信小程序城市定位(百度地图API)

    概述 微信小程序提供一些API(地址)用于获取当前用户的地理位置等信息,但无论是wx.getLocation,还是wx.chooseLocation均没有单独的字段表示国家与城市信息,仅有经纬度信息. ...

随机推荐

  1. Oracle数据库定义语言(DDL)

    --使用Create遇见创建表 Create Table table_name ( column_name datatype [null|not null], column_name datatype ...

  2. Sqoop- sqoop将mysql数据表导入到hive报错

    sqoop将mysql数据表导入到hive报错 [root@ip---- lib]# sqoop import --connect jdbc:mysql://54.223.175.12:3308/gx ...

  3. 用eclipse写xml文件

    1. 2.把写好的xml文件粘贴到src文件夹中.

  4. vs2015配置boost c++

    参考:https://blog.csdn.net/zengraoli/article/details/70187556 https://blog.csdn.net/misterfm/article/d ...

  5. Skype SILK vs. iLBC vs. Speex

    对比一下这三种VOIP语音算法的特点: 1 参数与特征 2 SILK性能 关于iLBC和Speex的性能可以参考以前写的文章. 3 关于VOIP一些观点(仅代表个人观点) 1)  Skype 辛苦三年 ...

  6. java.util Properties使用记录

    转:http://www.2cto.com/px/201006/47834.html 在java.util 包下面有一个类 Properties,该类主要用于读取以项目的配置文件(以.properti ...

  7. C++STL库中map容器常用应用

    #include<iostream> #include<cstdio> #include<map> //按键值大小构成二叉搜索树 using namespace s ...

  8. UE3代码阅读需知

    转自:http://www.cnblogs.com/hmxp8/archive/2012/02/21/2361211.html 掌握一款庞大的引擎,要一下子掌握真的很难,慢慢地从Editor,Scri ...

  9. httpd或Nginx负载均衡tomcat

    实验环境:CentOS7 #两台tomcat的基本配置如下: [root@webapps localhost]#setenforce 0 [root@webapps localhost]#iptabl ...

  10. oracle--循环PL/SQL--demo1---

    --简单的条件判断if–then --编写一个过程,可以输入一个雇员名,如果该雇员的工资低于2000,就给该员工工资增加10%. create or replace procedure sp_pro6 ...