php获取ios或android通过文件头(header)传过来的坐标,通过百度接口获取具体城市和地址,并存入到session中。
2017-09-02 17:02
489 查看
首先,在function.php方法文件中封装一个获取header头文件的方法。
if (!function_exists('getallheaders')) {
function getallheaders() {
$headers = array();
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
}
然后,在相应控制器中调用该方法获取数据,先检查session中有没有位置信息。
if(!$_SESSION['MyLocation'])
{
//获取定位坐标
$location = getallheaders();
$this->get_location($location); //调用封装的方法
}
最后,封装一个方法,用来请求百度接口返回具体位置信息,并存到session中。
public function get_location($location)
{
$lat = $location['Latitude'];
$lng = $location['Longitude'];
if($lat && $lng)
{
$url = 'http://api.map.baidu.com/geocoder/v2/?ak=5BFNbSgnVF5g2O72NpvTDxFm&location=' . $lat . ',' . $lng . '&output=json&pois=1';
$html = json_decode(file_get_contents($url),true);
if($html)
{
$myLocation = array(
'city' => $html['result']['addressComponent']['city'],
'addr' => isset($html['result']['addressComponent']['street']) ? $html['result']['addressComponent']['street'] : $html['result']['formatted_address'],
'lng' => $html['result']['location']['lng'],
'lat' => $html['result']['location']['lat'],
);
$_SESSION['MyLocation'] = $myLocation;
}
}
}
if (!function_exists('getallheaders')) {
function getallheaders() {
$headers = array();
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
}
然后,在相应控制器中调用该方法获取数据,先检查session中有没有位置信息。
if(!$_SESSION['MyLocation'])
{
//获取定位坐标
$location = getallheaders();
$this->get_location($location); //调用封装的方法
}
最后,封装一个方法,用来请求百度接口返回具体位置信息,并存到session中。
public function get_location($location)
{
$lat = $location['Latitude'];
$lng = $location['Longitude'];
if($lat && $lng)
{
$url = 'http://api.map.baidu.com/geocoder/v2/?ak=5BFNbSgnVF5g2O72NpvTDxFm&location=' . $lat . ',' . $lng . '&output=json&pois=1';
$html = json_decode(file_get_contents($url),true);
if($html)
{
$myLocation = array(
'city' => $html['result']['addressComponent']['city'],
'addr' => isset($html['result']['addressComponent']['street']) ? $html['result']['addressComponent']['street'] : $html['result']['formatted_address'],
'lng' => $html['result']['location']['lng'],
'lat' => $html['result']['location']['lat'],
);
$_SESSION['MyLocation'] = $myLocation;
}
}
}
相关文章推荐
- android 百度地图选择地址并再通过坐标获取地址
- IOS网络笔记--地图内容3(点击获取当前地址的具体坐标)
- php 通过ip获取所在城市地址信息 获取计算机外网ip
- 用户Ip地址和百度地图api接口获取用户地理位置(经纬度坐标,城市)
- 通过百度坐标获取地址
- java 调用百度地址匹配接口获取经纬度坐标
- iOS开发——定位获取经纬度坐标,并反编码获得地理位置信息(具体地址,国家、省、市等)
- PHP通过访问第三方接口,根据IP地址获取所在城市
- 想在自己的android应用中获得当天的天气情况,这该怎么做呢?不用担心。中国国家气象局提供了获取所在城市天气预报信息接口。通过这个接口,我们就可以获取天气信息了。
- Android Map Api 使用和开发(2) 定位我的位置、地图弹出泡泡、通过经纬度获取地址
- Android Map Api 使用和开发(2) 定位我的位置、地图弹出泡泡、通过经纬度获取地址
- Android Map Api 使用和开发(2) 定位我的位置、地图弹出泡泡、通过经纬度获取地址
- android 通过经纬度获取地址信息
- iOS通过百度mp3接口解析mp3文件地址代码分享
- Android Map Api 使用和开发(3)浮动搜索框 ,通过地址名称获取经纬度和详细地址并定位
- Android 根据坐标获取地址
- 求助:php通过新浪微博接口 api 如果获取某一地区下所有用户的微博列表?或使用包含某一关键词的微博列表
- 根据IP地址获取所在城市【通过新浪接口】
- Android Map Api 使用和开发(3)浮动搜索框 ,通过地址名称获取经纬度和详细地址并定位
- Android Map Api 使用和开发(3)浮动搜索框 ,通过地址名称获取经纬度和详细地址并定位