测试经纬度信息,37.863036,113.598909。通过地图查询,所在城市为:阳泉。

<?php
class test{
public static $test_key = 'dfgfdgfh3c8ttrr1774gfdgfdgfd9rgqw1'; //这里是申请的百度appkey
$url = 'http://api.map.baidu.com/geocoder/v2/';
//37.863036
$longitude = number_format(doubleval($longitude), );
//113.598909
$latitude = number_format(doubleval($latitude), );
$gps = $latitude . ',' . $longitude;
$params = array(
'location' => $gps,
'pois' => ,
);
$info = test2::get_contents($url, $params);
print_r($info);
}
class test2{ private static function get_contents($url, $param)
{
$baidu_key = test::test_key ;
$param['ak'] = $baidu_key;
$param['output'] = 'json';
$url = $url . '?' . http_build_query($param, '', '&');
//echo $url;
$retry = ;
$result = array(); while ($retry > ) {
$result = json_decode(self::curl_get($url), true); if (!empty($result) && isset($result['status']) && $result['status'] == ) {
return $result;
}
if (!empty($result) && isset($result['status'])) {
$status = $result['status'];
} else {
$status = ' http_error:';
}
EdjLog::info("baidu_response retry status is" . $status.'params'.json_encode($param));
$retry--;
}
EdjLog::warning('request to baidu lbs api failed after retries'); return $result;
} private static function curl_get($url, $milliseconds = )
{
$start_time = microtime(true); $ch = curl_init();
//这个参数很重要,设置这个才可以支持毫秒级的超时设置
curl_setopt($ch, CURLOPT_NOSIGNAL, );
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_USERAGENT, self::$useragent);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FAILONERROR, );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, self::$connecttimeout);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $milliseconds); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
$http_error_code = curl_errno($ch);
curl_close($ch); EdjLog::info("request to baidu lbs api, url is $url, cost time:" . (microtime(true) - $start_time));
EdjLog::info("baidu http_error:$http_error_code response is " . str_replace(PHP_EOL, '', $response));
return $response;
}

结果:

Array
(
[status] =>
[result] => Array
(
[location] => Array
(
[lng] => 113.598909
[lat] => 37.863036027778
) [formatted_address] => 山西省阳泉市城区德胜街130号
[business] =>
[addressComponent] => Array
(
[country] => 中国
[country_code] =>
[country_code_iso] => CHN
[country_code_iso2] => CN
[province] => 山西省
[city] => 阳泉市
[city_level] =>
[district] => 城区
[town] =>
[adcode] =>
[street] => 德胜街
[street_number] => 130号
[direction] => 北
[distance] =>
) [pois] => Array
(
) [roads] => Array
(
) [poiRegions] => Array
(
[] => Array
(
[direction_desc] => 内
[name] => 阳泉站
[tag] => 交通设施;火车站
[uid] => a2ef2cba1b2e6961ab0dd8da
[distance] =>
) ) [sematic_description] => 阳泉站内
[cityCode] =>
) )

通过经纬度获取所属城市信息-php的更多相关文章

  1. 根据百度API获得经纬度,然后根据经纬度在获得城市信息

    package com.pb.baiduapi; import java.io.BufferedReader; import java.io.IOException; import java.io.I ...

  2. Java根据百度API获得经纬度,然后根据经纬度在获得城市信息

    原文:http://www.open-open.com/code/view/1421032487812 import java.io.BufferedReader; import java.io.IO ...

  3. ios项目开发(天气预报项目):通过经纬度获取当前城市名称

     1 . 在项目里加入� CoreLocation.framework 2 .在 .h 文件输入例如以下: 1.#import <CoreLocation/CLLocation.h> ...

  4. Python-根据照片信息获取用户详细信息(微信发原图或泄露位置信息)

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 蒙娜丽胖 PS:如有需要Python学习资料的小伙伴可以加点击下方 ...

  5. windows phone 7 定位(获取经纬度),然后找到经纬度所在的位置(城市信息)

    原文:windows phone 7 定位(获取经纬度),然后找到经纬度所在的位置(城市信息) 前几天做项目用到, 代码贴给大家. /// <summary> /// 获取当前位置的经纬度 ...

  6. [微信小程序] 微信小程序获取用户定位信息并加载对应城市信息,wx.getLocation,腾讯地图小程序api,微信小程序经纬度逆解析地理信息

    因为需要在小程序加个定位并加载对应城市信息 然而小程序自带api目前只能获取经纬度不能逆解析,虽然自己解析方式,但是同时也要调用地图,难道用户每次进小程序还要强行打开地图选择地址才定位吗?多麻烦也不利 ...

  7. 微信小程序自动定位,通过百度地图根据经纬度获取该地点所在城市信息

    微信小程序获得经纬度 var that = this wx.getLocation({ type: 'wgs84', success(res) { console.log(res) that.setD ...

  8. 百度api:根据经纬度获取地理位置信息

    调用百度api,根据经度和纬度获取地理位置信息,返回Json. C#代码: using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Syste ...

  9. JS根据经纬度获取地址信息

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

随机推荐

  1. 阿里云香港主机自动换IP

    为什么要写这个脚本原因你懂的,现在都是直接封IP pip3 install aliyun-python-sdk-alidns aliyun-python-sdk-domain aliyun-pytho ...

  2. Fiddler 手机抓包设置

    IOS 为例 1. Fiddler -> Tools -> Options ->Connection 2. 手机 Settings -> WIFI ->设置当前连接的网络 ...

  3. JS--理解call、apply和bind

    call.apply和bind call,apply是Function原型中的方法,它们的作用一样,区别在于传入参数的方式不同. call(thisArg, arg1, arg2...) 传入的参数不 ...

  4. leetcode473 Matchsticks to Square

    一开始想求所有结果为target的组合来着,但是所选元素不能重叠.用这个递归思想很简单,分成四个桶,每次把元素放在任意一个桶里面,最后如果四个桶相等就可以放进去,有一个地方可以剪枝,假如任意一个桶的元 ...

  5. Appium遇到的问题二(持续更新....)

    Python版: 1.运行Appium遇到的错误:此问题是由于JDK版本要在1.7及以上. Android开发要求. A new session could not be created. C:\Py ...

  6. unity3d-游戏实战突出重围,第一天

           此游戏是根据书上一步一步敲的.是一个入门级别的游戏.         游戏状态机 说到游戏状态机,就不得不提到开发中最常用的MVC模式. MVC模式的全称是Model-View-Cont ...

  7. vs使用gitflow

    1.背景:之前在开发一个项目时,用tfs管理代码,并用“禁止多人编辑”来避免冲突,但仅适用于开发团队较小时.缺点: (1).开发团队较大,开发人员较多时,会出现经常互相锁,增加沟通成本.比如增加文件时 ...

  8. SnmpTools配置

    上网搜索了很多文档,但是snmptools一直没有配置好,原因就是64机器,网上的说法大多直接复制过来的,或者就没有考虑64位机器.经过仔细搜索和测试,一下是详细的配置过程: Index 安装 如果是 ...

  9. jQuery安装

    http://www.runoob.com/jquery/jquery-install.html 网页中添加jQuery: 方法一:可以从http://jquery.com/download/ 下载j ...

  10. js函数集

    js函数集·字符串(String) 1.声明 var myString = new String("Every good boy does fine."); var myStrin ...