Android
Data objects
Query GeoPoint su Android usando Parse e Back4App
9 min
geoquery introduzione in questa guida, imparerai come eseguire query geopoint su parse in un'applicazione android questo tutorial utilizza un'app creata in android studio arctic fox 2020 3 1 con compilesdk = 30 compilesdk = 30 , minsdk = 23 minsdk = 23 e targetsdk = 30 targetsdk = 30 in qualsiasi momento, puoi accedere al progetto android completo costruito con questo tutorial nei nostri repository github https //github com/templates back4app/android parse sdk kotlin https //github com/templates back4app/android parse sdk java obiettivo eseguire geoquery utilizzando geopoint memorizzati su back4app e geolocalizzazione android ecco un'anteprima di ciò che realizzeremo requisiti per completare questo tutorial, abbiamo bisogno di https //developer android com/studio/index html un'app creata su back4app nota segui il https //www back4app com/docs/get started/new parse app per imparare a creare un'app parse su back4app un'app android collegata a back4app nota segui il https //www back4app com/docs/android/parse android sdk per creare un progetto android studio collegato a back4app un dispositivo (o https //developer android com/studio/run/managing avds html ) che esegue android 4 1 (jelly bean) o versioni successive iniziamo! prima dei prossimi passi, dobbiamo connettere back4app back4app alla nostra applicazione dovresti salvare il appid appid e clientkey clientkey da back4app back4app nel file string xml string xml e poi inizializza parse parse nel nostro app java app java o app kt app kt file segui il https //www back4app com/docs/android/parse android sdk se non sai come inizializzare parse parse nella tua app oppure puoi scaricare i progetti di cui abbiamo condiviso i link github sopra e modificare solo le parti appid appid e clientkey clientkey secondo le tue esigenze 1 salva alcuni dati su back4app in questo passaggio, creeremo una classe con la console js e i codici javascript forniti da parse parse e creeremo query per questa classe creiamo una città città , che sarà l'obiettivo delle nostre query in questa guida sulla console js di parse è possibile eseguire codice javascript direttamente, interrogando e aggiornando i contenuti del database della tua applicazione utilizzando i comandi del js sdk esegui il codice qui sotto dalla tua console js e inserisci i dati su back4app ecco come appare la console js nel tuo dashboard procedi e crea la città città classe con il seguente contenuto di esempio 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 interroga i dati dall'app android ora che hai una classe popolata, possiamo eseguire alcune query geopoint in essa iniziamo ordinando i risultati della città città in base alla più vicina a kingston in giamaica (latitudine 18 01808695059913 e longitudine 76 79894232253473), utilizzando il metodo parsequery wherenear parsequery wherenear 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 } ora facciamo una query utilizzando il metodo parsequery wherewithinkilometers parsequery wherewithinkilometers , che recupererà tutti i risultati il cui campo geopoint si trova entro la distanza massima kingston sarà utilizzato ancora una volta come riferimento e il limite di distanza sarà di 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 } un altro metodo di query utile è parsequery wherewithinpolygon parsequery wherewithinpolygon , che interrogherà i risultati il cui valore del campo geopoint si trova all'interno del poligono specificato, composto da un array di geopoints (almeno tre) se il percorso del poligono è aperto, verrà chiuso automaticamente da parse collegando gli ultimi e i primi punti per questo esempio, utilizzerai un semplice poligono che contiene grossolanamente il continente sudamericano, composto da 5 geopoints distanti nell'oceano 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 } è fatto! alla fine di questa guida, hai imparato come funzionano le query sui dati geopoint su parse e come eseguirle su back4app da un'app android nella prossima guida, verificherai come creare e gestire gli utenti in parse