我从 PHP 中的 API 接收 lan 和 lot 数据,并使用($jsonData[0]['lat']);
和从响应中提取它们$jsonData[0]['lon']);
。如何将它们添加为我的两个 MySQL 列location
作为 POINT 值?
1 回答
MySQL's geometry stuff offers the ST_GeomFromText()
function. You can use it like this.
ST_GeomFromText('POINT(40.7488 -73.9854)')
Notice that 'POINT(40.7488 -73.9854)'
is a text string, and that there's no comma between the two numbers.
If you do something like this to make one of those text strings
$pointText = "POINT(" . $jsonData[0]['lat'] . " " . $jsonData[0]['lon'] . ")";
then you can do an insert like this:
INSERT INTO tbl (geocolumn) VALUES (ST_GeomFromText(?));
with $pointText as the parameter.
With MySQL versions prior to 8, I agreed with @El_Vanja's suggestion of using separate FLOAT
columns for lat / long. But with version 8, MySQL has the ability to handle spherical as well as cartesian coordinate systems, so the spatial index can be very useful.