iOS
...
Data Objects
เพิ่มประสิทธิภาพ Geoqueries ด้วย ParseSwift SDK สำหรับนักพัฒนา
8 นาที
geoqueries บทนำ เราใช้คำว่า geoqueries เพื่ออ้างถึงประเภทของคำถามที่เงื่อนไขของพวกเขาเกี่ยวข้องกับ parsegeopoint parsegeopoint ประเภทฟิลด์ แนะนำให้ใช้ parsegeopoint parsegeopoint โครงสร้างเพื่อเก็บข้อมูลตำแหน่งทางภูมิศาสตร์ในฐานข้อมูล back4app 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 ประเภทข้อมูล) และระยะทางสูงสุด (เป็น กม ) ที่เมืองสามารถอยู่ห่างจากศูนย์กลางของภูมิภาคนี้ได้ เพื่อเลือกเมืองทั้งหมดที่อยู่ห่างไม่เกิน 3000 กม จากคิงส์ตัน จาเมกา เราสามารถทำได้ในลักษณะต่อไปนี้ 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 ทำให้การดำเนินการเหล่านี้ง่ายขึ้น