How to find the nearby users around the app users locality - android

Sorry for not providing any sample code because I don't have any idea about how to do this.
I'm coding an Android application which uses Firebase as the backend.
I'm storing the current current user location to mysl db using the fusedlocationapi.
Now, when a user is logged into the application, he/she is given a option to show only the list of user around a radius of 25km (let's assume as an example). Can any one guide me with this feature please.
I don.t wanna do this on client side ...is there any way to so it using mysql

You can use Google Maps Android API utility library
public class DistanceHelper {
private static final Double RADIUS = 25000.0;
public static double getDistance(LatLng user1, LatLng user2){
double distance = SphericalUtil.computeDistanceBetween(user1, user2);
return distance;
}
public static Double getRadius(){
return RADIUS;
}
public static String formatNumber(double distance) {
String unit = "m";
if (distance < 1) {
distance *= 1000;
unit = "mm";
} else if (distance > 1000) {
distance /= 1000;
unit = "km";
}
return String.format("%4.3f%s", distance, unit);
}
}
LatLng user1 = new LatLng(currentUser.getLatitude(), currentUser.getLongitude());
LatLng user2 = new LatLng(otherUser.getLatitude(), otherUser.getLongitude());
Double distance = DistanceHelper.getDistance(user1, user2);
if (distance <= DistanceHelper.getRadius()) {
//code here
}
I store the users' latitude and longitude in FB database using GPS and LocationManager. put the calculation inside your ChildEventListener to retrieve get users inside 25km radius.
However I think this implementation is wrong because the calculations are done on client side so what if you have million or billion of users then you have to compute each one by one on client's device which might cause very slow performance of your app.

Related

I need to sort the latitude and longitude. which is nearer to my current location

This is how i calculate the Distance between my list.
my code is below
public void calc(){
for(int i = 0; i < mSearchResultModelsToGetAppointments.size(); i++){
Location newLocation = new Location("");
newLocation.setLatitude(gpsTracker.getLatitude());
newLocation.setLongitude(gpsTracker.getLongitude());
if(String.valueOf(gpsTracker.getLatitude()).equalsIgnoreCase("0.0") || String.valueOf(gpsTracker.getLongitude()).equalsIgnoreCase("0.0")) {
} else{
Location oldLocation = new Location("");
oldLocation.setLatitude(mSearchResultModelUnderProcess.getLatitude());
oldLocation.setLongitude(mSearchResultModelUnderProcess.getLongitude());
float distanceInMeters = newLocation.distanceTo(oldLocation)/1000;
if(distanceInMeters < 2820){
getAdapter().notifyDataSetChanged();
getAdapter().removeItem(i);
getAdapter().notifyDataSetChanged();
}
}
}
}
This is how i display the location now.
If you do not want use any external library, I would suggest following way.
Create your own custom Location Class with attributes lat and lng, and additionally attribute called distance which measures from your current location till the point. here you can find how to find distance between 2 points
This class should implement Comparable interface and override its compare function. Inside this function, you can create your own sorting logic based on distance
here you can find how to use comparable interface, Let me know if you have still problem
Happy coding

Getting the Speed of the User - getSpeed and casting

I am currently trying to get the speed of a user using the GPS (Google Play Location Services).
However, the problem with my code is that the speed is stuck on a very small number - going down to 1.9^E18, basically 0. This value doesn't change even though I move around.
I am currently trying to calculate the speed of the user manually through using speed and time stamps. However, I have also found a function called getSpeed(). I was considering to use it, but there were many complaints I found here that said that it always returned zero.
Firstly - is getSpeed() only used for mock locations, or can it be used safely with location objects obtained by the GPS?
Secondly if getSpeed does not work, I will need to continue obtaining the speed manually. I believe there may be problems with the casting. Can someone help in finding the problem of my current method?
public void onLocationChanged(Location location) {
location.getLatitude();
Toast.makeText(this, "Location Changed", Toast.LENGTH_SHORT).show();
if (settings == null) {
settings = getSharedPreferences(PREFS_NAME, 0);
}
int TimeDifference = (int) (location.getTime() - OldLocation.getTime());
int distance = (int) OldLocation.distanceTo(location);
double speed = distance / TimeDifference;
if (LowerLimit < speed && speed < UpperLimit) {
TimeInstance += TimeDifference;
}
if (TimeInstance>TimeLimit) {
if (!dialogShown) {
dialogShown = true;
ViolationMessage();
TimeInstance = 0;
}
else {
TimeInstance = 0;
}
}
OldLocation = location;
}

Android geocoding by gps lat long

in my android app i calculate gps position and show the lat log value into TextView, and i use this lat long value for a query on SQLITE DB.
Now i'm trying to show into TextView the name of Location calculated by lat and log. I have try geocodeby i have soma error.
this is my code for calculate the lat and long value:
ImageView bt_get_mygpspos = (ImageView)getView().findViewById(R.id.bt_get_mygpspos);
edittext_mygpspos = (EditText)getView().findViewById(R.id.edittext_mygpspos);
bt_get_mygpspos.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
GPSTracker mGPS = new GPSTracker(getActivity());
if(mGPS.canGetLocation() ){
query.curPos = new LatLng(mGPS.getLatitude(),mGPS.getLongitude());
edittext_mygpspos.setText("lat:"+query.curPos.latitude+" lon:"+query.curPos.longitude);
Log.e("test", "lat:"+query.curPos.latitude+" lon:"+query.curPos.longitude);
}else{
mGPS.showSettingsAlert();
}
}
});
how i can trasform the mGPS.Latitude and Longitude in a name of Location to show into TextView?
Thans
You could use any public web-based API for geocoding like:
Google API;
Yandex API;
Yandex is better in ex-USSR space and have better daily free limits: 25 000 request when compare to 2 500 in Google. In any other detail Google is better.

