<?php
namespace App\Http\Controllers\Utils;
use App\Http\Controllers\Controller;
use Curl\Curl;
use Illuminate\Config\Repository;
/**
* Class AMap
* @author <fl140125@gmail.com>
* @package App\Http\Controllers\Utils
*/
class AMap extends Controller
{
protected static $instance;
protected $a_map_key;
protected $url;
protected $data = array();
protected $Curl;
private function __clone()
{
}
public static function getInstance()
{
if (!self::$instance instanceof self) {
self::$instance = new static();
}
return self::$instance;
}
public function __construct()
{
$this->a_map_key = config('app.a_map_key');
$this->url = 'https://restapi.amap.com/v3/';
$this->data = array('key' =>$this->a_map_key, 'output' =>'json');
$this->Curl = new Curl();
}
public function getWeather($ad_code, string $extensions = 'base')
{
try {
$weatherUrl = 'weather/weatherInfo';
$this->data['city'] = $ad_code;
$this->data['extensions'] = $extensions;
return $this->Curl->get($this->url.$weatherUrl, $this->data);
} catch (\Exception $exception) {
return ['code' => Code::SERVER_ERROR, 'message' => $exception->getMessage()];
}
}
public function getAddress(string $ipAddress = '127.0.0.1')
{
try {
$ipUrl = 'ip';
$this->data['ip'] = $ipAddress;
return $this->Curl->get($this->url.$ipUrl, $this->data);
} catch (\Exception $exception) {
return ['code' => Code::SERVER_ERROR, 'message' => $exception->getMessage()];
}
}
public function geoCode($city, string $address)
{
try {
$geoUrl = 'geocode/geo';
$this->data['city'] = $city;
$this->data['address'] = $address;
return $this->Curl->get($this->url.$geoUrl, $this->data);
} catch (\Exception $exception) {
return ['code' => Code::SERVER_ERROR, 'message' => $exception->getMessage()];
}
}
public function direction(string $origin, string $destination)
{
try {
$directionUrl = 'direction/walking';
$this->data['origin'] = $origin;
$this->data['destination'] = $destination;
return $this->Curl->get($this->url.$directionUrl, $this->data);
} catch (\Exception $exception) {
return ['code' => Code::SERVER_ERROR, 'message' => $exception->getMessage()];
}
}
public function inputTips(string $keywords, string $location, string $city)
{
try {
$assistantUrl = 'assistant/inputtips';
$this->data['keywords'] = $keywords;
$this->data['location'] = $location;
$this->data['city'] = $city;
return $this->Curl->get($this->url.$assistantUrl, $this->data);
} catch (\Exception $exception) {
return ['code' => Code::SERVER_ERROR, 'message' => $exception->getMessage()];
}
}
}