Browse Source

计算距离

master
Lemon 5 years ago
parent
commit
b6a7b9f16c
  1. 11
      app/Service/v3/Implementations/LocationService.php
  2. 8
      app/Service/v3/Implementations/UserAddressService.php

11
app/Service/v3/Implementations/LocationService.php

@ -2,6 +2,8 @@
namespace App\Service\v3\Implementations;
use App\Constants\v3\ErrorCode;
use App\Exception\ErrorCodeException;
use App\Service\v3\Interfaces\LocationServiceInterface;
use App\Model\v3\Area;
use App\Model\v3\Market;
@ -88,6 +90,13 @@ class LocationService implements LocationServiceInterface
*/
$type = 'bicycling';
$key = 'GB3BZ-7W2CU-LW3VH-B7ZIF-XRKSF-D3FOL';
return file_get_contents($url.$type.'/?from='.$lat1.','.$lng1.'&to='.$lat2.','.$lng2.'&output=json&key='.$key);
$result = file_get_contents($url.$type.'/?from='.$lat1.','.$lng1.'&to='.$lat2.','.$lng2.'&output=json&key='.$key);
$resultArr = json_decode($result,true);
if($resultArr['status'] > 0)
{
throw new ErrorCodeException(ErrorCode::LOCATION_ERROR,$resultArr['message']);
}
$distance = $resultArr['result']['routes'][0]['distance'];
return $distance;
}
}

8
app/Service/v3/Implementations/UserAddressService.php

@ -75,13 +75,7 @@ class UserAddressService implements UserAddressServiceInterface
{
$address = $this->get($userAddressId);
$market = Market::query()->select('lng','lat')->find($marketId);
$result = $this->locationService->getDistanceByTencent($address->lng,$address->lat,$market->lng,$market->lat);
$resultArr = json_decode($result,true);
if($resultArr['status'] > 0)
{
throw new ErrorCodeException(ErrorCode::LOCATION_ERROR,$resultArr['message']);
}
$distance = $resultArr['result']['routes'][0]['distance'];
$distance = $this->locationService->getDistanceByTencent($address->lng,$address->lat,$market->lng,$market->lat);
$distributionRrice = $this->calculateDistributionRrice($distance);
$res['address'] = $address;
$res['distribution_price'] = $distributionRrice;

Loading…
Cancel
Save