Location Field
This field is a first pass. We plan to extend it with bounding boxes, polygons, zooms, circles, etc. It will also get a lot easier once we update Laravel to 5.5/5.6.
We're using Grimzy's laravel-mysql-spatial package. You might want to take a look at that for more info on accessing the data that is stored.
You'll need to do some extra things for these fields:
- Add
Grimzy\LaravelMysqlSpatial\SpatialServiceProvider::class,toprovidersinconfig/app.php. Use the
Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTraittrait on your modeluse Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait; class Thing extends Model { use SpatialTrait; // ... }Add the relevant fields to
spatialFieldson your model. E.g.protected $spatialFields = ['map_location'];Location fields should be the
POINTcolumn type. This is not available in Laravel's Schema builder in Laravel v5.4 so you will have to write this migration manually. E.g.public function up() { DB::statement('ALTER TABLE `things` ADD location POINT'); } public function down() { DB::statement('ALTER TABLE `things` DROP COLUMN location'); }In your crud configs you might want to set the name of the column that the data is stored in, otherwise the default will be
map_location. E.g.:(new LocationField('location')) ->setLocationColumn('location'),This is a little weird but should allow us to make map fields that let you set a location AND, say, a boundary box and store them in separate columns.
Accessing Location Data
If your location column is called map_location, you can access it from your modal like so:
```php
$foo = $modal->map_location;
```
This will give you a namespace Grimzy\LaravelMysqlSpatial\Types\Point object, from which you can get the individual lat/lng values:
```php
$foo->getLat();
$foo->getLng();
```
Coordinate Inputs
From v0.2.19 onwards you can display the coordinates below the map
```php
$my_map_field->setShowCoordFields(true)
```
Migrating
You might have separate latitude and longitude columns. Here's an example migration.
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::statement('ALTER TABLE `places` ADD location POINT');
DB::statement('UPDATE places SET location = POINT(longitude, latitude)');
Schema::table('places', function (Blueprint $table) {
$table->dropColumn('longitude');
$table->dropColumn('latitude');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('places', function (Blueprint $table) {
$table->double('latitude', 12, 9)->nullable();
$table->double('longitude', 12, 9)->nullable();
});
DB::statement('UPDATE places SET longitude = X(location), latitude = Y(location)');
DB::statement('ALTER TABLE `places` DROP COLUMN location');
}