Android: How can I get the traveled distance while I'm running?

I'm wrinting a application and I have to show the distance covered while I'm running.
I use the function "public void onLocationChanged" of the LocationListener. When the user tap a botton and start running I want to show the distance he covered updated to the point in which he is located.
I've written this code:
public void onLocationChanged(Location location) {
if(location != null) {
if(location.hasSpeed()){
if(latitude1 == 0 && longitude1 == 0){
latitude1 = (location.getLatitude()*Math.PI)/180;
longitude1 = (location.getLongitude()*Math.PI)/180;
} else{
latitude2 = (location.getLatitude()*Math.PI)/180;
longitude2 = (location.getLongitude()*Math.PI)/180;
distance = (6372.795477598)*Math.acos(Math.sin(latitude1)
*Math.sin(latitude2)+Math.cos(latitude1)
*Math.cos(latitude2)*Math.cos(longitude1-longitude2));
sumDistance += distance;
latitude1 = latitude2;
longitude1 = longitude2;
}
tv.setText("Distance covered=" + sumDistance + " m");
}
}
}
Is it accurated?
Just a Suggestion:
Store the Latitude and Longitude of the start location and the end location when the user clicks the appropriate button.
and then you could use distanceBetween or distanceTo to get the distance between those two geoPoints.
P.S: This may not work if the user will start and end his run at the same point ;)
Addition:
Check this tutorial:
Recently google has improved its location based API's. They have fused the sensors with the location based api's to share examples of how location can be made more accurate (about 4 times more effective) and consume much lesser power (10 times lesser).
Some interesting links for you to go through will be this and video google io.
SO link for how to use the API's here

Distance Travelled

I am creating a Phonegap App (using jQuery and Google Maps API V3) related to travelling and would like to show the user the distance he/she has travelled since starting the app.
Is there any Google function for this? Or can anyone recommend what I can do to get the total distance travelled?
It can be done easily. You have to understand, by using a Google Maps API for jQuery you have all needed tools to do it by yourself.
First, you need to define a time period (lets say 1 minute). We will need it to create a reference point of our movements. Next step is to use phonegap/cordova geolocation API to calculate our position (latitude and longitude):
http://docs.phonegap.com/en/1.0.0/phonegap_geolocation_geolocation.md.html
At this point choose do you want to:
Store a position (latitude and longitude) every 1 minute in some js array
Or do the same thing like in point 1 and also show it on the map with gmap addMarker ability.
At the end you need to calculate a distance. Loop through the position array and use latitude and longitude to calculate a distance between every available point. Here you will find function used for a such calculation (with a js example):
http://www.movable-type.co.uk/scripts/latlong.html
Or you can use Google Maps APi function:
google.maps.geometry.spherical.computeDistanceBetween (latLngA, latLngB);
EDIT:
Here's a working example for you: http://jsfiddle.net/Gajotres/6YpAG/
Code:
$(document).ready(function(){
// Chicago
var coordinateObject= new Object();
coordinateObject.lat = '41.83682786072714';
coordinateObject.lng = '-87.626953125';
geoLocationHandler.addLocation(coordinateObject);
// New York
var coordinateObject= new Object();
coordinateObject.lat = '40.58058466412761';
coordinateObject.lng = '-74.00390625';
geoLocationHandler.addLocation(coordinateObject);
// Toronto
var coordinateObject= new Object();
coordinateObject.lat = '43.70759350405294';
coordinateObject.lng = '-79.365234375';
geoLocationHandler.addLocation(coordinateObject);
var len = geoLocationHandler.arrLocations.length;
for (var i = 0; i < len; i++) {
if(i == 1){
geoLocationHandler.distance += geoLocationHandler.calcDistance(geoLocationHandler.arrLocations[i-1].lat,geoLocationHandler.arrLocations[i-1].lng,geoLocationHandler.arrLocations[i].lat,geoLocationHandler.arrLocations[i].lng);
}
}
alert(geoLocationHandler.distance);
});
var geoLocationHandler = {
arrLocations : [],
distance : 0,
addLocation:function(obj){
geoLocationHandler.arrLocations.push(obj);
},
calcDistance:function(fromLat, fromLng, toLat, toLng) {
return google.maps.geometry.spherical.computeDistanceBetween(new google.maps.LatLng(fromLat, fromLng), new google.maps.LatLng(toLat, toLng));
}
};
Final result is a distance expressed in meters.
Every time you have a new coordination use this to create a new object and add it to the array:
var coordinateObject= new Object();
coordinateObject.lat = '41.83682786072714'; // Change with your latitude
coordinateObject.lng = '-87.626953125'; // Change with your longitude
geoLocationHandler.addLocation(coordinateObject);

Categories

Resources