高德云图PHP Simple SDK

高德云图周边搜索接口中filter参数无效,有需要这个参数的人就别用了。

只封装了我用到的几个功能,其他功能对照着文档自己封装,
调用:

$lbs = new lbsyun('table_id', 'key');
// 创建一个新的Point
$lbs->create('名称', '纬度', '经度', Array('其他自定义字段'=>'值') );
// 搜索周边的Point
$result = $lbs->around(Array(
    'center'=>'longitidue,latitude',
    'page'=>'1',
    'limit'=>'50',
    '_sortrule'=>'_distance:1',
    'radius'=>'10000')
);

var_dump($result);

源码:

<?php

/**
 * 高德云图 SDK
 *
 * @author cevin<cevin.cheung(at)gmail.com>
 * @site <http://cevin.net>
 */
class lbsyun {

    const SERVICE_URL = 'http://yuntuapi.amap.com';
    private $table_id = '';
    private $key = '';

    private $error = null;

    public function __construct($table_id,$key) {
        $this->table_id = $table_id;
        $this->key = $key;
    }

    public function create($name,$lat,$lng,Array $param = array()) {
        $data = array();
        $data['_name'] = $name;
        $data['_location'] = "{$lng},{$lat}";
        $data = json_encode(array_merge($data,$param));

        $ret = $this->process('/datamanage/data/create',$data,'post');

        $ret = $this->parse($ret);

        return $ret['status'] == 1 ? (int)$ret['_id'] : false;
    }

    public function update($id,Array $data) {
        $data['_id'] = $id;
        $data = json_encode($data);

        $ret = $this->process('/datamanage/data/update',$data,'post');
        $ret = $this->parse($ret);

        return $ret['status'] == 1 ? true : false;
    }

    public function delete($id) {

        $data = array();
        $data['tableid'] = $this->table_id;
        $data['key'] = $this->key;
        $data['ids'] = $id;

        $ret = $this->request('/datamanage/data/delete',$data,'post');
        $ret = $this->parse($ret);
        return $ret['status'] == 1 ? true : false;
    }

    /**
     * 周边检索
     * @return array [description]
     */
    public function around(Array $data) {
        $url = '/around';
        return $this->search($url,$data,'get');
    }

    private function search($url,Array $data,$method) {
        $url = '/datasearch'.$url;

        $data['key'] = $this->key;
        $data['tableid'] = $this->table_id;

        return $this->parse($this->request($url,$data,$method));
    }

    private function process($url,$data,$method) {

        $basedata = array();
        $basedata['tableid'] = $this->table_id;
        $basedata['key'] = $this->key;
        $basedata['loctype'] = 1;
        $basedata['data'] = $data;

        return $this->request($url,$basedata,$method);
    }

    private function request($url,Array $data,$method='get') {

        $method = strtoupper($method);
        $data = $this->build_query($data);

        $opts = array();

        $opts[CURLOPT_URL] = self::SERVICE_URL.$url;
        $opts[CURLOPT_SSL_VERIFYPEER] = false;
        $opts[CURLOPT_SSL_VERIFYHOST] = 2;
        $opts[CURLOPT_RETURNTRANSFER] = 1;

        if ($method != 'GET') {
            $opts[CURLOPT_CUSTOMREQUEST] = $method;
            $opts[CURLOPT_POSTFIELDS] = $data;
        } else {
            $opts[CURLOPT_URL] = $opts[CURLOPT_URL].'?'.$data;
        }

        $ch = curl_init();
        curl_setopt_array($ch,$opts);
        $ret = curl_exec($ch);
        curl_close($ch);
        return $ret;
    }

    private function parse($data) {
        $data = json_decode($data,1);

        if (!is_array($data)) {
            $this->error = 'remote_service_error';
        } else {
            if ($data['status'] == 0) {
                $this->error = $data['info'];
            }
        }

        return $data;
    }

    private function build_query(Array $data) {
        $str = '';
        foreach ($data as $key=>$item) {
            $str .= "{$key}=".rawurlencode($item)."&";
        }

        return substr($str,0,-1);
    }
    public function error() {
        return $this->error;
    }
}
去打赏

您的支持将鼓励我们继续创作!

[微信] 扫描二维码打赏

发表评论

电子邮件地址不会被公开。