How to use coarse location to compare with saved location in database

安稳与你 提交于 2019-12-11 17:55:08

问题


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.

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.


回答1:


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
}


来源:https://stackoverflow.com/questions/10168545/how-to-use-coarse-location-to-compare-with-saved-location-in-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!