An Interest In:
Web News this Week
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
April 25, 2022 04:05 pm GMT
Original Link: https://dev.to/bibekkakati/how-to-store-coordinates-in-mysql-31la
How to store coordinates in MySQL
Many times we capture the geo-location of users and store in the database for different use cases.
I have seen that most of the developers use multiple fields to store the latitude and longitude separately likeTable_name(field1, field2, ..., latitude, longitude)
.
In this short article we will see an alternative way of storing coordinates in MySQL database using the spatial data types like POINT
.
Create a table
- Let us create a table named
locations
. - Field
coordinates
of data typePOINT
.
CREATE TABLE locations ( id INT(11) NOT NULL AUTO_INCREMENT, coordinates POINT, PRIMARY KEY (id));
Insertion of coordinates
- To insert/update the field
coordinates
, we need to prepare a string like this'POINT(latitude longitude)'
. - Then we will use the in-built function called
ST_GeomFromText
to create a geometry in given SRID from WKT specification. - Pass the prepared string of points into
ST_GeomFromText
function.
INSERT INTO locations (coordinates) VALUES (ST_GeomFromText('POINT(21.67890 91.54789)');
Table will store and display the data in the following way
id coordinates1 POINT(21.67890 91.54789)
Originally published on blog.bibekkakati.me
Also, check this out to know how to capture user's geo-location in web browser.
Thank you for reading
If you enjoyed this article or found it helpful, give it a thumbs-up
Feel free to connect
Original Link: https://dev.to/bibekkakati/how-to-store-coordinates-in-mysql-31la
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To