Obtain approach radius from a geographical coordinate

Asked

Viewed 984 times

0

I have a PHP application that gets the latitude and longitude of an address, using the Google Maps API. The idea is to delimit a "tolerance" distance around this point to later validate whether the user is within the bounded radius.

From this one point I have, how can I get the coordinates of a radius of x meters around this point?

If there is no way to do this, how can I set a tolerance distance that the user may be from the set point?

OBS.: I don’t need to display lightning on the front end of the map (as shown here: How do I light a map with a proximation circumference?), but rather a way to, for example, calculate whether an A coordinate is within an x-metre radius of coordinate B.

  • 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

  • 1
  • @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.

  • 4

    @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.

  • 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. :)

1 answer

2


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.

  • 1

    Oops! I’m using Laravel in the same project. I didn’t know this library. But from what I read, it seems to solve my problem. I’ll implement it soon. For now, I will mark as an answer. Thank you very much for the tip, Kayo!

  • When testing your example, I’m getting the exception "Call to Undefined method App Adress::distanceSphere".

  • Friend on your model you imported the lib? use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait; E gave the use inside the model? class Model {
 use SpatialTrait;
}

  • Yes. I imported the lib and put the use Spatialtrait in Model, I used both the implementation of your example and the documentation. I also added Provider in config app.php. So I didn’t understand why the system didn’t recognize the function.

  • 1

    Solved. I updated the package with Composer update and it started working. For some reason, you must have installed an outdated version.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.