How to find distance between two longitude location? [duplicate] - android

This question already has answers here:
How to calculate distance between two locations using their longitude and latitude value
(14 answers)
Closed 6 years ago.
I want your help to calculate the distance b/t two longitudes. Can anybody tell me how to find the distance? I am new on Android studio. I want to find the distance in my app. Destination address in defined by mlat and mlong and current address is calculated through GPS whose value goes in longitude and latitude.
My code is:
double mlat =30.726030;
double mlong=76.759268;
imgButton1 =(ImageButton)findViewById(R.id.imageButton1);
mgButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Location gpsLocation = appLocationService.getLocation(LocationManager.GPS_PROVIDER);
if (gpsLocation != null) {
double latitude = gpsLocation.getLatitude();
double longitude = gpsLocation.getLongitude();
String result = "Latitude: " + gpsLocation.getLatitude() +
" Longitude: " + gpsLocation.getLongitude();
tvAddress.setText(result);
} else {
showSettingsAlert();
}
}
});
Tell me how to find the distance between two longitudes and latitudes.

Use the distanceBetween method to calculate distance:
Location.distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)

Related

do not concatenate text displayed with settext

How can i have bot the lat and log in one textview separated with a comma?
I tried as below and i am getting the "do not concatenate text displayed with settext" warning!
gpsTracker = new GpsTracker( MainActivity.this);
if(gpsTracker.canGetLocation()){
double latitude = gpsTracker.getLatitude();
double longitude = gpsTracker.getLongitude();
textviewGPSLocation.setText(String.valueOf(latitude) + "/," + String.valueOf ( longitude ));
You can format your string like this
double latitude = gpsTracker.getLatitude();
double longitude = gpsTracker.getLongitude();
String text = String.format("%1$2f / %2$2f", latitude, longitude);
textviewGPSLocation.setText(text);

GPS data on android phone

I am building an app which can detect location change by reading GPS data. I am observing the change on longitude and latitude to detect the location change. But my app is only working when I go far away. The value I get on latitude and longitude is only upto two decimal points.Is there any way to get more precise data of latitude and longitude in android? I want get the value upto 7 or 8 decimal point. My code is given below-
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.d("GPS ", "ON");
mysample = mysample + "GPS = 1 \n";
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double latitude = 0;
double longitude = 0;
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
if ( plat != latitude || plong != longitude) {
Log.d("GPS MOVING ", "ON");
mysample = mysample + "GPS MOVING = 1 \n";
Log.d("GPS CORDINATE ", "" + longitude + "" + latitude);
plat = latitude;
plong = longitude;
} else {
Log.d("GPS MOVING ", "OFF");
mysample = mysample + "GPS MOVING = 0 \n";
Log.d("GPS CORDINATE ", "" + longitude + "" + latitude);
}
} else {
Log.d("GPS ", "OFF");
mysample = mysample + "GPS = 0 \n";
Log.d("GPS MOVING ", "OFF");
mysample = mysample + "GPS MOVING = 0 \n";
}
Getting precise data of latitude and longitude is a nice idea however, setting location results to 7 or 8 decimal points is practically used in surveying purposes, as shown in Decimal degrees in Wikipedia.
A perfect explanation for this concern can be found in this thread.
Also, please check solution given in this SO post - How to better handle GPS location and android. Hope it helps!

How to get Current Lat Lng position on GMAPS API V3?

I'm trying to calculate my route, from my current position to the specific location (var: end). How I suppose to get my LatLng from my current position ? Thx
Here's my Code :
function calcRoute() {
var start = "How to get this LatLng ?";
var end = new google.maps.LatLng(-6.28529,106.871542);
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.WALKING
};
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
}
If you have a Location object (lets call it location), all you need to do is LatLng mLatLng = new LatLng(location.getLatitude(), location.getLongitude()) and you will have a LatLng object. location.getLatitude() returns a double corresponding to the latitude in degrees and location.getLongitude() returns the same but for longitude. See the documentation for more details: http://developer.android.com/reference/android/location/Location.html

Geocoding android - Longitude and Latitude values incorrect

