微信小程式 計算兩地之間的距離 + 高德地圖獲取經緯度 + elasticsearch

lishasha發表於2022-03-13

高德地圖

composer require guzzlehttp/guzzle

config中經緯度轉化

<?php
return[
//    把地址轉化為經緯度
'geocode'=>'https://restapi.amap.com/v3/geocode/geo?key=f814bbfcb1fb9feb27a0887fbc06ad77&address=%s&city=%s&output=json'
];

微信小程式 計算兩地之間的距離  +  高德地圖獲取經緯度

封裝函式 計算距離

  distance: function (la1, lo1, la2, lo2) {
      var La1 = la1 * Math.PI / 180.0;
      var La2 = la2 * Math.PI / 180.0;
      var La3 = La1 - La2;
      var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
      var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
      s = s * 6378.137;
      s = Math.round(s * 10000) / 10000;
      s = s.toFixed(2);
      return s;
    },

傳送請求查詢目的地經度緯度 獲取當前位置 使用方法

 onLoad: function (options) {
      let _this = this
        let id = options.id;
        wx.request({
          url: 'http://www.week.com/index.php/msg',
          data:{id:id},
          success(e){
                let data = e.data.data;
                let latitude= e.data.data.latitude;
                let longitude=e.data.data.longitude;
                wx.getLocation({
                  success(e){                                                                                     
                    let la = e.latitude
                    let lo = e.longitude
                    let distance = _this.distance(la,lo,latitude,longitude)
                    _this.setData({distance})
                  }
                })
                _this.setData({data,latitude,longitude})
            }  
        })
}

安裝外掛

在您的composer.json檔案中包含elasticsearch-php
  {
      "require": {
          "elasticsearch/elasticsearch": "~7.0"
      }
  }

comoposer require elasticsearch/elasticsearch
更新
composer update
//ES客戶端連結
    private $client;

    /**
     * 建構函式
     * MyElasticsearch constructor.
     */
    public function __construct()
    {
        $params = array(
            '127.0.0.1:9200'
        );
        $this->client = ClientBuilder::create()->setHosts($params)->build();
    }
/**
     * 判斷索引是否存在
     * @param string $index_name
     * @return bool|mixed|string
     */
    public function exists_index($index_name = 'test_ik')
    {
        $params = [
            'index' => $index_name
        ];

        try {
            return $this->client->indices()->exists($params);
        } catch (\Elasticsearch\Common\Exceptions\BadRequest400Exception $e) {
            $msg = $e->getMessage();
            $msg = json_decode($msg,true);
            return $msg;
        }
    }
/**
     * 建立索引
     * @param string $index_name
     * @return array|mixed|string
     */
    public function create_index($index_name = 'test_ik') { // 只能建立一次
        $params = [
            'index' => $index_name,
            'body' => [
                'settings' => [
                    'number_of_shards' => 5,
                    'number_of_replicas' => 0
                ]
            ]
        ];

        try {
            return $this->client->indices()->create($params);
        } catch (\Elasticsearch\Common\Exceptions\BadRequest400Exception $e) {
            $msg = $e->getMessage();
            $msg = json_decode($msg,true);
            return $msg;
        }
    }
 /**
     * 刪除索引
     * @param string $index_name
     * @return array
     */
    public function delete_index($index_name = 'test_ik') {
        $params = ['index' => $index_name];
        $response = $this->client->indices()->delete($params);
        return $response;
    }
/**
     * 新增文件
     * @param $id
     * @param $doc ['id'=>100, 'title'=>'phone']
     * @param string $index_name
     * @param string $type_name
     * @return array
     */
    public function add_doc($id,$doc,$index_name = 'test_ik',$type_name = 'goods') {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id,
            'body' => $doc
        ];

        $response = $this->client->index($params);
        return $response;
    }
/**
     * 判斷文件存在
     * @param int $id
     * @param string $index_name
     * @param string $type_name
     * @return array|bool
     */
    public function exists_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id
        ];

        $response = $this->client->exists($params);
        return $response;
    }
/**
     * 獲取文件
     * @param int $id
     * @param string $index_name
     * @param string $type_name
     * @return array
     */
    public function get_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id
        ];

        $response = $this->client->get($params);
        return $response;
    }
/**
     * 更新文件
     * @param int $id
     * @param string $index_name
     * @param string $type_name
     * @param array $body ['doc' => ['title' => '蘋果手機iPhoneX']]
     * @return array
     */
    public function update_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods', $body=[]) {
        // 可以靈活新增新欄位,最好不要亂新增
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id,
            'body' => $body
        ];

        $response = $this->client->update($params);
        return $response;
    }
/**
     * 刪除文件
     * @param int $id
     * @param string $index_name
     * @param string $type_name
     * @return array
     */
    public function delete_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id
        ];

        $response = $this->client->delete($params);
        return $response;
    }
  * 搜尋文件 (分頁,排序,權重,過濾)
  * @param string $index_name
  * @param string $type_name
  * @param array $body
    public function search_doc($index_name = "test_ik",$type_name = "goods",$body=[]) {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'body' => $body
        ];

        $results = $this->client->search($params);
        return $results;
    }
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章