PHP code example of nashgao / hyperf-mysql-spatial
1. Go to this page and download the library: Download nashgao/hyperf-mysql-spatial library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nashgao / hyperf-mysql-spatial example snippets
namespace App;
use Nashgao\HyperfMySQLSpatial\Eloquent\SpatialTrait;
use Hyperf\Database\Model\Model;
/**
* @property \Nashgao\HyperfMySQLSpatial\Types\Point $location
* @property \Nashgao\HyperfMySQLSpatial\Types\Polygon $area
*/
class Place extends Model
{
use SpatialTrait;
protected $fillable = [
'name'
];
protected $spatialFields = [
'location',
'area'
];
}
use Nashgao\HyperfMySQLSpatial\Types\Point;
use Nashgao\HyperfMySQLSpatial\Types\Polygon;
use Nashgao\HyperfMySQLSpatial\Types\LineString;
$place1 = new Place();
$place1->name = 'Empire State Building';
// saving a point
$place1->location = new Point(40.7484404, -73.9878441); // (lat, lng)
$place1->save();
// saving a polygon
$place1->area = new Polygon([new LineString([
new Point(40.74894149554006, -73.98615270853043),
new Point(40.74848633046773, -73.98648262023926),
new Point(40.747925497790725, -73.9851602911949),
new Point(40.74837050671544, -73.98482501506805),
new Point(40.74894149554006, -73.98615270853043)
])]);
$place1->save();
use Nashgao\HyperfMySQLSpatial\Types\Point;
use Nashgao\HyperfMySQLSpatial\Types\Polygon;
use Nashgao\HyperfMySQLSpatial\Types\LineString;
$place1 = new Place();
$place1->name = 'Empire State Building';
// saving a point with SRID 4326 (WGS84 spheroid)
$place1->location = new Point(40.7484404, -73.9878441, 4326); // (lat, lng, srid)
$place1->save();
// saving a polygon with SRID 4326 (WGS84 spheroid)
$place1->area = new Polygon([new LineString([
new Point(40.74894149554006, -73.98615270853043),
new Point(40.74848633046773, -73.98648262023926),
new Point(40.747925497790725, -73.9851602911949),
new Point(40.74837050671544, -73.98482501506805),
new Point(40.74894149554006, -73.98615270853043)
])], 4326);
$place1->save();