You will solve this using this library to the Laravel, she works with spatial data types on Mysql. This library has several methods for working with coordinates, polygons, distance and etc.
After following the documentation for installation and configuration of the same, you will have more or less this:
Migration of your address table or table that will own the lat and lng:
Schema::create('addresses', function (Blueprint $table) {
$table->increments('id');
$table->string('zipcode');
$table->integer('neighborhood');
$table->string('street');
$table->string('number');
$table->string('complement');
$table->string('landmark');
$table->point('location'); // Lat e Long
$table->tinyInteger('status')->default(1)->comment('0=Disabled, 1=Enabled');
$table->timestamps();
});
Model:
use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;
class Mall extends Model
{
use SpatialTrait;
protected $fillable = ['zipcode', 'neighborhood', 'street', 'number', 'complement', 'landmark', 'location', status];
}
Controller:
use App\Address; // Namespace do seu model
use Grimzy\LaravelMysqlSpatial\Types\Point;
class BikerQueueController extends Controller
{
public function calcRadius( Request $request )
{
$lat = $request->input( 'lat' );
$lng = $request->input( 'lng' );
// O método distanceSphere() é da lib instalada,
// você deve informar 3 parâmetros: distanceSphere($geometryColumn, $geometry, $distance); A distância deve ser informada em milhas.
$address = Address::distanceSphere( 'location', new Point( $lat, $lng ), 5000 )
->whereStatus( 1 ) // Aqui é um exemplo de que você pode usar os métodos padrões do seu model junto com os métodos da lib
->first();
return (bool)$address; // retorna true ou false
}
}
P.S: Please abstract the variables handling of the request and return method, the focus was only to show how you can do to know if a coordinate is within the radius of an address.
See if this resolves: https://answall.com/questions/213270/como-fa%C3%A7o-para-ciar-um-mapa-com-uma-circunfer%C3%Aancia-de-proxima%C3%A7%C3%A3o
– bfavaretto
Possible duplicate of How do I light a map with a proximation circumference?
– Woss
@bfavaretto This example shows how to circle graphically on the front end. But it still has only one coordinate. I need a way to verify that the coordinates of the user’s location are within this approximation radius.
– André Raubach
@Andréraubach Just calculate the distance from the point of the user to the center of the circumference. If the distance is less than the radius it will be inside her.
– Woss
Thanks, Anderson! After you spoke, it was even obvious. Kkkkk E to calculate, you can use Haversine’s formula to calculate the distance in a straight line. :)
– André Raubach