Android
Data objects
안드로이드 앱에서 Parse GeoPoints로 위치기반 기능 구현하기
32 분
안드로이드 앱에서 parse geopoints 사용하기 소개 parse는 객체와 실제 세계의 위도 및 경도 좌표를 연결할 수 있게 해줍니다 parsegeopoint 를 parseuser ,에 추가하면 다른 사용자와 가장 가까운 사용자를 쉽게 찾고, 앱 사용자들의 위치를 보여주며, 사용자의 위치 정보를 저장하는 등 다양한 가능성을 활용할 수 있습니다 또한 parsegeopoint 를 모든 parseobject ,에 연결할 수 있습니다 예를 들어, 귀하의 앱이 물리적 제휴점이 있는 상점과 관련이 있다면, 해당 상점의 위치를 보여주거나 사용자에게 가장 가까운 상점을 보여주는 활동을 만들 수 있습니다 이 연관 사용의 또 다른 예 귀하의 앱이 캐릭터를 나타내기 위해 parseobjects를 생성한 게임이라면, 이러한 캐릭터에 parsegeopoints를 추가하면 플레이어의 경로를 따라 표시할 수 있습니다 이 튜토리얼은 back4app을 통해 parsegeopoint의 일부 기능을 사용하는 방법을 설명합니다 이 튜토리얼을 따라하면 다음과 같은 작업을 수행할 수 있습니다 언제든지 이 튜토리얼로 구축된 전체 android 프로젝트에 접근할 수 있습니다 github 저장소 전제 조건 이 튜토리얼을 완료하려면 다음이 필요합니다 android studio back4app에서 생성된 사용자 등록 로그인 앱 참고 사용자 등록 로그인 튜토리얼을 따라 back4app에서 사용자 등록 로그인 앱을 만드는 방법을 배우세요 android 4 0 (아이스크림 샌드위치) 이상이 실행되는 실제 장치 참고 이 튜토리얼로 구축된 앱이 가상 장치에서 예상대로 실행되지 않거나 현재 위치를 검색하지 못해 충돌할 가능성이 매우 높습니다 따라서 실제 장치를 사용하여 실행하는 것을 강력히 권장합니다 1 google api 키 설정 parsegeopoint에 저장된 위치를 표시하려면 지도를 표시해야 합니다 이를 위해 google maps activity를 사용하는 것이 흥미롭습니다 android studio에서 google maps activity를 생성하려면 다음을 수행하십시오 로 가십시오 파일 파일 > 새로 만들기 새로 만들기 > 구글 구글 > google maps activity google maps activity 그러면 자동으로 java java 파일, layout layout 파일 및 values values 파일이 생성됩니다 생성된 values values 파일로 가십시오 (다음 경로를 통해 접근할 수 있습니다 app app > res res > values values > google maps api xml google maps api xml ) 아래 이미지와 같이 이 파일은 google maps api 키를 얻는 방법에 대한 지침을 제공합니다 기본적으로 아래 이미지에 표시된 링크를 열어야 합니다 3\ 열고 나면, google 계정에 로그인하고 프로젝트 만들기 프로젝트 만들기 옵션을 선택한 후 계속 계속 프로젝트를 생성하는 동안 google이 api를 활성화합니다 4\ api가 활성화되면 api 키를 받을 수 있습니다 그렇게 하려면 클릭하세요 5\ 그러면 키가 생성되고 복사하여 값 값 파일에 붙여넣을 수 있습니다 이 페이지로 안내하는 곳에 여기에 키 입력 여기에 키 입력 라고 적혀 있는 곳에 6\ androidmanifest xml 파일에 아래의 사용 권한이 있는 것이 중요합니다 위의 지침에 따라 google maps activity를 생성했다면, 이 권한 중 하나는 이미 매니페스트에 있어야 합니다 어쨌든 앱이 제대로 작동하려면 두 가지 모두 필요하므로 androidmanifest xml 파일에 있는지 확인하고, 그렇지 않으면 추가하세요 1 \<uses permission android\ name="android permission access coarse location" /> 2 \<uses permission android\ name="android permission access fine location" /> 7\ 당신의 mapsactivity mapsactivity , 다음을 가져옵니다 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 대시보드를 설정하여 사용자의 위치 저장하기 이동하여 back4app 웹사이트 에 로그인하고, 앱을 찾아 대시보드를 엽니다 이동하여 core core > browser browser > user user , 아래 이미지와 같이 3\ 이제 사용자의 위치를 저장할 새 열을 만듭니다 그렇게 하려면, 아래와 같이 오른쪽 상단의 편집 편집 버튼을 클릭합니다 4\ 그런 다음, 열 추가 열 추가 를 클릭합니다 5\ 데이터 저장 유형을 묻는 필드에서 geopoint 옵션을 선택합니다 6\ 무엇이라고 부를까요?라는 필드에 “ 위치 ”를 입력합니다 7\ 따라서 아래 이미지에 표시된 대로 열 추가를 클릭합니다 8\ 이제, 귀하의 앱은 사용자의 위치 데이터를 저장할 수 있으며 back4pp 대시보드의 사용자 클래스는 다음과 같아야 합니다 3 back4pp 대시보드에 사용자의 현재 위치 저장하기 귀하의 mapsactivity mapsactivity 를 열고 공용 클래스 mapsactivity mapsactivity 내에서 request location request location 이라는 int를 정의하고 값은 1로 설정하며 locationmanager locationmanager , 다음 코드와 같이 1 private static final int request location = 1; 2 locationmanager locationmanager; 2\ oncreate oncreate 메서드에서 locationmanager locationmanager , 다음 코드와 같이 생성합니다 1 locationmanager = (locationmanager)getsystemservice(context location service); 3\ 사용자의 위치를 back4pp 대시보드에 저장하려면, 다음 메서드를 구현하고 onmapready onmapready 메서드에서 호출하세요 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\ 다음 메서드를 구현하는 것이 정말 중요합니다 그래야 savecurrentuserlocation savecurrentuserlocation 메서드가 작동합니다 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 대시보드에 저장된 위치를 반환해야 합니다 이를 위해, 다음 메서드를 mapsactivity mapsactivity 에 구현하고 필요할 때 호출하세요 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 현재 사용자의 위치를 지도에 표시하기 사용자의 위치를 지도에 표시하려면, 사용자의 위치를 검색한 다음 검색된 정보를 사용하여 지도에 마커를 생성해야 합니다 이를 위해 다음 메서드를 mapsactivity mapsactivity 에 구현하고 onmapready onmapready 메서드에서 호출합니다 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 쿼리를 생성하고 wherenear wherenear , back4app의 user 클래스에서 쿼리를 수행하는 열(이 경우 “location” 열)을 지정하고, 가장 가까운 사용자를 찾기 위한 참조 parsegeopoint(이 경우 현재 사용자 위치와 연결된 parsegeopoint)를 알려야 합니다 다음 코드는 이를 수행합니다 1 parsequery\<parseuser> query = parseuser getquery(); 2 query wherenear("location", getcurrentuserlocation()); 2\ 이 쿼리의 결과 수를 제한하려면 setlimit setlimit 기본적으로 결과는 100으로 제한됩니다 wherenear wherenear 제한은 쿼리가 현재 사용자 위치에서 거리(가장 가까운 것부터 먼 것까지) 순서로 정렬된 사용자 배열을 검색하도록 합니다 가까운 사용자의 수를 2로 설정하면 쿼리의 결과 목록에는 현재 사용자와 그에게 가장 가까운 사용자 두 명만 포함됩니다 다음 코드는 결과의 수를 2로 설정합니다 1 query setlimit(2); 3\ 이제 쿼리를 제한했으므로 결과를 검색해 보겠습니다 이를 위해 findinbackground findinbackground 메서드를 호출하고 사용자 목록을 인수로 전달하여 쿼리의 결과가 저장될 것입니다 또한 오류를 처리하기 위해 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\ 오류가 발생하지 않으면, 현재 사용자와 가장 가까운 두 사용자가 nearusers nearusers 목록에 표시됩니다 (실제 현재 사용자와 그로부터 가장 가까운 사용자) 이제 현재 사용자가 아닌 사용자를 찾기만 하면 됩니다 그렇게 하려면, if (e == null) if (e == null) 블록 안에서 다음 코드를 사용하세요 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\ 이제 현재 사용자와 가장 가까운 사용자가 누구인지 알았으니, distanceinkilometersto distanceinkilometersto 메서드를 사용하여 그들 사이의 거리를 측정할 수 있습니다 그렇게 하려면, 다음 코드를 사용하세요 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 "); 위에서 사용된 alertdisplayer alertdisplayer 메서드는 다음과 같습니다 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에 연결하기 당신이 구축하고 있는 예제 앱이 물리적 제휴점이 있는 상점 그룹과 관련되어 있다고 가정해 보십시오 이 상점의 모든 물리적 제휴점을 지도에 표시하는 것은 흥미로울 것입니다 이를 위해 back4pp 대시보드에서 stores 클래스를 생성하고, 기존 상점을 parseobjects로 저장한 후, 그 위치를 저장하면 앱이 물리적 제휴점을 쿼리하고 지도에 그 위치를 표시할 수 있게 됩니다 다음 단계가 도움이 될 것입니다 1 back4app 웹사이트 , 로그인하고, 앱을 찾아 대시보드를 엽니다 2\ core core > browser browser > 클래스 생성 클래스 생성 , 아래 이미지와 같이 3\ what typ e of class do you need? e of class do you need? , 선택란에서 custom custom 옵션을 선택합니다 4\ 필드에서 무엇이라고 불러야 할까요? 무엇이라고 불러야 할까요? , “stores”를 입력한 후 클래스 생성 클래스 생성 버튼을 클릭하세요 5\ 그러면 “stores”라는 새로운 클래스가 표시되고, 여기에 2개의 열을 삽입해야 합니다 데이터 유형이 문자열 문자열 인 이름 이름 , 그리고 데이터 유형이 geopoint geopoint 인 위치 위치 열을 만드는 방법을 기억하지 못한다면, 2단계로 돌아가서 back4app 대시보드에서 열을 만드는 방법을 설명한 부분을 확인하세요 당신의 클래스는 이렇게 되어야 합니다 6\ 이제 이 클래스를 정보로 채우세요 그렇게 하려면, 아래 이미지에 표시된 것처럼 오른쪽 상단 또는 페이지 중앙의 행 추가 행 추가 버튼을 클릭하세요 7\ 그런 다음 필요한 정보로 행을 채우세요 상점의 이름과 위도 및 경도 일부 데이터를 입력한 후, 귀하의 상점 클래스는 아래 이미지와 같아야 합니다 더 많은 데이터를 입력하려면, 상단의 행 추가 행 추가 버튼을 클릭하거나, 데이터의 마지막 행 아래에 있는 + + 버튼을 클릭하세요 이제 귀하는 앱에서 상점의 위치 정보를 사용할 준비가 되었습니다 8 모든 상점의 위치를 지도에 표시하기 1\ 당신이 추가한 모든 상점을 mapsactivity mapsactivity 에서 보여주려면 parsequery를 사용해야 합니다 먼저, 쿼리할 back4pp 대시보드 클래스의 이름(이 경우 “stores” 클래스)을 알려주는 parseobject 쿼리를 생성해야 하며, whereexists whereexists , 쿼리를 수행하는 back4app의 store 클래스의 열(이 경우 “location” 열)을 지정해야 합니다 다음 코드는 이를 수행합니다 1 parsequery\<parseobject> query = parsequery getquery("stores"); 2 query whereexists("location"); 2\ 이제 쿼리를 제한했으므로 결과를 검색해 보겠습니다 이를 위해 findinbackground findinbackground 메서드를 호출하고, 쿼리 결과가 저장될 상점 목록과 오류를 처리할 parseexception을 인수로 전달합니다 whereexists whereexists 제한은 위치를 나타내는 parsegeopoint가 연결된 클래스의 parseobjects만 상점 목록에 저장되도록 합니다 오류를 피하려면 쿼리 실행 후 캐시된 결과를 지우는 것을 잊지 마세요 다음 코드는 이를 수행합니다 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\ 오류가 발생하지 않으면 stores stores 목록에 위치가 연결된 모든 상점이 있게 되며, 이제 이를 지도에 표시하기 위해 루프를 만들기만 하면 됩니다 그렇게 하려면 if (e == null) if (e == null) 블록 안에서 다음 코드를 사용하세요 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단계를 모두 실행하는 완전한 방법은 아래에 있습니다 작동하려면 onmapready onmapready 메서드를 호출하세요 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를 사용하여 수행할 수 있습니다 먼저, 쿼리할 back4pp 대시보드 클래스의 이름을 알리는 parseobject 쿼리를 생성해야 합니다(이 경우 “stores” 클래스) 그리고 wherenear wherenear , back4app의 store 클래스에서 쿼리를 수행하는 열(이 경우 “location” 열)을 알립니다 다음 코드는 이를 수행합니다 1 parsequery\<parseobject> query = parsequery getquery("stores"); 2 query wherenear("location", getcurrentuserlocation()); 2\ 이 쿼리의 결과 수를 제한하려면 setlimit setlimit 기본적으로 결과는 100개로 제한됩니다 wherenear wherenear 제한으로 인해 쿼리는 현재 사용자 위치에서 거리 순으로 정렬된 사용자 배열을 검색하게 되며, 가까운 사용자의 수를 1로 설정하면 쿼리 결과 목록에는 현재 사용자에게 가장 가까운 상점만 포함됩니다 다음 코드는 결과의 제한을 1로 설정합니다 1 query setlimit(1); 3\ 이제 쿼리를 제한했으니, 결과를 가져옵시다 이를 위해 findinbackground findinbackground 메서드를 호출하고, 쿼리 결과가 저장될 상점 객체의 리스트와 오류를 처리할 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\ 오류가 발생하지 않으면, nearstores nearstores 리스트에 현재 사용자와 가장 가까운 상점이 있을 것입니다 이제 이를 mapsactivity mapsactivity 에 표시하기만 하면 됩니다 이를 위해 if (e == null) if (e == null) 블록 안에서 다음 코드를 사용하세요 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)); 위에서 사용된 alertdisplayer alertdisplayer 메서드는 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\ 앱을 찾아서 대시보드 대시보드 > 코어 코어 > 브라우저 브라우저 > 사용자 사용자 를 클릭하고, 그들과 관련된 위치를 가진 사용자 몇 명을 생성하여 showclosestuser showclosestuser 메서드가 작동하도록 합니다 3\ 실제 기기에서 앱을 실행하고 계정을 등록하세요 모든 기능을 시도해보세요! 4\ back4app 대시보드로 돌아가서 사용자 행에 위치가 저장되어 있는지 확인하세요 완료되었습니다! 이 단계에서 back4app을 통해 parsegeopoint의 일부 기능을 사용할 수 있습니다!