如何使用粗略的位置与数据库保存的位置比较 [英] How to use coarse location to compare with saved location in database

查看:198
本文介绍了如何使用粗略的位置与数据库保存的位置比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个Android应用程序,其中用户将使用大地定位使用网络位置粗略的位置。我希望用户能够保存在特定的时间地点到DB(savedLocation),然后如果用户返回到savedLocation,(或位置的公差范围,因为我告网络位置中)我想火到广播接收器。我知道罚款,以及如何将用户的位置保存到数据库,并把它与当前位置比较,等等等等。

I am writing an android app where a user will geolocate using coarse location using Network Location. I want the user to be able to save a location at a particular time into a db (savedLocation), and then if the user returns to savedLocation, (or within a coarse range of that location since i'm suing network location) I want to fire to a broadcast receiver. I know fine and well how to save the user's location to the db, and to compare it with the current location, etc etc.

然而,由于网络位置是用于确定位置的相对不准确的方法中,当用户认为savedLocation的距离X内接近,这将是用于确定一个很好的方法。

However, given that the network location is a relatively inaccurate method for determining location, what would be a good approach for determining when the user approaches within distance X of that savedLocation.

推荐答案

从你的问题是什么不解的是,你正在跟踪用户的位置和保存的位置数据库。现在要提示用户,当用户达到了previous的位置,但应该有一个特定区域。如果是这样,那么你可以用下面的code到知道,从经纬度用户的区域。

What understand from your question is that you are tracking the user's location and saving that locations to the database. Now you want to prompt user when user reach to the previous location but there should be a specific region. If this is then you can use the below code to know the region of the user from latitude and longitude.

String latitude1, latitude2, longitude1, longitude2;
//Now Suppose you have some values in this variables. 
//In your case latitude1 and longitude1 is from database and latitude2 and longitude2 is from current Location.
float radius = 500; //Distance in meters

Location location1 = new Location("GPS");
location1.setLatitude(latitude1);
location1.setLongitude(longitude1);

Location location2 = new Location("GPS");
location2.setLatitude(latitude2);
location2.setLongitude(longitude2);

Float distance = location1.distanceTo(location2);
distance = Math.abs(distance);

//Here you can compare two distances 1. distance and 2. radius
if(distance <= radius) {
//Use is in a 500 meters radius. You can notify user by notification
}

这篇关于如何使用粗略的位置与数据库保存的位置比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