从ruby到Ecto Elixir的地理查询复制

从ruby到Ecto Elixir的地理查询复制,ruby,elixir,geo,ecto,Ruby,Elixir,Geo,Ecto,在Ruby中,我有这个函数query.by_distance其中asby_distance类似于 def_dataset_method(:by_distance) do |from, meters| point = Geocoding.as_point(from) query = "ST_DWithin(location, ST_SetSRID(ST_Point(?, ?), 4326)::geography, ?)" where(query, point.lng,

在Ruby中,我有这个函数
query.by_distance
其中as
by_distance
类似于

  def_dataset_method(:by_distance) do |from, meters|
    point = Geocoding.as_point(from)
    query = "ST_DWithin(location, ST_SetSRID(ST_Point(?, ?), 4326)::geography, ?)"
    where(query, point.lng, point.lat, meters)
  end
在ruby端点中,用户正在传递两个值,它们是
is\u near\u to
,主要是一个城市或国家的名称。。通过它,
Geecoding
获取其点,2值为
在_距离内
,这是
,用于获取该
距离内的摄像机

上述情况在Ruby中发生

我在长生不老药中做的是复制它

  def by_distance(query, is_near_to, within_distance) do
    [%{"lat" => lat, "lng" => lng}] = fetch(is_near_to)
    latitude = lat
    longitude = lng
    query
    |> where([cam], st_dwithin(cam.location, st_set_srid(st_point_from_text(^"#{latitude},#{longitude}"), 4326), ^within_distance))
  end
为了得到时间,我做了一些事情

defmodule EvercamMedia.Geocode do

  def fetch(address) do
    response = HTTPotion.get "http://maps.googleapis.com/maps/api/geocode/json?address=#{URI.encode(address)}&sensor=false"

    {:ok, results} = Poison.decode response.body

    get_in(results, ["results", Access.all(), "geometry", "location"])
  end
end

现在我有了相同的场景。我有两个值
在_附近
在_距离之内
。但是我完全不知道我如何在查询中复制我们在项目和文档中使用的相同内容,这显然是可能的,但我不知道怎么做。

这是我正在开发的应用程序的一部分,我相信它能满足您的需要,它使用一个片段将ST_distance_sphere调用直接传递给postgis

使用图书馆和postgis

模型中

def near_by(query, point, distance) do
  from place in query,
  where: fragment("ST_distance_sphere(?,?)", place.location, ^point) >= ^distance,
  order_by: fragment("ST_distance_sphere(?,?)", place.location, ^point),
  select: {place, fragment("ST_distance_sphere(?,?)", place.location, ^point)}
end
控制器中

def near_by(conn, %{"latitude" => latitude, "longitude" => longitude, "distance" => distance}) do
  point = %Geo.Point{coordinates: {String.to_float(latitude), String.to_float(longitude)}, srid: 4326}
  places = Place
  |> Place.near_by(point, String.to_float(distance))
  |> Repo.all
  render(conn, "near_by.json", places: places)
end
视图中

def render("near_by_place.json", %{place: place}) do
  { place, distance } = place
  {lat, lon} = place.location.coordinates
  %{id: place.id,
    name: place.name,
    latitude: lat,
    longitude: lon,
    distance: distance}
end