Android
Data objects
Geoqueries
9 min
geoqueries introduction in this guide, you’ll learn how to perform geopoint querying on parse in an android application this tutorial uses an app created in android studio arctic fox 2020 3 1 with compilesdk = 30 compilesdk = 30 , minsdk = 23 minsdk = 23 and targetsdk = 30 targetsdk = 30 at any time, you can access the complete android project built with this tutorial at our github repositories kotlin example repository java example repository goal perform geoqueries using geopoints stored on back4app and android geolocation here is a preview of what we are gonna achieve prerequisites to complete this tutorial, we need android studio an app created on back4app note follow the new parse app tutorial to learn how to create a parse app on back4app an android app connected to back4app note follow the install parse sdk tutoria l to create an android studio project connected to back4app a device (or virtual device ) running android 4 1 (jelly bean) or newer let’s get started! before next steps, we need to connect back4app back4app to our application you should save the appid appid and clientkey clientkey from the back4app back4app to string xml string xml file and then init parse parse in our app java app java or app kt app kt file follow the new parse app tutorial if you don’t know how to init parse parse to your app or you can download the projects we shared the github links above and edit only the appid appid and clientkey clientkey parts according to you 1 save some data on back4app in this step, we will create a class with the js console and javascript codes provided by parse parse and we will create queries for this class let’s create a city city class, which will be the target of our queries in this guide on parse js console is possible to run javascript code directly, querying and updating your application database contents using the js sdk commands run the code below from your js console and insert the data on back4app here is how the js console looks like in your dashboard go ahead and create the city city class with the following example content 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(); 38 39 console log('success!'); 2 query the data from android app now that you have a populated class, we can now perform some geopoint queries in it let’s begin by ordering city city results by the nearest from kingston in jamaica (latitude 18 01808695059913 and longitude 76 79894232253473), using the parsequery wherenear parsequery wherenear method 1 parsequery\<parseobject> query = new parsequery<>("city"); 2 query wherenear("location",new parsegeopoint(18 018086, 76 798942)); 3 query findinbackground((objects, e) > { 4 if (e==null){ 5 initdata(objects); 6 } else { 7 toast maketext(mainactivity this, e getlocalizedmessage(), toast length short) show(); 8 } 9 });1 val query = parsequery\<parseobject>("city") 2 query wherenear("location", parsegeopoint(18 018086, 76 798942)) 3 query findinbackground { objects list\<parseobject>?, e parseexception? > 4 if (e == null) { 5 initdata(objects!!) 6 } else { 7 toast maketext(this\@mainactivity, e localizedmessage, toast length short) 8 show() 9 } 1 0 } let’s now query using the method parsequery wherewithinkilometers parsequery wherewithinkilometers , which will retrieve all results whose geopoint field is located within the max distance kingston will be used once again as a reference and the distance limit will be 3000 km 1 parsequery\<parseobject> query = new parsequery<>("city"); 2 query wherewithinkilometers("location",new parsegeopoint(18 018086, 76 798942),3000); 3 query findinbackground((objects, e) > { 4 if (e==null){ 5 initdata(objects); 6 } else { 7 toast maketext(mainactivity this, e getlocalizedmessage(), toast length short) show(); 8 } 9 });1 val query = parsequery\<parseobject>("city") 2 query wherewithinkilometers( 3 "location", 4 parsegeopoint(18 018086, 76 798942), 5 3000 0 6 ) 7 query findinbackground { objects list\<parseobject>?, e parseexception? > 8 if (e == null) { 9 initdata(objects!!) 10 } else { 11 toast maketext(this\@mainactivity, e localizedmessage, toast length short) 12 show() 13 } 14 } another useful query method is parsequery wherewithinpolygon parsequery wherewithinpolygon , which will query results whose geopoint field value is within the specified polygon, composed of an array of geopoints (at least three) if the polygon path is open, it will be closed automatically by parse connecting the last and first points for this example, you will be using a simple polygon that roughly contains the south american continent, composed of 5 distant geopoints in the ocean 1 parsequery\<parseobject> query = new parsequery<>("city"); 2 3 parsegeopoint geopoint1 = new parsegeopoint(15 822238344514378, 72 42845934415942); 4 parsegeopoint geopoint2 = new parsegeopoint( 0 7433770196268968, 97 44765968406668); 5 parsegeopoint geopoint3 = new parsegeopoint( 59 997149373299166, 76 52969196322749); 6 parsegeopoint geopoint4 = new parsegeopoint( 9 488786415007201, 18 346101586021952); 7 parsegeopoint geopoint5 = new parsegeopoint(15 414859532811047, 60 00625459569375); 8 parsegeopoint geopoint6 = new parsegeopoint(41 015137, 28 97953); 9 10 list\<parsegeopoint> list = new arraylist<>(); 11 list add(geopoint1); 12 list add(geopoint2); 13 list add(geopoint3); 14 list add(geopoint4); 15 list add(geopoint5); 16 list add(geopoint6); 17 query wherewithinpolygon("location",list); 18 19 query findinbackground((objects, e) > { 20 if (e==null){ 21 initdata(objects); 22 } else { 23 toast maketext(mainactivity this, e getlocalizedmessage(), toast length short) show(); 24 } 25 });1 val query = parsequery\<parseobject>("city") 2 val geopoint1 = parsegeopoint(15 822238344514378, 72 42845934415942) 3 val geopoint2 = parsegeopoint( 0 7433770196268968, 97 44765968406668) 4 val geopoint3 = parsegeopoint( 59 997149373299166, 76 52969196322749) 5 val geopoint4 = parsegeopoint( 9 488786415007201, 18 346101586021952) 6 val geopoint5 = parsegeopoint(15 414859532811047, 60 00625459569375) 7 val geopoint6 = parsegeopoint(41 015137, 28 97953) 8 val list mutablelist\<parsegeopoint> = 9 arraylist() 10 list add(geopoint1) 11 list add(geopoint2) 12 list add(geopoint3) 13 list add(geopoint4) 14 list add(geopoint5) 15 list add(geopoint6) 16 query wherewithinpolygon("location", list) 17 query findinbackground { objects list\<parseobject>?, e parseexception? > 18 if (e == null) { 19 initdata(objects!!) 20 } else { 21 toast maketext(this\@mainactivity, e localizedmessage, toast length short) 22 show() 23 } 24 } it’s done! at the end of this guide, you learned how geopoint data queries work on parse and how to perform them on back4app from an android app in the next guide, you will check how to create and manage users in parse