php根据地理坐标获取国家、省份、城市,及周边数据类
功能:当App获取到用户的地理坐标时,可以根据坐标知道用户当前在那个国家、省份、城市,及周边有什么数据。
原理:基于百度Geocoding API 实现,需要先注册百度开发者,然后申请百度AK(密钥)
Geocoding.class.PHP
<?php
/**
* 根据地理坐标获取国家、省份、城市,及周边数据类(利用百度Geocoding API实现)
* 百度密钥获取方法:http://lbsyun.baidu.com/apiconsole/key?application=key(需要先注册百度开发者账号)
* Date: 2015-07-30
* Author: fdipzone
* Ver: 1.0
*
* Func:
* Public getAddressComponent 根据地址获取国家、省份、城市及周边数据
* Private toCurl 使用curl调用百度Geocoding API
*/
class Geocoding {
// 百度Geocoding API
const API = 'http://api.map.baidu.com/geocoder/v2/';
// 不显示周边数据
const NO_POIS = 0;
// 显示周边数据
const POIS = 1;
/**
* 根据地址获取国家、省份、城市及周边数据
* @param String $ak 百度ak(密钥)
* @param Decimal $longitude 经度
* @param Decimal $latitude 纬度
* @param Int $pois 是否显示周边数据
* @return Array
*/
public static function getAddressComponent($ak, $longitude, $latitude, $pois=self::NO_POIS){
$param = array(
'ak' => $ak,
'location' => implode(',', array($latitude, $longitude)),
'pois' => $pois,
'output' => 'json'
);
// 请求百度api
$response = self::toCurl(self::API, $param);
$result = array();
if($response){
$result = json_decode($response, true);
}
return $result;
}
/**
* 使用curl调用百度Geocoding API
* @param String $url 请求的地址
* @param Array $param 请求的参数
* @return JSON
*/
private static function toCurl($url, $param=array()){
$ch = curl_init();
if(substr($url,0,5)=='https'){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));
$response = curl_exec($ch);
if($error=curl_error($ch)){
return false;
}
curl_close($ch);
return $response;
}
}
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
demo.php
<?php
require "Geocoding.class.php";
$ak = '这里填写你的百度AK';
$longitude = 113.327782;
$latitude = 23.137202;
$result = Geocoding::getAddressComponent($ak, $longitude, $latitude, Geocoding::NO_POIS);
echo '<pre>';
print_r($result);
echo '</pre>';
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
例子返回:
Array
(
[status] => 0
[result] => Array
(
[location] => Array
(
[lng] => 113.32778195925
[lat] => 23.137201991056
)
[formatted_address] => 广东省广州市天河区体育西横街35号
[business] => 天河城,体育中心,沙河
[addressComponent] => Array
(
[city] => 广州市
[country] => 中国
[direction] => 附近
[distance] => 14
[district] => 天河区
[province] => 广东省
[street] => 体育西横街
[street_number] => 35号
[country_code] => 0
)
[poiRegions] => Array
(
)
[sematic_description] => 红盾大厦北58米
[cityCode] => 257
)
)
php根据地理坐标获取国家、省份、城市,及周边数据类的更多相关文章
- php依据地理坐标获取国家、省份、城市,及周边数据类
功能:当App获取到用户的地理坐标时,能够依据坐标知道用户当前在那个国家.省份.城市.及周边有什么数据. 原理:基于百度Geocoding API 实现.须要先注冊百度开发人员.然后申请百度AK(密钥 ...
- Laravel根据Ip获取国家,城市信息
https://blog.csdn.net/zhezhebie/article/details/79097133 1.安装: composer require geoip2/geoip2:~2.0 2 ...
- 根据现有IP地址获取其地理位置(省份,城市等)的方法
根据现有IP地址获取其地理位置(省份,城市等)的方法 function GetIpLookup($ip = ''){ if(empty($ip)){ return '请输入IP地址'; } $res ...
- 企业运维实践-Nginx使用geoip2模块并利用MaxMind的GeoIP2数据库实现处理不同国家或城市的访问最佳实践指南
关注「WeiyiGeek」公众号 设为「特别关注」每天带你玩转网络安全运维.应用开发.物联网IOT学习! 希望各位看友[关注.点赞.评论.收藏.投币],助力每一个梦想. 本章目录 目录 0x00 前言 ...
- php根据IP获取IP所在城市
转载出处:php实现根据IP地址获取其所在省市的方法 //根据现有IP地址获取其地理位置(省份,城市等)的方法 function GetIpLookup($ip = ''){ if(empty($ip ...
- mpvue微信小程序多列选择器用法:实现省份城市选择
前言 微信小程序默认给我们提供了一个省市区的picker选择器,只需将mode设置为region即可 <picker mode="region" bindchange=&qu ...
- ABP开发框架前后端开发系列---(6)ABP基础接口处理和省份城市行政区管理模块的开发
最近没有更新ABP框架的相关文章,一直在研究和封装相关的接口,总算告一段落,开始继续整理下开发心得.上次我在随笔<ABP开发框架前后端开发系列---(5)Web API调用类在Winform项目 ...
- 省份-城市-区域三级联动【struts2 + ajax +非数据库版】
package loaderman; /** * 实体,封装省份和城市 */ public class Bean { private String province;//省份 private Stri ...
- Ajax案例-基于XML,以POST方式,完成省份-城市二级下拉联动
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC & ...
随机推荐
- font-face 跨域解决
nginx 里设置@font-face 跨域 server { ... # Fix @font-face cross-domain restriction in Firefox location ~* ...
- Knockout开发中文API系列4–绑定关键字
目的 Visible绑定通过绑定一个值来确定DOM元素显示或隐藏 示例 <div data-bind="visible: shouldShowMessage"> You ...
- 安装redis出现cc adlist.o /bin/sh:1:cc:not found
安装redis时 提示执行make命令时, 提示 CC adlist.o /bin/sh: cc: 未找到命令 问题原因:这是由于系统没有安装gcc环境,因此在进行编译时才会出现上面提示,当安装好gc ...
- Redis 2.8.18 安装报错 error: jemalloc/jemalloc.h: No such file or directory解决方法
http://www.phperz.com/article/14/1219/42002.html ——————————————————————————————————————————————————
- R-table和tapply函数
table可统计数据的频数 tapply可根据因子.向量和要计算的函数计算 > class<-c(1,2,3,2,1,2,1,3) > class[1] 1 2 3 > c(8 ...
- Android Studio错误提示:Gradle project sync failed. Basic functionality (eg. editing, debugging) will not work properly
Android Studio中出现提示: Gradle project sync failed. Basic functionality (eg. editing, debugging) will n ...
- 【Python】python3-list列表引用
print(names) #列出列表的内容 print(names[3]) #访问列表中第4个值 print(names[1:3]) #访问列表中从第2个到第3个的值 print(names[-1]) ...
- MongoDB MapReduce 小例子
var map = function(){ if (this.gscode == "ZTJB"){ ymd = this.ymd; emit("maxymd", ...
- oc总结 --oc基础语法相关知识
m是OC源文件扩展名,入口点也是main函数,第一个OC程序: #import <Foundation/Foundation.h> int main(int argc, const cha ...
- WebService系列一:WebService简介
原文链接:http://www.cnblogs.com/xdp-gacl/p/4259109.html 一.WebService是什么 WebService是一种跨编程语言和跨操作系统平台的远程调用技 ...