Android
Data objects
在Android应用中使用Parse进行GeoPoint查询指南
9 分
地理查询 介绍 在本指南中,您将学习如何在 android 应用程序中对 parse 执行 geopoint 查询。 本教程使用在 android studio arctic fox 2020 3 1 中创建的应用程序, compilesdk = 30 compilesdk = 30 , minsdk = 23 minsdk = 23 和 targetsdk = 30 targetsdk = 30 在任何时候,您都可以在我们的 github 仓库中访问使用本教程构建的完整 android 项目 https //github com/templates back4app/android parse sdk kotlin https //github com/templates back4app/android parse sdk java 目标 使用存储在 back4app 上的地理点和 android 地理位置执行地理查询。 以下是我们将要实现的预览: 前提条件 要完成本教程,我们需要: https //developer android com/studio/index html 在 back4app 上创建的应用程序。 注意: 请遵循 https //www back4app com/docs/get started/new parse app 以了解如何在 back4app 上创建 parse 应用程序。 一个连接到 back4app 的安卓应用程序。 注意: 请遵循 https //www back4app com/docs/android/parse android sdk 以创建一个连接到 back4app 的 android studio 项目。 一台运行 android 4 1(果冻豆)或更高版本的设备(或 https //developer android com/studio/run/managing avds html )。 让我们开始吧! 在下一步之前,我们需要将 back4app back4app 连接到我们的应用程序。您应该保存 appid appid 和 clientkey clientkey 从 back4app back4app 到 string xml string xml 文件,然后在我们的 app java app java 或 app kt app kt 文件中初始化 https //www back4app com/docs/android/parse android sdk 如果您不知道如何将 parse parse 初始化到您的应用程序中。 或者您可以下载我们在上面分享的 github 链接的项目,并根据您的需要仅编辑 appid appid 和 clientkey clientkey 部分。 1 在 back4app 上保存一些数据 在这一步中,我们将使用 parse parse 提供的 js 控制台和 javascript 代码创建一个类,并为该类创建查询。 让我们创建一个 城市 城市 类,这将是我们在本指南中查询的目标。在 parse js 控制台中,可以直接运行 javascript 代码,使用 js sdk 命令查询和更新您的应用程序数据库内容。从您的 js 控制台运行下面的代码,并将数据插入到 back4app 中。 以下是您的仪表板中 js 控制台的样子: 继续创建 城市 城市 类,内容如下示例: 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 从 android 应用查询数据 现在您已经有了一个填充的类,我们可以在其中执行一些 geopoint 查询。让我们开始按距离最近的方式对来自牙买加金斯敦的 城市 城市 结果进行排序(纬度 18 01808695059913 和经度 76 79894232253473),使用 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 } 现在让我们使用方法进行查询 parsequery wherewithinkilometers parsequery wherewithinkilometers , 这将检索所有其 geopoint 字段位于最大距离内的结果。金斯顿将再次作为参考,距离限制为 3000 公里。 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 } 另一个有用的查询方法是 parsequery wherewithinpolygon parsequery wherewithinpolygon , 这将查询其 geopoint 字段值位于指定多边形内的结果,该多边形由一组 geopoints(至少三个)组成。如果多边形路径是开放的,parse 将自动通过连接最后一个和第一个点来关闭它。 在这个例子中,您将使用一个简单的多边形,大致包含南美洲大陆,由 5 个远离海洋的 geopoints 组成。 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 } 完成了! 在本指南的最后,您了解了如何在parse上进行geopoint数据查询,以及如何从android应用程序在back4app上执行这些查询。在下一个指南中,您将查看如何在parse中创建和管理用户。