开发者

How to use a Point or Geometry type in Datamapper (codeigniter)

I'm using codeigniter and datamapper to build an API service (on mysql), and part of it involves geolocation - 开发者_如何学Gohowever I can't seem to find a workaround to use the point datatype. If I try to insert GeomFromText, the system treats it as a string.

Can anyone help?

Cheers, Mark.


Try

CREATE TABLE Points ( 
    ID INT(10) PRIMARY KEY NOT NULL AUTO_INCREMENT, 
    location POINT NOT NULL, 
    SPATIAL INDEX(location) 
) ENGINE= MYISAM

In CodeIgniter:

$this->db->set("location",'geomfromtext("POINT(30.2 40.3)")',false);
$this->db->insert("Points");

In CodeIgniter with Datamaper (see help for "Using Formulas in Updates" in the documentation)...

$point = new Point();
$point->update('location','geomfromtext("POINT(30.2 40.3)")',FALSE);


As explained in the CodeIgniter User's Guide:

$this->db->set();

set function enables you to set values for inserts or updates.

set() will accepts a third parameter ($escape), that will prevent data from being escaped if set to FALSE.

$this->db->set('geo', "ST_GeomFromText('{$data['geo']}')", FALSE);
$this->db->insert('tableName');

return $this->db->insert_id() ? $this->db->insert_id() : FALSE;


Use FLOAT as your database datatype.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