How to use a Point or Geometry type in Datamapper

2019-08-01 03:51发布

I'm using codeigniter and datamapper to build an API service (on mysql), and part of it involves geolocation - however 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.

3条回答
Fickle 薄情
2楼-- · 2019-08-01 04:18

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);
查看更多
戒情不戒烟
3楼-- · 2019-08-01 04:22

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;
查看更多
虎瘦雄心在
4楼-- · 2019-08-01 04:22

Use FLOAT as your database datatype.

查看更多
登录 后发表回答