LoginSignup
2
2

More than 5 years have passed since last update.

parse-ruby-clientでGeoQueriesを扱う

Last updated at Posted at 2015-06-07

はじめに

ParseのサードパーティSDKに、parse-ruby-clientがあります。
ParseからRuby用として紹介されています。
https://github.com/adelevie/parse-ruby-client

parse-ruby-clientはGeoPointは扱えますが、GeoQueriesについてはTODOになっており、現状では非対応です。コミットを見ると、開発が一時停止してたようです。
https://github.com/adelevie/parse-ruby-client#geoqueries

SDKはREST APIのラッパーなので、parse-ruby-clientからGeoQueriesを扱えるようにパラメータを追加して対応してみましょう。

必要なパラメータ

GeoQueriesを扱う際に必要なパラメータは、1つだけ、$nearSphereです。

$nearSphere

これだけでもOKですが、$maxDistanceInMilesなどの内1つを付けると、中心点からの指定した距離に限定できます。

$maxDistanceInMiles
$maxDistanceInKilometers
$maxDistanceInRadians

これらパラメータはParseのドキュメントで解説されています。
https://parse.com/docs/rest/guide#geopoints-geo-queries

parse-ruby-clientからリクエストする

それでは、$nearSphere付きでリクエストしてみましょう。
GeoPointデータがpositionに入っているとします。
最初に書いてあるのは、GeoQueriesで検索する対象のGeoPointです。

value = Parse::GeoPoint.new({
                                "latitude" => 35.0,
                                "longitude" => 130.0
                            })
gq.add_constraint("position", { "$nearSphere" => Parse.pointerize_value(value) })

ある程度の距離で限定する

前述の中心点からある程度の距離に限定してみましょう。同じ手順で\$maxDistanceInMilesなどを付加するだけです。
下は$maxDistanceInMilesで3マイルに限定しています。

gq.add_constraint("position", { "$maxDistanceInMiles" => Parse.pointerize_value(3) })

レスポンス数を絞る

距離は限定しましたが、レスポンスの数を絞りたいことがあります。その際は、(他の場合でも使われる)limitがGeoQueriesでも有効です。

qb.limit = 10

まとめ

parse-ruby-clientからGeoQueriesを扱う際のまとめです。
\$nearSphereと$maxDistanceInMiles、そしてlimitで数を10に絞った状態です。
Parseのクラス名はpositionsとします。

parse.rb
value = Parse::GeoPoint.new({
                                "latitude" => 35.0,
                                "longitude" => 130.0
                            })

gq = Parse::Query.new("positions")

gq.add_constraint("position", { "$nearSphere" => Parse.pointerize_value(value) })
gq.add_constraint("position", { "$maxDistanceInMiles" => Parse.pointerize_value(3) })
gq.limit = 10

gq.get.each do |r|
  p r["position"].to_json
end
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2