我的目标是使用 mysql POINT(lat,long) 在数据库中查找附近的实体。我正在尝试在本教程的底部做类似的事情http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL。这是我得到的:
桌子:
CREATE TABLE mark (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) DEFAULT NULL,
loc POINT NOT NULL,
SPATIAL KEY loc (loc)
) ENGINE=MyISAM;
插入一些测试数据:
INSERT INTO mark (loc,name) VALUES (POINT(59.388433,10.415039), 'Somewhere 1');
INSERT INTO mark (loc,name) VALUES (POINT(63.41972,10.39856), 'Somewhere 2');
声明距离函数:
DELIMITER $$
CREATE FUNCTION `distance`
(a POINT, b POINT)
RETURNS double DETERMINISTIC
BEGIN
RETURN
round(glength(linestringfromwkb(linestring(asbinary(a),
asbinary(b)))));
END $$
DELIMITER;
尝试使用该功能搜索前:
SELECT name, distance(mark.loc, GeomFromText( ' POINT(31.5 42.2) ' )) AS cdist
FROM mark
ORDER BY
cdist limit 10;
或者:
SELECT DISTINCT
dest.name,
distance(orig.loc, dest.loc) as sdistance
FROM
mark orig,
mark dest
having sdistance < 10
ORDER BY
sdistance limit 10;
我遇到的问题是: ERROR 1367 (22007): Illegal non geometry 'aswkb(a@0)' value found during paring, or ERROR 1416 (22003): Cannot get geometry object from data you sent to the GEOMETRY field
我似乎无法弄清楚如何解决这个问题。重要的是“距离”功能可以动态使用。
我也尝试过这个解决方案:Find the distance between two points in MYSQL。(使用点数据类型)
这是我的 mysql 版本 mysql Ver 14.14 Distrib 5.5.23,适用于 Linux (x86_64),使用 readline 5.1
希望有人的专业知识可以帮助我。干杯!