iOS
...
Data Objects
ParseSwift SDKを使用したParseGeoPointクエリの実践
8 分
ジオクエリ はじめに 私たちは「 ジオクエリ 」という用語を、条件が parsegeopoint parsegeopoint 型のフィールドを含むクエリの種類を指すために使用します。地理的な位置データをback4appデータベースに保存するには、 parsegeopoint parsegeopoint 構造体を使用することをお勧めします。 parseswift sdk parseswift sdk は、 parsegeopoint parsegeopoint データ型に適用される条件に従ってデータをクエリするための一連のメソッドを提供します。 前提条件 このチュートリアルを完了するには、次のものが必要です: back4appで 作成されたアプリ 。 クエリをテストするための基本的なiosアプリ 目標 地理的な位置データに条件を適用してデータをクエリする方法を理解すること。 1 query\<u> クラスについての簡単なレビュー back4appデータベースで実行されるすべてのクエリは、一般的なクラス query\<u> query\<u> を介して行われます。一般的なパラメータ u u ( parseobject parseobject プロトコルに準拠)とは、データベースから取得しようとしているオブジェクトのデータ型です。 データ型が myobject myobject , これらのオブジェクトをback4appデータベースから次のように取得します 1 import parseswift 2 3 struct myobject parseobject { 4 5 } 6 7 let query = myobject query() 8 9 // executes the query asynchronously 10 query find { result in 11 // handle the result (of type result<\[myobject], parseerror>) 12 } 次のことについて詳しく読むことができます query\<u> query\<u> クラス 公式ドキュメントでこちらをご覧ください https //github com/parse community/parse swift 2 back4appにデータを保存する クエリを実行する前に、back4appデータベースにサンプルデータを保存する必要があります。 クイックスタート https //www back4app com/docs/ios/parse swift sdk/install sdk ガイドに従うことで、サンプルiosアプリをback4appデータベースに設定してリンクできます。このガイドでは、都市に関する情報を保存します。都市の情報を整理するために、以下の構造体を使用します 1 import parseswift 2 3 struct city parseobject { 4 5 6 var name string? // city's name 7 var location parsegeopoint? // it will store the city's coordinate on earth 8 } 次に、サンプルデータを保存します。このステップは、 swift を使用するか、アプリの コンソール https //www youtube com/watch?v=nvwryrzbcma をback4appプラットフォームで直接使用して実装できます。 //after setting up your xcode project, calling the following method should save some sample data on your back4app database 1 import parseswift 2 3 func setupsampledata() { 4 do { 5 // montevideo uruguay 6 = try city( 7 name "montevideo", 8 location parsegeopoint(coordinate cllocationcoordinate2d(latitude 34 85553195363169, longitude 56 207280375137955)) 9 ) save() 10 11 // brasília brazil 12 = try city( 13 name "brasília", 14 location parsegeopoint(coordinate cllocationcoordinate2d(latitude 15 79485821477289, longitude 47 88391074690196)) 15 ) save() 16 17 // bogotá colombia 18 = try city( 19 name "bogotá", 20 location parsegeopoint(coordinate cllocationcoordinate2d(latitude 4 69139880891712, longitude 74 06936691331047)) 21 ) save() 22 23 // mexico city mexico 24 = try city( 25 name "mexico city", 26 location parsegeopoint(coordinate cllocationcoordinate2d(latitude 19 400977162618933, longitude 99 13311378164776)) 27 ) save() 28 29 // washington, d c united states 30 = try city( 31 name "washington, d c ", 32 location parsegeopoint(coordinate cllocationcoordinate2d(latitude 38 930727220189944, longitude 77 04626261880388)) 33 ) save() 34 35 // ottawa canada 36 = try city( 37 name "ottawa", 38 location parsegeopoint(latitude 45 41102167733425, longitude 75 695414598736) 39 ) save() 40 } catch let error as parseerror { 41 print("\[parseswift error]", error message) 42 } catch { 43 print("\[error]", error localizeddescription) 44 } 45 } back4app's console //a quick way to insert elements on your back4app database is via the console located in your app’s api section once you are there, you can start running javascript code to save the sample data 1 // add city objects and create table 2 // note how geopoints are created, passing latitude and longitude as arguments 3 // montevideo 4 city = new parse object('city'); 5 city set('name', 'montevideo uruguay'); 6 city set('location', new parse geopoint( 34 85553195363169, 56 207280375137955)); 7 await city save(); 8 9 // brasília 10 city = new parse object('city'); 11 city set('name', 'brasília brazil'); 12 city set('location', new parse geopoint( 15 79485821477289, 47 88391074690196)); 13 await city save(); 14 15 // bogotá 16 city = new parse object('city'); 17 city set('name', 'bogotá colombia'); 18 city set('location', new parse geopoint(4 69139880891712, 74 06936691331047)); 19 await city save(); 20 21 // mexico city 22 city = new parse object('city'); 23 city set('name', 'mexico city mexico'); 24 city set('location', new parse geopoint(19 400977162618933, 99 13311378164776)); 25 await city save(); 26 27 // washington, d c 28 city = new parse object('city'); 29 city set('name', 'washington, d c usa'); 30 city set('location', new parse geopoint(38 930727220189944, 77 04626261880388)); 31 await city save(); 32 33 // ottawa 34 city = new parse object('city'); 35 city set('name', 'ottawa canada'); 36 city set('location', new parse geopoint(45 41102167733425, 75 695414598736)); 37 await city save(); 3 データのクエリ 結果のソート サンプルデータが保存されたので、さまざまなクエリタイプを実行し始めることができます。 最初の例として、すべての都市を選択し、参照ジオポイントからの距離に応じてソートします。このクエリは、 query\<city> query\<city> オブジェクトに制約を渡すことで実装します。メソッド near(key\ geopoint ) near(key\ geopoint ) は、 parseswift sdk parseswift sdk を介して利用可能であり、そのような制約を構築することができます。引数として、参照 key key を含むフィールド名(通常は geopoint geopoint と呼ばれます)を渡します。 1 // the reference geopoint will be kingston city jamaica 2 guard let kingstongeopoint = try? parsegeopoint(latitude 18 018086950599134, longitude 76 79894232253473) else { return } 3 4 let query = city query(near(key "location", geopoint kingstongeopoint)) // the method near(key\ geopoint ) returns the constraint needed to sort the query 5 6 let sortedcities \[city]? = try? query find() // executes the query synchronosuly and returns an array containing the cities properly sorted 7 8 query find { result in // executes the query asynchronosuly and returns a result<\[city], parseerror> type object to handle the results 9 switch result { 10 case success(let cities) 11 // cities = \[bogotá, washington dc, mexico city, ottawa, brasília, montevideo] 12 case failure(let error) 13 // handle the error if something happened 14 } 15 } 特定の地域内の結果を選択する 特定の地域内の都市を選択したいとします。これは、メソッドによって作成された制約を使用して達成できます withinkilometers(key\ geopoint\ distance ) withinkilometers(key\ geopoint\ distance ) 引数として、都市の位置を含むフィールドの名前、地域の中心( parsegeopoint parsegeopoint データ型)およびこの地域の中心から都市が最大でどれだけ離れているか( km )を渡します。キングストン ジャマイカから最大で 3000km 離れているすべての都市を選択するには、次のようにします。 1 let distance double = 3000 // km 2 guard let kingstongeopoint = try? parsegeopoint(latitude 18 018086950599134, longitude 76 79894232253473) else { return } 3 4 let query = city query(withinkilometers(key "location", geopoint kingstongeopoint, distance distance)) 5 // the method withinkilometers(key\ geopoint\ distance ) returns the constraint we need for this case 6 7 let sortedcities \[city]? = try? query find() // executes the query synchronosuly and returns an array containing the cities we are looking for (bogotá, washington dc and mexico city in this case) 8 9 query find { result in // executes the query asynchronosuly and returns a result<\[city], parseerror> type object to handle the results 10 switch result { 11 case success(let cities) 12 // cities = \[bogotá, washington dc, mexico city] 13 case failure(let error) 14 // handle the error if something happened 15 } 16 } さらに、距離がキロメートルではなくマイルで与えられる場合、私たちは withinmiles(key\ geopoint\ distance\ sorted ) withinmiles(key\ geopoint\ distance\ sorted ) メソッドを使用できます。 あまり一般的ではないメソッド、 withinradians(key\ geopoint\ distance\ sorted ) withinradians(key\ geopoint\ distance\ sorted ) も、距離がラジアンで与えられる場合に利用可能です。その使用法は前のメソッドと非常に似ています。 指定されたポリゴン内の結果を選択する 前の例では、円形の領域で表された地域内の都市を選択しました。地域に非円形の形状が必要な場合、 parseswift sdk parseswift sdk は、その頂点からそのような領域を構築することを可能にします。 この例の目標は、五角形のポリゴン内の都市を選択することです。これらの頂点は、 parsegeopoint parsegeopoint 構造体を使用して表現されます。頂点を作成したら、 parsepolygon parsepolygon をインスタンス化します。このポリゴンは次に、 withinpolygon(key\ polygon ) withinpolygon(key\ polygon ) メソッド( parseswift sdk parseswift sdk によって提供される)に渡され、ポリゴン内の都市を選択するための制約を構築します。 1 // the polygon where the selected cities are 2 let polygon parsepolygon? = { 3 do { 4 // we first instantiate the polygon vertices 5 let geopoint1 = try parsegeopoint(latitude 15 822238344514378, longitude 72 42845934415942) 6 let geopoint2 = try parsegeopoint(latitude 0 7433770196268968, longitude 97 44765968406668) 7 let geopoint3 = try parsegeopoint(latitude 59 997149373299166, longitude 76 52969196322749) 8 let geopoint4 = try parsegeopoint(latitude 9 488786415007201, longitude 18 346101586021952) 9 let geopoint5 = try parsegeopoint(latitude 15 414859532811047, longitude 60 00625459569375) 10 11 // next we compose the polygon 12 return try parsepolygon(\[geopoint1, geopoint2, geopoint3, geopoint4, geopoint5]) 13 } catch let error as parseerror { 14 print("failed to instantiate vertices \\(error message)") 15 return nil 16 } catch { 17 print("failed to instantiate vertices \\(error localizeddescription)") 18 return nil 19 } 20 }() 21 22 guard let safepolygon = polygon else { return } 23 24 let query = city query(withinpolygon(key "location", polygon safepolygon)) 25 // withinpolygon(key\ polygon ) returns the required constraint to apply on the query 26 27 let cities = try? query find() // executes the query synchronously 28 29 query find { result in // executes the query asynchronously and returns a result of type result<\[], parseerror> 30 // handle the result 31 } 結論 現在、位置データに対して操作を行い、カスタムサービスを提供することは非常に重要です。back4appは、 parseswift sdk parseswift sdk と共に、これらの操作を実装するのを容易にします。