My goal is to use mysql POINT (lat, long) to find the closest objects in the database. I am trying to do something like the bottom of this tutorial http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL . Here is what I got:
Table:
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 some test data:
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');
Distance function declaration:
DELIMITER $$ CREATE FUNCTION `distance` (a POINT, b POINT) RETURNS double DETERMINISTIC BEGIN RETURN round(glength(linestringfromwkb(linestring(asbinary(a), asbinary(b))))); END $$ DELIMITER;
Trying to use function to search ex .:
SELECT name, distance(mark.loc, GeomFromText( ' POINT(31.5 42.2) ' )) AS cdist FROM mark ORDER BY cdist limit 10;
or
SELECT DISTINCT dest.name, distance(orig.loc, dest.loc) as sdistance FROM mark orig, mark dest having sdistance < 10 ORDER BY sdistance limit 10;
The problem I get is: ERROR 1367 (22007): Invalid non-geometric value aswkb (a @ 0) found during parsing, or ERROR 1416 (22003): Unable to get the geometry object from the data sent to the GEOMETRY field.
I canβt figure out how to solve this. The important thing is that the distance function can be used dynamically.
I also tried this solution: Find the distance between two points in MYSQL. (using dot type)
This is my version of mysql mysql Ver 14.14. Distribution 5.5.23 for Linux (x86_64) using readline 5.1
Hope someone can help me. Hurrah!