php版微信JS-SDK地理位置取街景实例
发布:smiling 来源: PHP粉丝网 添加日期:2018-10-30 10:14:38 浏览: 评论:0
根据《微信JS-SDK地理位置接口例子》中的QQMapModel和ImageCacheModel类进行扩展。看了下腾讯地图有个静态图的V2版,我顺便也加上去,继续围绕腾讯地图把取街景的接口写上。由于是Demo,像取街景有几个参数可以自行定义,我只用默认!不多说看码吧
QQMapModel.class.php: (注:API_KEY 用QQ在官方申请,目前免费)
- namespace Home\Model;
- class QQMapModel {
- const
- PANO_API = 'http://apis.map.qq.com/ws/streetview/v1/getpano',
- API_KEY = 'CZQBZ-RC53V-2RQPX-UFNBE-FCH2J-DF00';
- static public function call($url, array $params = null) {
- $url = $url.'?'.http_build_query($params);
- $ch = curl_init($url);
- curl_setopt_array($ch, array(
- CURLOPT_RETURNTRANSFER => 1,
- CURLOPT_FOLLOWLOCATION => 1,
- CURLOPT_AUTOREFERER => 1,
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_SSL_VERIFYPEER => 0,
- CURLOPT_VERBOSE => 1,
- ));
- $result = curl_exec($ch);
- if (curl_errno($ch)) {
- return false;
- }
- curl_close($ch);
- return $result;
- }
- //新静态图v2接口
- static function staticMap($point, $otherParam = array()) {
- $pos = explode(',', $point);
- $posStr = $pos[1].','.$pos[0];
- $param = array(
- 'size' => '620*380',
- 'center' => $posStr,
- 'zoom' => 13,
- 'format' => 'png',
- 'maptype' => 'roadmap',
- 'markers' => $posStr,
- 'key' => self::API_KEY,
- );
- if(count($otherParam))
- $param = array_merge($param, $otherParam);
- return 'http://apis.map.qq.com/ws/staticmap/v2/?' . http_build_query($param);
- }
- //取街景图接口
- static function streetView($pano, $otherParam = array()) { //max 960x640
- $param = array(
- 'size' => '620x380',
- 'pano' => $pano,
- 'heading' => 0,
- 'pitch' => 0,
- 'key' => self::API_KEY,
- );
- if(count($otherParam))
- $param = array_merge($param, $otherParam);
- return 'http://apis.map.qq.com/ws/streetview/v1/image?' . http_build_query($param);
- }
- //街景图的ID接口
- static function getPano($location, $otherParam = array()) {
- $param = array(
- 'location' => $location,
- 'radius' => 200,
- 'output' => 'json',
- 'key' => self::API_KEY,
- );
- if(count($otherParam))
- $param = array_merge($param, $otherParam);
- $result = self::call(self::PANO_API, $param);
- if ($result) {
- return json_decode($result, 1);
- }
- return false;
- }
- //静态图v1版接口
- static function mapImage($point, $otherParam = array()) {
- $param = array(
- 'size' => '620*380',
- 'center' => $point,
- 'zoom' => 13,
- 'format' => 'png',
- 'markers' => $point,
- );
- if(count($otherParam))
- $param = array_merge($param, $otherParam);
- return 'http://st.map.qq.com/api?' . http_build_query($param);
- }
- }
- ImageCacheModel类:(只是在上篇教程上加多个静态方法处理街景的缓存)
- public static function getStreetCacheImg($points) {
- $fileName = md5($points);
- self::$FULL_CACHE_DIR = C('PUBLIC_FULL_DIR').self::CACHE_DIR;
- $cacheImg = self::$FULL_CACHE_DIR.'/'.$fileName.self::$TYPE;
- if(file_exists($cacheImg)) {
- return self::CACHE_DIR.$fileName.self::$TYPE;
- } else {
- $res = QQMapModel::getPano($points);
- if($res['status'] === 0) {
- $pano = $res['detail']['id'];
- $imageUrl = QQMapModel::streetView($pano);
- self::saveCacheImg($imageUrl, $fileName);
- return self::CACHE_DIR.$fileName.self::$TYPE;
- }
- return self::CACHE_DIR.'default'.self::$TYPE;
- }
- }
然后Controller里的处理:(至于Layout模版里的AJAX调用与上篇地理接口差不多,这里就不写了)
- public function streetpicAction() {
- layout(false);
- if(I('pos','')) {
- $target = ImageCacheModel::getStreetCacheImg(I('pos'));
- $url = __ROOT__.$target;
- redirect($url);
- }
- }
Tags: php版微信 JS-SDK
相关文章
- ·php版微信公共平台开发者认证实例(2014-07-13)
- ·php版本微信js-sdk支付接口类例子(2018-10-30)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)