Coder Social home page Coder Social logo

axkuechler / laravel-geoly Goto Github PK

View Code? Open in Web Editor NEW
42.0 1.0 2.0 15 KB

Perform fast and efficient radius searches on your Laravel Eloquent models.

License: MIT License

PHP 100.00%
laravel latitude longitude eloquent geolocation distance-calculation distance radius search model

laravel-geoly's Introduction

Laravel Geoly

Packagist Version PHP from Packagist StyleCI GitHub

Perform fast and efficient radius searches on your Laravel Eloquent models.

Laravel Geoly provides a convenient way for your Laravel Eloquent models to query in a certain radius around a position. It is lightning fast by using a bounding box to cut down the possible results and calculating the distance only on the remaining subset. Laravel Geoly works on both MySQL and PostgreSQL.

Requirements

  • PHP 7.1+
  • Laravel 5+
  • Tested on MySQL and PostgreSQL

Installation

Simply require the project via composer:

$ composer require akuechler/laravel-geoly

How to use

Geoly assumes the two columns latitude and longitude on your eloquent model. Simply add them to your migration if not present yet.

$table->double('latitude');
$table->double('longitude');

If you prefer to use other names for your database columns, specify them in your model.

const LATITUDE  = 'lat';
const LONGITUDE = 'lng';

Use the Geoly package within your Eloquent model.

class YourModel extends Model
{
    use Geoly;
    ...
}

To search for all models within a specific radius around a position, add the radius scope to your query.

$query = YourModel::radius($latitude, $longitude, $radius);
$query->get();

Credits

This project is heavily inspired by Laravel Geographical and Movable Type Scripts article on Selecting points within a bounding circle.

laravel-geoly's People

Contributors

axkuechler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

laravel-geoly's Issues

I get error when -66.1188918 longitude is negative

Illuminate\Database\QueryException
SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at end of input LINE 1: ...itude))*cos(radians(longitude)-?)) * ? < ? order by distance ^ (SQL: select *, acos(sin(0.3222817973143)*sin(radians(latitude)) + cos(0.3222817973143)*cos(radians(latitude))*cos(radians(longitude)--1.1539923596799)) * 6371 As distance from (select * from "rooms" where "latitude" between 18.375454639408 and 18.555318960592 and "longitude" between -66.213705462789 and -66.024078137211) as "rooms" where acos(sin(0.3222817973143)*sin(radians(latitude)) + cos(0.3222817973143)*cos(radians(latitude))*cos(radians(longitude)--1.1539923596799)) * 6371 < 10 order by distance)

Numeric value out of range with Postgres 12

Hi.

SQLSTATE[22003]: Numeric value out of range: 7 ERROR: input is out of range (SQL: select *, acos(sin(1.0395159958754)*sin(radians(latitude)) + cos(1.0395159958754)*cos(radians(latitude))*cos(radians(longitude)-0.52601709333789)) * 6371 As distance from (select * from "cities" where "latitude" between 57.761236088163 and 61.358522511837 and "longitude" between 26.587818705252 and 33.689300094748) as "cities" where acos(sin(1.0395159958754)*sin(radians(latitude)) + cos(1.0395159958754)*cos(radians(latitude))*cos(radians(longitude)-0.52601709333789)) * 6371 < 200 and "size" >= 2 order by distance, "size" desc limit 50

Latitude: 59.5598793
Longitude: 30.1385594

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.