簡體   English   中英

Typeorm Postgis 按最近點排序

[英]Typeorm Postgis order by nearest Point

我有一個包含一堆點的 postgis 數據庫,我想按照到我自己的 position(經度和緯度)的距離順序獲取這些點。

所以我想做類似的事情:

const markers = await this.markerRepo.find({
  where: {
    type: "test"
  },
  order: { position: 'ASC' },
});

但我想根據我目前的 position 訂購它們。

這 id 我的表實體(Nestjs):

@Entity()
 export class Marker {
   @PrimaryGeneratedColumn()
   id: number;

   @Column({
     type: 'geography',
     spatialFeatureType: 'Point',
     srid: 4326,
     nullable: true,
   })
   position: Point;
 
   @Column()
   type: string;
 }

如果您需要更多信息,請告訴我

原來 Typeorm 不支持 Postgis(誰會想到)

解決方案是像這樣發送 SQL 請求:

const markers = await this.markerRepo.query(
  `SELECT * , ST_Distance(ST_MakePoint(${yourLatitude}, ${yourLongitude} ), position) AS dist FROM marker ORDER BY dist LIMIT 10;`,
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM