zoukankan      html  css  js  c++  java
  • php获取ios或android通过文件头(header)传过来的坐标,通过百度接口获取具体城市和地址,并存入到session中

    首先,在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;
        }
      }
    }

    来源:https://www.cnblogs.com/shenzikun1314/p/7467119.html

  • 相关阅读:
    前端面试1
    关于JavaScript学习,推荐博客及书籍
    GET 和 POST 两种方式来完成Http接口
    mvc Web api 如何在控制器中调用
    c#怎么获取当前页面的url
    MVC3缓存:使用页面缓存
    十大排序算法梳理
    浅谈设计模式——工厂模式
    Java 中的 反射机制
    浅谈设计模式——单例模式
  • 原文地址:https://www.cnblogs.com/jdwang-admin/p/7866330.html
Copyright © 2011-2022 走看看