一聚教程网:一个值得你收藏的教程网站

热门教程

php版微信JS-SDK地理位置取街景实例

时间:2022-06-24 17:48:13 编辑:袖梨 来源:一聚教程网

根据《微信JS-SDK地理位置接口例子》中的QQMapModel和ImageCacheModel类进行扩展。看了下腾讯地图有个静态图的V2版,我顺便也加上去,继续围绕腾讯地图把取街景的接口写上。由于是Demo,像取街景有几个参数可以自行定义,我只用默认!不多说看码吧
QQMapModel.class.php: (注:API_KEY 用QQ在官方申请,目前免费)
namespace HomeModel;
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);
        }
    }

热门栏目