Storing latitude and longitude Laravel

Asked

Viewed 647 times

-1

I am storing in the database the latitude and longitude. When returning this latitude it is returned as string. I used decimal casting to return a number with all decimals but continues to return as string.

Latitude: -21.21973152
Longitude: -47.83068889

Migration:

$table->decimal('latitude', 10, 8)->nullable();
$table->decimal('longitude', 11, 8)->nullable();

Model:

protected $casts = [
    'latitude' => 'decimal:10',
    'longitude' => 'decimal:11',
];

Controller:

public function edit(Company $company)
{
    dd(gettype($company->latitude)); //Retorna "string"

}

Documentation that I followed: https://laravel.com/docs/6.x/eloquent-mutators#attribute-casting

About the type for latitude and longitude: https://stackoverflow.com/questions/12504208/what-mysql-data-type-should-be-used-for-latitude-longitude-with-8-decimal-places

3 answers

5

The guy decimal is represented as string. If you need the values in their numerical form, just do the cast for that guy:

protected $casts = [
    'latitude' => 'float',
    'longitude' => 'float',
];

Just keep in mind that by doing this you will get the rounding problems that naturally type float it possesses due to the limitations of storage of numbers. For latitude and longitude that will not interfere so much - until the point of not needing to be decimal the type of the column.

You can do the cast for real, float or double.

  • Floatusually causes problems in the display of latitude and longitude by removing the decimal places , I don’t think q will solve just change the type of the cast

  • @Erloncharles But that’s the limitation of cast or the exhibition?

  • From the show, the cast itself works perfectly

  • 2

    @Erloncharles Then the answer answers, doesn’t it? The cast is done without losing the decimal places, the question of truncation should be taken into account only when displaying the value, if necessary.

  • His problem is in the exhibition and not how to save in the bank

  • 2

    @Erloncharles No, the problem is that the spine is like decimal and he wondered to get a string in PHP. No truncation was cited as displayed in the question.

  • Not really in the question, but it iseriu in the comments

  • @Woss I actually researched and saw that it would return a string. But I needed to convert this to a number with all the decimal places that were stored. I tested as double and lost 2 decimal places. I tested in google maps and the location is correct. The problem is that I do not know if the loss of houses will have problem in other coordinates.

  • 1

    @Marcelo But why do you need this as a numeric? PHP is able to handle strings numbers naturally. And how did the loss of the decimals occur? This probably happened only when you went to display the value, not while doing the cast. Are separate problems.

  • @Woss I need to return as number pq I am using in a component Vue. By giving a dd($company->latitude) in Laravel I realized the loss.

  • @Marcelo, the main question is "the data is correct in the database?"

  • @Erloncharles Yes, in the bank is correct, with all decimal places. The problem is being the return of what is in the bank.

  • 1

    @Marcelo Entra no chat if you want to talk better. It seems like you’re complicating the problem too much.

  • @Just to close, I put in Migration the type double('latitude', 10, 8) and did not need to use the $Casts from Laravel. Apparently everything’s okay with the display on the map. What confused me was the question of stackoverflow q I put in the question that spoke to use decimal.

  • @Marcelo Yes, when you use double in the bank Eloquent already brings you the value as double in PHP. The "problem" (which is no problem) is that the decimal type in the database is represented as string in PHP and that’s what caused you confusion - and it’s used string precisely to avoid the limitations of numerical types.

Show 10 more comments

1


Recalling that the $casts it is optional it is not mandatory to spend you choose, if you start eating numbers try to increase the length column.

Migration:

$table->double('latitude', 10, 8)->nullable();
$table->double('longitude', 11, 8)->nullable();

Model:

protected $casts = [
    'latitude' => 'double',
    'longitude' => 'double',
];

-1

According to these definitions, we have the following description of the accuracy of the decimal places of latitude and longitude:

The digit of unit (a decimal degree) gives the position up to 111 kilometers (60 nautical miles, or 69 miles). It tells us what country we are in approximately.

The first decimal place goes up to 11.1 km: it differentiates the position of a large city from another large neighboring city.

The second decimal place goes up to 1.1 km: it separates one village from another.

The third decimal place up to 110m: it identifies a huge agricultural field or an institutional campus.

Fourth to every decimal goes up to 11m: it identifies a portion of earth. It is comparable to the common precision of a GPS unit without any interference.

The fifth decimal place goes up to 1.1m: it differentiates one tree from another. The precision of this level in commercial GPS can only be achieved with differential correction.

The sixth decimal place Worth up to 0.11 m: you can see the structure in detail, to design landscapes or build streets. It should be more than enough to monitor glacier and river movements. This can only be measured with very robust GPS.bold text

From the seventh decimal place it comes to millimeters and nanometers, so you don’t have a need for more than 6 houses to save and recover geolocations with an acceptable accuracy.

Completion

The exchange of the $casts is really the best way to treat your problem

That said, you have two possibilities to be able to work the return correctly on your $casts:

  1. Change the type to:
    protected $casts = [
        'latitude' => 'float', // pode ser real, float ou double 
        'longitude' => 'float', // pode ser real, float ou double 
    ];
  1. Or just convert into your controller to decimal using (decimal)$company->latitude

The second possibility solves the problem, even if it is the least recommended of the two.

Browser other questions tagged

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