最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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);
}
}
相关文章
- 炉石传说兑换码大全 12-26
- 重返未来1999趋光性研究夜幕之外怎么玩 趋光性夜幕之外活动介绍 12-26
- 光遇12.26大蜡烛在哪里 光遇12月26日大蜡烛位置攻略 12-26
- 江南百景图金谷园怎么样 江南百景图金谷园建筑介绍 12-26
- 光遇12.26每日任务怎么做 光遇12月26日每日任务做法攻略 12-26
- 光遇12.26季节蜡烛在哪里 光遇12月26日季节蜡烛位置攻略 12-26