เพิ่ม ParseGeoPoints ในแอป Android ด้วย Back4App
31 นาที
การใช้ parse geopoints ในแอป android ของคุณ บทนำ parse ช่วยให้คุณสามารถเชื่อมโยงพิกัดละติจูดและลองจิจูดในโลกจริงกับวัตถุได้ การเพิ่ม parsegeopoint ไปยัง parseuser , คุณจะสามารถค้นหาผู้ใช้ที่ใกล้เคียงที่สุดกับอีกคนหนึ่งได้ง่าย ๆ แสดงตำแหน่งของผู้ใช้ในแอปของคุณ และยังเก็บข้อมูลตำแหน่งของผู้ใช้ได้อีกด้วย นอกจากนี้ยังมีความเป็นไปได้อื่น ๆ อีกมากมาย คุณยังสามารถเชื่อมโยง parsegeopoint กับ parseobject ตัวอย่างเช่น หากแอปของคุณเชื่อมโยงกับร้านค้าที่มีสาขาทางกายภาพ คุณจะสามารถสร้างกิจกรรมเพื่อแสดงตำแหน่งของร้านค้าเหล่านั้นหรือแสดงให้ผู้ใช้เห็นว่าร้านค้าไหนอยู่ใกล้เขาที่สุด ตัวอย่างอีกอย่างของการใช้งานการเชื่อมโยงนี้ หากแอปของคุณเป็นเกมที่คุณสร้าง parseobjects เพื่อแทนตัวละคร การเพิ่ม parsegeopoints ให้กับตัวละครเหล่านี้จะทำให้สามารถแสดงตัวละครตามเส้นทางของผู้เล่นได้ บทเรียนนี้อธิบายวิธีการใช้ฟีเจอร์บางอย่างของ parsegeopoint ผ่าน back4app หลังจากทำตามบทเรียนนี้ คุณจะสามารถทำสิ่งนี้ได้ ในทุกช่วงเวลา คุณสามารถเข้าถึงโปรเจกต์ android ที่สร้างขึ้นด้วยบทเรียนนี้ได้ที่ github repository https //github com/back4app/android geopoints tutorial ข้อกำหนดเบื้องต้น ในการทำบทเรียนนี้ให้เสร็จสิ้น เราต้องการ android studio https //developer android com/studio/index html แอปลงทะเบียนผู้ใช้ เข้าสู่ระบบที่สร้างขึ้นบน back4app หมายเหตุ โปรดทำตามบทเรียนการลงทะเบียนผู้ใช้ เข้าสู่ระบบเพื่อเรียนรู้วิธีการสร้างแอปลงทะเบียนผู้ใช้ เข้าสู่ระบบบน back4app อุปกรณ์จริงที่ใช้ android 4 0 (ice cream sandwich) หรือใหม่กว่า หมายเหตุ แอปที่สร้างขึ้นด้วยบทเรียนนี้อาจไม่ทำงานตามที่คาดหวังในอุปกรณ์เสมือนและอาจล้มเหลวได้เนื่องจากอาจไม่สามารถดึงตำแหน่งปัจจุบันของอุปกรณ์เสมือนได้ ดังนั้นเราขอแนะนำอย่างยิ่งให้คุณใช้ อุปกรณ์จริงในการรันแอปนี้ 1 ตั้งค่า google api key เพื่อแสดงตำแหน่งที่คุณเก็บไว้ใน parsegeopoint คุณจะต้องแสดงแผนที่ เพื่อทำเช่นนั้น น่าสนใจที่จะใช้ google maps activity ในการสร้าง google maps activity ใน android studio ให้ทำตามขั้นตอนดังต่อไปนี้ ไปที่ \<font color="#2166ae">ไฟล์\</font> > \<font color="#2166ae">ใหม่\</font> > \<font color="#2166ae">google\</font> > \<font color="#2166ae">google maps activity\</font> จากนั้นจะสร้างไฟล์ \<font color="#2166ae">java\</font> ไฟล์หนึ่ง ไฟล์ \<font color="#2166ae">layout\</font> และไฟล์ \<font color="#2166ae">values\</font> ที่สอดคล้องกับ google maps activity ที่คุณได้สร้างขึ้น ไปที่ไฟล์ที่สร้างขึ้น \<font color="#2166ae"> values\</font> (คุณสามารถทำได้โดยการเข้าถึง \<font color="#2166ae">app\</font> > \<font color="#2166ae">res\</font> > \<font color="#2166ae">values\</font> > \<font color="#2166ae">google maps api xml\</font> ), ตามที่แสดงในภาพด้านล่าง ไฟล์นี้จะให้คำแนะนำเกี่ยวกับวิธีการรับ google maps api key โดยพื้นฐานแล้ว คุณควรเปิดลิงก์ที่แสดงในภาพ 3\ หลังจากเปิดมันแล้ว คุณควรเข้าสู่ระบบบัญชี google ของคุณ และเลือก \<font color="#2166ae">สร้างโปรเจกต์\</font> ตัวเลือกและคลิกที่ \<font color="#2166ae">ดำเนินการต่อ\</font> ในขณะที่สร้างโปรเจกต์ google จะเปิดใช้งาน api ของคุณ 4\ หลังจากที่ api ของคุณเปิดใช้งานแล้ว คุณจะสามารถรับคีย์ api ได้ เพื่อทำเช่นนั้นให้คลิกที่ 5\ จากนั้น คีย์ของคุณจะถูกสร้างขึ้นและคุณสามารถคัดลอกและวางมันใน \<font color="#2166ae">ค่า\</font> ไฟล์ที่นำคุณไปยังหน้านี้ ในที่ที่เขียนว่า \<font color="#2166ae">your key here\</font> 6\ สิ่งสำคัญคือต้องมีการอนุญาตการใช้งานด้านล่างในไฟล์ androidmanifest xml ของคุณ หากคุณสร้าง google maps activity ตามคำแนะนำข้างต้นแล้ว หนึ่งในสิทธิ์เหล่านี้ควรอยู่ใน manifest ของคุณแล้ว อย่างไรก็ตาม คุณจะต้องมีทั้งสองสิทธิ์เพื่อให้แอปของคุณทำงานได้อย่างถูกต้อง ดังนั้นตรวจสอบว่าพวกเขาอยู่ในไฟล์ androidmanifest xml ของคุณหรือไม่ มิฉะนั้นให้แทรกพวกเขาในนั้น 1 \<uses permission android\ name="android permission access coarse location" /> 2 \<uses permission android\ name="android permission access fine location" /> 7\ ที่จุดเริ่มต้นของ \<font color="#2166ae">mapsactivity\</font> , นำเข้าดังต่อไปนี้ 1 // android dependencies 2 import android manifest; 3 import android app progressdialog; 4 import android content context; 5 import android content dialoginterface; 6 import android content intent; 7 import android content pm packagemanager; 8 import android location location; 9 import android location locationmanager; 10 import android support annotation nonnull; 11 import android support v4 app activitycompat; 12 import android support v4 app fragmentactivity; 13 import android os bundle; 14 import android util log; 15 import android view\ view; 16 import android widget button; 17 // google maps dependencies 18 import com google android gms maps cameraupdatefactory; 19 import com google android gms maps googlemap; 20 import com google android gms maps onmapreadycallback; 21 import com google android gms maps supportmapfragment; 22 import com google android gms maps model bitmapdescriptorfactory; 23 import com google android gms maps model latlng; 24 import com google android gms maps model markeroptions; 25 // parse dependencies 26 import com parse findcallback; 27 import com parse parseexception; 28 import com parse parsegeopoint; 29 import com parse parsequery; 30 import com parse parseuser; 31 // java dependencies 32 import java util list; 2 ตั้งค่า back4app dashboard เพื่อบันทึกตำแหน่งของผู้ใช้ ไปที่ เว็บไซต์ back4app https //www back4app com/ เข้าสู่ระบบ ค้นหาแอปของคุณและเปิดแดชบอร์ดของมัน ไปที่ \<font color="#2166ae">core\</font> > \<font color="#2166ae">browser\</font> > \<font color="#2166ae">user\</font> , ตามที่แสดงในภาพด้านล่าง 3\ ตอนนี้ สร้างคอลัมน์ใหม่เพื่อบันทึกตำแหน่งของผู้ใช้ เพื่อทำเช่นนี้ ให้คลิกที่ \<font color="#2166ae">แก้ไข\</font> ปุ่มที่มุมขวาบน ตามที่แสดงด้านล่าง 4\ จากนั้น คลิกที่ \<font color="#2166ae">เพิ่มคอลัมน์\</font> 5\ ในฟิลด์ คุณต้องการเก็บข้อมูลประเภทใด? ให้เลือกตัวเลือก geopoint 6\ ในฟิลด์ เราควรเรียกมันว่าอะไร? ให้พิมพ์ “ ตำแหน่ง ” 7\ ดังนั้น คลิกที่ เพิ่มคอลัมน์ ตามที่แสดงในภาพด้านล่าง 8\ ตอนนี้ แอปของคุณสามารถเก็บข้อมูลตำแหน่งของผู้ใช้ และคลาสผู้ใช้ของคุณในแดชบอร์ด back4pp ควรมีลักษณะดังนี้ 3 บันทึกตำแหน่งปัจจุบันของผู้ใช้ในแดชบอร์ด back4pp เปิด \<font color="#2166ae">mapsactivity \</font> และภายในคลาสสาธารณะ \<font color="#2166ae">mapsactivity \</font> กำหนด int ที่เรียกว่า \<font color="#2166ae">request location \</font> ด้วยค่า 1 และ \<font color="#2166ae">locationmanager\</font> , ตามโค้ดด้านล่าง 1 private static final int request location = 1; 2 locationmanager locationmanager; 2\ ใน \<font color="#2166ae">oncreate \</font> เมธอด สร้าง \<font color="#2166ae">locationmanager\</font> , ตามโค้ดด้านล่าง 1 locationmanager = (locationmanager)getsystemservice(context location service); 3\ เพื่อบันทึกตำแหน่งของผู้ใช้ใน back4pp dashboard ของคุณ เพียงแค่ทำการ implement วิธีการต่อไปนี้และเรียกใช้ใน \<font color="#2166ae">onmapready \</font> วิธีการ 1 private void savecurrentuserlocation() { 2 // requesting permission to get user's location 3 if(activitycompat checkselfpermission(usersactivity this, manifest permission access fine location) != packagemanager permission granted && activitycompat checkselfpermission(usersactivity this, manifest permission access coarse location) != packagemanager permission granted){ 4 activitycompat requestpermissions(usersactivity this, new string\[]{android manifest permission access fine location}, request location); 5 } 6 else { 7 // getting last know user's location 8 location location = locationmanager getlastknownlocation(locationmanager network provider); 9 10 // checking if the location is null 11 if(location != null){ 12 // if it isn't, save it to back4app dashboard 13 parsegeopoint currentuserlocation = new parsegeopoint(location getlatitude(), location getlongitude()); 14 15 parseuser currentuser = parseuser getcurrentuser(); 16 17 if (currentuser != null) { 18 currentuser put("location", currentuserlocation); 19 currentuser saveinbackground(); 20 } else { 21 // do something like coming back to the login activity 22 } 23 } 24 else { 25 // if it is null, do something like displaying error and coming back to the menu activity 26 } 27 } 28 } 4\ มันสำคัญมากที่จะต้อง implement วิธีการต่อไปนี้เพื่อให้ \<font color="#2166ae">savecurrentuserlocation \</font> ทำงาน 1 @override 2 public void onrequestpermissionsresult(int requestcode, @nonnull string\[] permissions, @nonnull int\[] grantresults){ 3 super onrequestpermissionsresult(requestcode, permissions, grantresults); 4 5 switch (requestcode){ 6 case request location 7 savecurrentuserlocation(); 8 break; 9 } 10 } 4 ดึงตำแหน่งของผู้ใช้ ในการดึงตำแหน่งของผู้ใช้ คุณจะต้องหาว่าใครคือผู้ใช้ปัจจุบัน บันทึกตำแหน่งของมันและจากนั้นคืนค่าตำแหน่งที่บันทึกไว้ใน back4app dashboard ของคุณ เพื่อทำเช่นนั้น ให้ implement วิธีการต่อไปนี้ใน \<font color="#2166ae">mapsactivity \</font> และเรียกใช้เมื่อจำเป็น 1 / saving the current user location, using the savecurrentuserlocation method of step 3, to avoid 2 null pointer exception and also to return the user's most current location / 3 savecurrentuserlocation(); 4 private parsegeopoint getcurrentuserlocation(){ 5 6 // finding currentuser 7 parseuser currentuser = parseuser getcurrentuser(); 8 9 if (currentuser == null) { 10 // if it's not possible to find the user, do something like returning to login activity 11 } 12 // otherwise, return the current user location 13 return currentuser getparsegeopoint("location"); 14 15 } 5 แสดงตำแหน่งของผู้ใช้ปัจจุบันในแผนที่ ในการแสดงตำแหน่งของผู้ใช้ในแผนที่ คุณจะต้องดึงตำแหน่งของผู้ใช้และจากนั้นสร้างเครื่องหมายในแผนที่โดยใช้ข้อมูลที่ดึงมา เพื่อทำเช่นนั้น ให้ดำเนินการตามวิธีการต่อไปนี้ใน \<font color="#2166ae">mapsactivity \</font> และเรียกใช้ใน \<font color="#2166ae">onmapready \</font> วิธีการ 1 private void showcurrentuserinmap(final googlemap googlemap){ 2 3 // calling retrieve user's location method of step 4 4 parsegeopoint currentuserlocation = getcurrentuserlocation(); 5 6 // creating a marker in the map showing the current user location 7 latlng currentuser = new latlng(currentuserlocation getlatitude(), currentuserlocation getlongitude()); 8 googlemap addmarker(new markeroptions() position(currentuser) title(parseuser getcurrentuser() getusername()) icon(bitmapdescriptorfactory defaultmarker(bitmapdescriptorfactory hue red))); 9 10 // zoom the map to the currentuserlocation 11 googlemap animatecamera(cameraupdatefactory newlatlngzoom(currentuser, 5)); 12 } 6 ค้นหาผู้ใช้ที่ใกล้เคียงที่สุดกับผู้ใช้ปัจจุบัน ตอนนี้คุณมีผู้ใช้จำนวนมากที่มีพิกัดเชิงพื้นที่ที่เกี่ยวข้อง จะดีมากหากค้นหาว่าผู้ใช้คนใดใกล้เคียงกับอีกคนหนึ่งมากที่สุด สิ่งนี้สามารถทำได้โดยใช้ parsequery ก่อนอื่นคุณจะต้องสร้างการค้นหา parseuser และจำกัดด้วย \<font color="#2166ae">wherenear\</font> , แจ้งในคอลัมน์ใดของคลาสผู้ใช้ใน back4app ที่คุณกำลังทำการค้นหา (ในกรณีนี้คือคอลัมน์ “location”) และยังแจ้งพิกัด parsegeopoint สำหรับการค้นหาจากซึ่งจะพบผู้ใช้ที่ใกล้เคียงที่สุด (ในกรณีนี้คือ parsegeopoint ที่เกี่ยวข้องกับตำแหน่งของผู้ใช้ปัจจุบัน) โค้ดต่อไปนี้ทำเช่นนั้น 1 parsequery\<parseuser> query = parseuser getquery(); 2 query wherenear("location", getcurrentuserlocation()); 2\ คุณสามารถจำกัดจำนวนผลลัพธ์ของการค้นหานี้โดยการเพิ่มข้อจำกัด \<font color="#2166ae">setlimit\</font> โดยค่าเริ่มต้น ผลลัพธ์จะถูกจำกัดไว้ที่ 100 เนื่องจากข้อจำกัด \<font color="#2166ae">wherenear \</font> จะทำให้การค้นหาดึงข้อมูลผู้ใช้ในรูปแบบอาร์เรย์ที่เรียงตามระยะทาง (จากใกล้ที่สุดไปไกลที่สุด) จาก currentuserlocation โดยตั้งค่าจำนวนผู้ใช้ที่ใกล้ที่สุดที่จะค้นหาเป็น 2 รายการ ผลลัพธ์ของการค้นหาจะมีผู้ใช้เพียงสองคน ผู้ใช้ปัจจุบันและผู้ใช้ที่ใกล้ที่สุดจากเขา โค้ดต่อไปนี้ตั้งค่าจำนวนผลลัพธ์เป็น 2 1 query setlimit(2); 3\ ตอนนี้ที่คุณได้จำกัดการค้นหาของคุณแล้ว มาลองดึงผลลัพธ์ของมันกันเถอะ เพื่อทำเช่นนั้น คุณจะเรียกใช้ \<font color="#2166ae">findinbackground \</font> เมธอดและส่งเป็นอาร์กิวเมนต์เป็นรายการของผู้ใช้ ซึ่งจะเก็บผลลัพธ์ของการค้นหาและยังมี parseexception เพื่อจัดการข้อผิดพลาด เพื่อหลีกเลี่ยงข้อผิดพลาด อย่าลืมล้างผลลัพธ์ที่เก็บไว้หลังจากการค้นหาเสร็จสิ้น โค้ดต่อไปนี้ทำเช่นนั้น 1 query findinbackground(new findcallback\<parseuser>() { 2 @override public void done(list\<parseuser> nearusers, parseexception e) { 3 if (e == null) { 4 // do something with the list of results of your query 5 } else { 6 // handle the error 7 } 8 } 9 }); 10 parsequery clearallcachedresults(); 11 } 4\ หากไม่มีข้อผิดพลาดเกิดขึ้น คุณจะมีใน \<font color="#2166ae">nearusers \</font> รายการผู้ใช้ที่ใกล้ที่สุดสองคนกับผู้ใช้ปัจจุบัน (ผู้ใช้ปัจจุบันจริงและผู้ใช้ที่ใกล้ที่สุดจากเขา) ตอนนี้คุณจะต้องหาว่าใครไม่ใช่ผู้ใช้ปัจจุบัน เพื่อทำเช่นนั้น ภายใน \<font color="#2166ae"> if (e == null)\</font> บล็อกให้ใช้โค้ดต่อไปนี้ 1 // avoiding null pointer 2 parseuser closestuser = parseuser getcurrentuser(); 3 // set the closestuser to the one that isn't the current user 4 for(int i = 0; i < nearusers size(); i++) { 5 if(!nearusers get(i) getobjectid() equals(parseuser getcurrentuser() getobjectid())) { 6 closestuser = nearusers get(i); 7 } 8 } 5\ ตอนนี้ที่คุณรู้ว่าใครคือผู้ใช้ที่ใกล้ที่สุดกับผู้ใช้ปัจจุบัน คุณสามารถวัดระยะทางระหว่างพวกเขาได้โดยใช้ \<font color="#2166ae">distanceinkilometersto \</font> วิธีการ ในการทำเช่นนั้น ให้ใช้โค้ดต่อไปนี้ 1 // finding and displaying the distance between the current user and the closest user to him 2 double distance = getcurrentuserlocation() distanceinkilometersto(closestuser getparsegeopoint("location")); 3 alertdisplayer("we found the closest user from you!", "it's " + closestuser getusername() + " \nyou are " + math round (distance 100 0) / 100 0 + " km from this user "); วิธีการ \<font color="#2166ae">alertdisplayer\</font> ที่ใช้ข้างต้นคือ 1 private void alertdisplayer(string title,string message){ 2 android app alertdialog builder builder = new android app alertdialog builder(usersactivity this) 3 settitle(title) 4 setmessage(message) 5 setpositivebutton("ok", new dialoginterface onclicklistener() { 6 @override 7 public void onclick(dialoginterface dialog, int which) { 8 dialog cancel(); 9 } 10 }); 11 android app alertdialog ok = builder create(); 12 ok show(); 13 } 6\ คุณยังสามารถแสดงผู้ใช้ทั้งสองในแผนที่ โดยใช้โค้ดต่อไปนี้ 1 // showing current user in map, using the method implemented in step 5 2 showcurrentuserinmap(mmap); 3 // creating a marker in the map showing the closest user to the current user location, using method implemented in step 4 4 latlng closestuserlocation = new latlng(closestuser getparsegeopoint("location") getlatitude(), closestuser getparsegeopoint("location") getlongitude()); 5 googlemap addmarker(new markeroptions() position(closestuserlocation) title(closestuser getusername()) icon(bitmapdescriptorfactory defaultmarker(bitmapdescriptorfactory hue green))); 6 // zoom the map to the currentuserlocation 7 googlemap animatecamera(cameraupdatefactory newlatlngzoom(closestuserlocation, 3)); วิธีการที่สมบูรณ์ซึ่งดำเนินการตามขั้นตอนทั้ง 6 ขั้นตอนข้างต้นคือดังนี้ 1 private void showclosestuser(final googlemap googlemap){ 2 parsequery\<parseuser> query = parseuser getquery(); 3 query wherenear("location", getcurrentuserlocation()); 4 // setting the limit of near users to find to 2, you'll have in the nearusers list only two users the current user and the closest user from the current 5 query setlimit(2); 6 query findinbackground(new findcallback\<parseuser>() { 7 @override public void done(list\<parseuser> nearusers, parseexception e) { 8 if (e == null) { 9 // avoiding null pointer 10 parseuser closestuser = parseuser getcurrentuser(); 11 // set the closestuser to the one that isn't the current user 12 for(int i = 0; i < nearusers size(); i++) { 13 if(!nearusers get(i) getobjectid() equals(parseuser getcurrentuser() getobjectid())) { 14 closestuser = nearusers get(i); 15 } 16 } 17 // finding and displaying the distance between the current user and the closest user to him, using method implemented in step 4 18 double distance = getcurrentuserlocation() distanceinkilometersto(closestuser getparsegeopoint("location")); 19 alertdisplayer("we found the closest user from you!", "it's " + closestuser getusername() + " \n you are " + math round (distance 100 0) / 100 0 + " km from this user "); 20 // showing current user in map, using the method implemented in step 5 21 showcurrentuserinmap(mmap); 22 // creating a marker in the map showing the closest user to the current user location 23 latlng closestuserlocation = new latlng(closestuser getparsegeopoint("location") getlatitude(), closestuser getparsegeopoint("location") getlongitude()); 24 googlemap addmarker(new markeroptions() position(closestuserlocation) title(closestuser getusername()) icon(bitmapdescriptorfactory defaultmarker(bitmapdescriptorfactory hue green))); 25 // zoom the map to the currentuserlocation 26 googlemap animatecamera(cameraupdatefactory newlatlngzoom(closestuserlocation, 3)); 27 } else { 28 log d("store", "error " + e getmessage()); 29 } 30 } 31 }); 32 parsequery clearallcachedresults(); 33 } 7 ตั้งค่า back4app เพื่อเชื่อมโยง parsegeopoint กับ parseobjects สมมติว่าแอปตัวอย่างที่คุณกำลังสร้างเกี่ยวข้องกับกลุ่มร้านค้าที่มีสาขาทางกายภาพ จะน่าสนใจที่จะโชว์สาขาทางกายภาพทั้งหมดของร้านนี้ในแผนที่ เพื่อทำเช่นนั้นให้สร้างคลาส stores บน back4pp dashboard บันทกร้านค้าที่มีอยู่เป็น parseobjects ตำแหน่งของพวกเขา และหลังจากนั้นแอปจะสามารถค้นหาสาขาทางกายภาพและแสดงตำแหน่งของพวกเขาบนแผนที่ได้ ขั้นตอนต่อไปนี้จะช่วยคุณในเรื่องนั้น 1 ไปที่ เว็บไซต์ back4app https //www back4app com/ , เข้าสู่ระบบ ค้นหาแอปของคุณและเปิด dashboard ของมัน 2\ ไปที่ \<font color="#2166ae">core \</font> > \<font color="#2166ae">browser \</font> > \<font color="#2166ae">สร้างคลาส\</font> , ตามที่แสดงในภาพด้านล่าง 3\ ในฟิลด์ what typ \<font color="#2166ae">e of class do you need?\</font> , เลือก \<font color="#2166ae">custom \</font> ตัวเลือก 4\ ในฟิลด์ \<font color="#2166ae">เราควรเรียกมันว่าอะไร?\</font> , พิมพ์ “stores” และจากนั้นคลิกที่ \<font color="#2166ae">สร้างคลาส\</font> ปุ่ม 5\ จากนั้นจะมีคลาสใหม่ชื่อ “stores” แสดงขึ้นและคุณควรแทรก 2 คอลัมน์ในนั้น คอลัมน์ที่มีประเภทข้อมูล \<font color="#2166ae">string \</font> ชื่อว่า \<font color="#2166ae">ชื่อ\</font> , รวมถึงอีกคอลัมน์หนึ่งที่มีประเภทข้อมูล \<font color="#2166ae">geopoint \</font> ชื่อว่า \<font color="#2166ae">ที่ตั้ง\</font> หากคุณไม่จำได้ว่าจะแทรกคอลัมน์อย่างไร ให้กลับไปที่ขั้นตอนที่ 2 ที่นั่นจะมีการอธิบายวิธีการสร้างคอลัมน์ที่ back4app dashboard คลาสของคุณควรจะเป็นแบบนี้ 6\ ตอนนี้ให้กรอกข้อมูลในคลาสนี้ เพื่อทำเช่นนั้นให้คลิกที่ \<font color="#2166ae">เพิ่มแถว\</font> ปุ่มในเมนูที่มุมขวาบนหรือกลางหน้า ตามที่แสดงในภาพด้านล่าง 7\ จากนั้นให้กรอกแถวด้วยข้อมูลที่จำเป็น ชื่อร้านและพิกัดละติจูดและลองจิจูด หลังจากใส่ข้อมูลบางอย่าง คลาส stores ของคุณควรมีลักษณะตามภาพด้านล่าง หากคุณต้องการใส่ข้อมูลเพิ่มเติม คุณสามารถคลิกที่ \<font color="#2166ae">เพิ่มแถว\</font> ปุ่มที่ด้านบน หรือใน \<font color="#2166ae">+\</font> ปุ่มด้านล่างแถวข้อมูลสุดท้าย ตอนนี้คุณพร้อมที่จะใช้ข้อมูลตำแหน่งของร้านค้าในแอปของคุณ 8 แสดงตำแหน่งร้านค้าทั้งหมดบนแผนที่ 1\ คุณสามารถแสดงร้านค้าทั้งหมดที่คุณเพิ่มในชั้นเรียนของคุณได้ที่ \<font color="#2166ae">mapsactivity \</font> โดยการใช้ parsequery ก่อนอื่นคุณจะต้องสร้างการค้นหา parseobject โดยระบุชื่อของคลาส back4pp dashboard ที่คุณต้องการค้นหา (ในกรณีนี้คือคลาส “stores”) และจำกัดด้วย \<font color="#2166ae">whereexists\</font> , โดยระบุคอลัมน์ของคลาส store บน back4app ที่คุณกำลังทำการค้นหา (ในกรณีนี้คือคอลัมน์ “location”) โค้ดต่อไปนี้ทำเช่นนั้น 1 parsequery\<parseobject> query = parsequery getquery("stores"); 2 query whereexists("location"); 2\ ตอนนี้ที่คุณได้จำกัดการค้นหาของคุณแล้ว มาลองดึงผลลัพธ์ของมันกัน ในการทำเช่นนั้น คุณจะเรียกใช้ \<font color="#2166ae">findinbackground\</font> เมธอดและส่งเป็นอาร์กิวเมนต์เป็นรายการของร้านค้า ซึ่งจะเก็บผลลัพธ์ของการค้นหาและยังมี parseexception เพื่อจัดการข้อผิดพลาด การจำกัด \<font color="#2166ae">whereexists \</font> จะทำให้เฉพาะ parseobjects ของคลาสที่มี parsegeopoint ที่เกี่ยวข้องกับพวกเขาซึ่งบ่งบอกถึงตำแหน่งของพวกเขาเท่านั้นที่จะถูกเก็บในรายการร้านค้า เพื่อหลีกเลี่ยงข้อผิดพลาด อย่าลืมล้างผลลัพธ์ที่เก็บไว้หลังจากการค้นหาเสร็จสิ้น โค้ดต่อไปนี้ทำเช่นนั้น 1 query findinbackground(new findcallback\<parseobject>() { 2 @override public void done(list\<parseobject> stores, parseexception e) { 3 if (e == null) { 4 // do something with the list of results of your query 5 } else { 6 // handle the error 7 } 8 } 9 }); 10 parsequery clearallcachedresults(); 11 } 3\ หากไม่มีข้อผิดพลาดเกิดขึ้น คุณจะมีใน \<font color="#2166ae">stores \</font> รายการร้านค้าทั้งหมดที่มีตำแหน่งที่เกี่ยวข้อง ตอนนี้คุณจะต้องทำการวนลูปเพื่อแสดงพวกเขาบนแผนที่ เพื่อทำเช่นนั้น ภายใน \<font color="#2166ae">if (e == null)\</font> บล็อกให้ใช้โค้ดต่อไปนี้ 1 for(int i = 0; i < stores size(); i++) { 2 latlng storelocation = new latlng(stores get(i) getparsegeopoint("location") getlatitude(), stores get(i) getparsegeopoint("location") getlongitude()); 3 googlemap addmarker(new markeroptions() position(storelocation) title(stores get(i) getstring("name")) icon(bitmapdescriptorfactory defaultmarker(bitmapdescriptorfactory hue azure))); 4 } วิธีการที่สมบูรณ์ซึ่งดำเนินการทั้ง 3 ขั้นตอนข้างต้นอยู่ด้านล่าง เรียกใช้มันใน \<font color="#2166ae">onmapready \</font> วิธีการเพื่อให้มันทำงาน 1 private void showstoresinmap(final googlemap googlemap){ 2 3 parsequery\<parseobject> query = parsequery getquery("stores"); 4 query whereexists("location"); 5 query findinbackground(new findcallback\<parseobject>() { 6 @override public void done(list\<parseobject> stores, parseexception e) { 7 if (e == null) { 8 for(int i = 0; i < stores size(); i++) { 9 latlng storelocation = new latlng(stores get(i) getparsegeopoint("location") getlatitude(), stores get(i) getparsegeopoint("location") getlongitude()); 10 googlemap addmarker(new markeroptions() position(storelocation) title(stores get(i) getstring("name")) icon(bitmapdescriptorfactory defaultmarker(bitmapdescriptorfactory hue azure))); 11 } 12 } else { 13 // handle the error 14 log d("store", "error " + e getmessage()); 15 } 16 } 17 }); 18 parsequery clearallcachedresults(); 19 } 9 แสดงผู้ใช้ที่ใกล้ที่สุดกับร้านค้า 1\ ตอนนี้คุณมีร้านค้ามากมายที่มีพิกัดเชิงพื้นที่ที่เกี่ยวข้อง มันจะดีที่จะหาว่าร้านค้าไหนใกล้ผู้ใช้มากที่สุด สิ่งนี้สามารถทำได้โดยการใช้ parsequery ก่อนอื่นคุณจะต้องสร้างการค้นหา parseobject โดยการแจ้งชื่อของคลาส back4pp dashboard ที่คุณต้องการค้นหา (ในกรณีนี้คือคลาส “stores”) และจำกัดด้วย \<font color="#2166ae">wherenear\</font> , โดยการแจ้งคอลัมน์ของคลาส store บน back4app ที่คุณกำลังทำการค้นหา (ในกรณีนี้คือคอลัมน์ “location”) โค้ดต่อไปนี้ทำเช่นนั้น 1 parsequery\<parseobject> query = parsequery getquery("stores"); 2 query wherenear("location", getcurrentuserlocation()); 2\ คุณสามารถจำกัดจำนวนผลลัพธ์ของการค้นหานี้โดยการเพิ่มข้อจำกัด \<font color="#2166ae">setlimit\</font> โดยค่าเริ่มต้น ผลลัพธ์จะถูกจำกัดที่ 100 เนื่องจากข้อจำกัด \<font color="#2166ae">wherenear \</font> จะทำให้การค้นหาดึงอาร์เรย์ของผู้ใช้ที่เรียงตามระยะทาง (จากใกล้ที่สุดไปไกลที่สุด) จาก currentuserlocation โดยการตั้งค่าขีดจำกัดของผู้ใช้ที่ใกล้ที่สุดที่จะค้นหาเป็นหมายเลข 1 รายการผลลัพธ์ของการค้นหาจะมีเพียงร้านค้าเดียว ร้านค้าที่ใกล้ที่สุดกับผู้ใช้ปัจจุบัน โค้ดต่อไปนี้ตั้งค่าขีดจำกัดของผลลัพธ์เป็น 1 1 query setlimit(1); 3\ ตอนนี้ที่คุณได้จำกัดการค้นหาของคุณแล้ว มาดูผลลัพธ์กันเถอะ เพื่อทำเช่นนั้น คุณจะเรียกใช้ \<font color="#2166ae">findinbackground \</font> วิธีการและส่งอาร์กิวเมนต์เป็นรายการของวัตถุร้านค้า ซึ่งจะเก็บผลลัพธ์ของการค้นหาและยังมี parseexception เพื่อจัดการข้อผิดพลาด เพื่อหลีกเลี่ยงข้อผิดพลาด อย่าลืมล้างผลลัพธ์ที่เก็บไว้หลังจากที่การค้นหาพบผลลัพธ์ใด ๆ โค้ดต่อไปนี้ทำเช่นนั้น 1 query findinbackground(new findcallback\<parseobject>() { 2 @override public void done(list\<parseobject> nearstores, parseexception e) { 3 if (e == null) { 4 // do something with the list of results of your query 5 } else { 6 // handle the error 7 } 8 } 9 }); 10 parsequery clearallcachedresults(); 11 } 4\ หากไม่มีข้อผิดพลาดเกิดขึ้น คุณจะมีใน \<font color="#2166ae">nearstores \</font> รายการร้านค้าที่ใกล้ที่สุดกับผู้ใช้ปัจจุบัน ตอนนี้คุณจะต้องแสดงมันใน \<font color="#2166ae">mapsactivity\</font> เพื่อทำเช่นนั้น ภายใน \<font color="#2166ae">if (e == null)\</font> บล็อกให้ใช้โค้ดต่อไปนี้ 1 parseobject closeststore = nearstores get(0); 2 // showing current user location, using the method implemented in step 5 3 showcurrentuserinmap(mmap); 4 // finding and displaying the distance between the current user and the closest store to him, using method implemented in step 4 5 double distance = getcurrentuserlocation() distanceinkilometersto(closeststore getparsegeopoint("location")); 6 alertdisplayer("we found the closest store from you!", "it's " + closeststore getstring("name") + " \n you are " + math round (distance 100 0) / 100 0 + " km from this store "); 7 // creating a marker in the map showing the closest store to the current user 8 latlng closeststorelocation = new latlng(closeststore getparsegeopoint("location") getlatitude(), closeststore getparsegeopoint("location") getlongitude()); 9 googlemap addmarker(new markeroptions() position(closeststorelocation) title(closeststore getstring("name")) icon(bitmapdescriptorfactory defaultmarker(bitmapdescriptorfactory hue green))); 10 // zoom the map to the closeststorelocation 11 googlemap animatecamera(cameraupdatefactory newlatlngzoom(closeststorelocation, 3)); วิธีการ \<font color="#2166ae">alertdisplayer\</font> ที่ใช้ข้างต้น เป็นวิธีเดียวกับขั้นตอนที่ 6 (การค้นหาผู้ใช้ที่ใกล้เคียงที่สุดกับผู้ใช้ปัจจุบัน) วิธีการทั้งหมดที่ดำเนินการทั้ง 4 ขั้นตอนข้างต้นคือ 1 private void showcloseststore(final googlemap googlemap){ 2 parsequery\<parseobject> query = parsequery getquery("stores"); 3 query wherenear("location", getcurrentuserlocation()); 4 // setting the limit of near stores to 1, you'll have in the nearstores list only one object the closest store from the current user 5 query setlimit(1); 6 query findinbackground(new findcallback\<parseobject>() { 7 @override public void done(list\<parseobject> nearstores, parseexception e) { 8 if (e == null) { 9 parseobject closeststore = nearstores get(0); 10 // showing current user location, using the method implemented in step 5 11 showcurrentuserinmap(mmap); 12 // finding and displaying the distance between the current user and the closest store to him, using method implemented in step 4 13 double distance = getcurrentuserlocation() distanceinkilometersto(closeststore getparsegeopoint("location")); 14 alertdisplayer("we found the closest store from you!", "it's " + closeststore getstring("name") + " \nyou are " + math round (distance 100 0) / 100 0 + " km from this store "); 15 // creating a marker in the map showing the closest store to the current user 16 latlng closeststorelocation = new latlng(closeststore getparsegeopoint("location") getlatitude(), closeststore getparsegeopoint("location") getlongitude()); 17 googlemap addmarker(new markeroptions() position(closeststorelocation) title(closeststore getstring("name")) icon(bitmapdescriptorfactory defaultmarker(bitmapdescriptorfactory hue green))); 18 // zoom the map to the closeststorelocation 19 googlemap animatecamera(cameraupdatefactory newlatlngzoom(closeststorelocation, 3)); 20 } else { 21 log d("store", "error " + e getmessage()); 22 } 23 } 24 }); 25 26 parsequery clearallcachedresults(); 27 28 } 10 ทดสอบแอปของคุณ 1\ เข้าสู่ระบบที่ เว็บไซต์ back4app https //www back4app com/ 2\ ค้นหาแอปของคุณและคลิกที่ \<font color="#2166ae">แดชบอร์ด \</font> > \<font color="#2166ae">หลัก \</font> > \<font color="#2166ae">เบราว์เซอร์ \</font> > \<font color="#2166ae">ผู้ใช้ \</font> และสร้างผู้ใช้บางคนที่มีตำแหน่งที่เกี่ยวข้องกับพวกเขาเพื่อให้วิธีการ \<font color="#2166ae">showclosestuser \</font> ทำงาน 3\ รันแอปของคุณในอุปกรณ์จริงและลงทะเบียนบัญชี ลองใช้ทุกฟีเจอร์! 4\ กลับไปที่แดชบอร์ด back4pp และตรวจสอบว่าตำแหน่งของคุณถูกเก็บไว้ในแถวผู้ใช้ของคุณหรือไม่ เสร็จสิ้น! ในขั้นตอนนี้ คุณสามารถใช้ฟีเจอร์บางอย่างของ parsegeopoint ผ่าน back4app!