I Have an address that I would like to know the coordinates of. For instance the address is "Skillman Ave" Queens, NY. The coordinates is: 40.747281, -73.9283169 according to maps.google.com. In my app I have a function like this:
public GeoPoint addressToGeo(String adr) {
Geocoder coder = new Geocoder(this);
List<Address> address = null;
GeoPoint coordinates;
try {
address = coder.getFromLocationName(adr, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (address == null) {
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
coordinates = new GeoPoint((int) (location.getLatitude() *1E6),
(int) (location.getLongitude() * 1E6));
return coordinates;
}
Which takes an address as parameter, and hopefully it will return the coordinates. My debugger says the the first element in the list adress contains this information:
[Address[addressLines=[0:"Skillman Ave",1:"Queens, New York",2:"Amerikas forente stater"],feature=Skillman Ave,admin=New York,sub-admin=Queens,locality=Queens,thoroughfare=Skillman Ave,postalCode=null,countryCode=US,countryName=Amerikas forente stater,hasLatitude=true,latitude=40.747281,hasLongitude=true,longitude=-73.9283169,phone=null,url=null,extras=null]]
which seems correct if you look at the latitude and longitude variables. But when I type in this code:
GeoPoint test;
test = addressToGeo("Skillman Ave");
double latitude = test.getLatitudeE6();
double longitude = test.getLongitudeE6();
String lat = Double.toString(latitude);
String lng = Double.toString(longitude);
String total = lat + " " + lng;
toAdress.setText(total);
the toAdress textField will contain 4.0747281E7, -7.3928316E7 The comma is not in the right spot, and what is the E7 in the end of each double?
'E7' is notation meaning that you need to multiply by 10^7 to get the actual number. In this case it would give you 40747281. You then need to format that into a proper coordinate.
Ankit's code looks like it might do that, but test to make sure.
Try this.
String lat = Double.toString(latitude);
String lng = Double.toString(longitude);
lat= (float) (lat / 1E6);
lng = (float)(lon / 1E6);
System.out.println("lat :" + (float) lat / 1E6);
System.out.println("lon :" + (float) lon / 1E6);
You've got all the right data, so this question is really about formatting a double. Use DecimalFormat.
Use this to display the lat/lon points in your test:
DecimalFormat formatter = new DecimalFormat("0.0000000");
String lat = formatter.format(test.getLatitudeE6() / 1E6);
String lon = formatter.format(test.getLongitudeE6() / 1E6);
toAddress.setText(lat + " " + lon);

How to get the latitude and longitude or gps data quickly while we are indoor in an android phone?

I'm trying to get the latitude and longitude information from an android phone through GPS, when i'm outdoor or under the sky directly i'm able to get the values instantly but when i'm indoor or inside a room its taking more than a minute to get the values. Can anyone help me in getting this values fastly when I'm using my app inside a room.
I'm using the following code in getting the values:
LocationManager locManager;
locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f,
locationListener);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
and
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
EditText myLocationText = (EditText)findViewById(R.id.editText1);
EditText myLocationText1 = (EditText)findViewById(R.id.editText2);
String latString = "";
String LongString = "";
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latString = "" + lat;
LongString ="" + lng;
} else {
latString = "No location found";
LongString = "No location found";
}
myLocationText.setText(""+ latString);
myLocationText1.setText(""+ LongString);
}
Is there any other way in getting the GPS values other than using LocationManager??
You can get the last known location, and it's quite fast:
/**
* Gets the last known location.
*
* #param locationManager the location manager
* #return the last known location
*/
public static Location getLastKnownLocation(LocationManager locationManager)
{
Location bestResult = null;
float bestAccuracy = 10000;
long bestTime = 0;
List<String> matchingProviders = locationManager.getAllProviders();
for (String provider: matchingProviders) {
Log.d("LOCATION", "Provider: " + provider);
Location location = locationManager.getLastKnownLocation(provider);
Log.d("LOCATION", "Location found? "+ (location==null?"NO":"YES"));
if (location != null) {
float accuracy = location.getAccuracy();
long time = location.getTime();
Log.d("LOCATION", "Acc: "+ String.valueOf(accuracy) + " -- Time: " + String.valueOf(time));
if ((time > minTime && accuracy < bestAccuracy)) {
bestResult = location;
bestAccuracy = accuracy;
bestTime = time;
}
else if (time < minTime &&
bestAccuracy == Float.MAX_VALUE && time > bestTime){
bestResult = location;
bestTime = time;
}
}
}
Log.d("LOCATION", "BEST FOUND? "+ (bestResult==null?"NO":"YES"));
return bestResult;
}
You can get the last known location of the device using the following method in LocationManager, this is the location that has been last found system-wide.
This method is the key:
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
See for more details Obtaining User Location
If you are indoors, you would be better of using LocationManager.NETWORK_PROVIDER as you are unlikely to get a signal inside. You could also use getLastKnownLocation(), but this may be non existent or out of date.
If you want get position in a room, gps provider is slowly. You can try changing:
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f,
locationListener);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
By:
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000L,500.0f,
locationListener);
And you will receive location in 'public void onLocationChanged(Location location)' method.
I used Network provider to fetch the Co-Ordinates and it is always much faster inside the house in living room. But when I try the same in the Basement area, it doesn't fetch the coordinates at all though I waited almost couple of minutes.
Note: I could able to make a call from basement area and I am also able to browse as well..

Categories

Resources