- Android Location.distanceBetween and Location.distanceTo difference
- Calculating distance between two geographic locations
- 7 Answers 7
- Get the distance between two locations in android?
- 10 Answers 10
- Android — Location Based Services
- The Location Object
- Get the Current Location
- Get the Updated Location
- Location Quality of Service
- Displaying a Location Address
- Example
- Create Android Application
Android Location.distanceBetween and Location.distanceTo difference
I encounter some issues on trying to calculate distance between one location and a list of others (in order to get the closest one). I tried by using Location.distanceBetween(. ) and location.distanceTo(. ), the two method are working but both of them send me different data and not any of them seems right.
My data came from a Class ConcessionDistance which is a basic extension of HashMap containing location data (as integer convert as string) and some other stuff that we don’t need here.
Can some one help me, to figure out where is the problem? I think of a conversion problem but I’m to deep in the problem to solve it myself 🙁
Any help will be welcome. Thanks by advance.
Edit: Here is part of my LogCat: I send to the emulator the gps location of «Strasbourg» (those are cities from east-france) and process distance to all those cities:
The real distance (given by google maps) between those cities and «Strasbourg» are :
Evry : 520km ( method1 : miss 175km, method2 : miss 145km)
Haguenau : 30km ( method1: add 70km, method2: return 0)
Besancon : 250km (method1: miss 140km, method2: miss 120km)
Brest : 1070km (method1: miss 200km, method2: miss 230km)
Hoenheim : 5km (method1: add 80km, method2: return 0)
Colmar : 75km (method1: miss 50km, method2: return 0)
Belfort : 155km (method1: miss 110km, method2: miss 20km)
Sarrebourg : 75km (method1: miss 5km, method2: return 0)
So we can notice that the second method return 0 when real distance is below
100km, wherease for the method1 I can’t see any logic inhere :/
Источник
Calculating distance between two geographic locations
please shed some light on this situation
Right now i have two array having latitude and longitude of nearby places and also have the user location latiude and longiude now i want to calculate the distance between user location and nearby places and want to show them in listview.
I know that there is a method for calculating distance as
Now what is the problem is how to pass these two array having nearby latitude and longitue in this method and get the array of distances.
7 Answers 7
Look into distanceTo
Returns the approximate distance in meters between this location and the given location. Distance is defined using the WGS84 ellipsoid.
Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them. Distance and bearing are defined using the WGS84 ellipsoid.
You can create a Location object from a latitude and longitude:
Try This Code. here we have two longitude and latitude values and selected_location.distanceTo(near_locations) function returns the distance between those places in meters.
here «distance» is distance between locationA & locationB (in Meters )
There is only one user Location, so you can iterate List of nearby places can call the distanceTo() function to get the distance, you can store in an array if you like.
From what I understand, distanceBetween() is for far away places, it’s output is a WGS84 ellipsoid.
distanceTo will give you the distance in meters between the two given location ej target.distanceTo(destination).
distanceBetween give you the distance also but it will store the distance in a array of float( results[0]). the doc says If results has length 2 or greater, the initial bearing is stored in results[1]. If results has length 3 or greater, the final bearing is stored in results[2]
hope that this helps
i’ve used distanceTo to get the distance from point A to B i think that is the way to go.
Источник
Get the distance between two locations in android?
i need to get distance between two location, but i need to get distance like blue line in the picture.
but i get red line distance.
10 Answers 10
Use the Google Maps Directions API. You’ll need to request the directions over HTTP. You can do this directly from Android, or via your own server.
For example, directions from Montreal to Toronto:
You’ll end up with some JSON. In routes[].legs[].distance , you’ll get an object like this:
You can also get the polyline information directly from the response object.
As Chris Broadfoot is correct, to parse returned JSON routes[].legs[].distance
You can use following method of Location class in android (if u have lat, longs of both the locations) the method returns approximate distance in meters.
public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)
Explanation:
Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them. Distance and bearing are defined using the WGS84 ellipsoid.
The computed distance is stored in results[0] . If results has length 2 or greater, the initial bearing is stored in results[1] . If results has length 3 or greater, the final bearing is stored in results[2] . Parameters:
startLatitude — the starting latitude
startLongitude the starting longitude
endLatitude the ending latitude
endLongitude the ending longitude
results an array of floats to hold the results
Источник
Android — Location Based Services
Android location APIs make it easy for you to build location-aware applications, without needing to focus on the details of the underlying location technology.
This becomes possible with the help of Google Play services, which facilitates adding location awareness to your app with automated location tracking, geofencing, and activity recognition.
This tutorial shows you how to use Location Services in your APP to get the current location, get periodic location updates, look up addresses etc.
The Location Object
The Location object represents a geographic location which can consist of a latitude, longitude, time stamp, and other information such as bearing, altitude and velocity. There are following important methods which you can use with Location object to get location specific information −
Sr.No. | Method & Description | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 |
Sr.No. | Callback Methods & Description | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 |
Sr.No. | Callback Method & Description | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 |
Sr.No. | Method & Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 |
Step | Description |
---|---|
1 | You will use Android studio IDE to create an Android application and name it as Tutorialspoint under a package com.example.tutorialspoint7.myapplication. |
2 | add src/GPSTracker.java file and add required code. |
3 | Modify src/MainActivity.java file and add required code as shown below to take care of getting current location and its equivalent address. |
4 | Modify layout XML file res/layout/activity_main.xml to add all GUI components which include three buttons and two text views to show location/address. |
5 | Modify res/values/strings.xml to define required constant values |
6 | Modify AndroidManifest.xml as shown below |
7 | Run the application to launch Android emulator and verify the result of the changes done in the application. |
Following is the content of the modified main activity file MainActivity.java.
Following is the content of the modified main activity file GPSTracker.java.
Following will be the content of res/layout/activity_main.xml file −
Following will be the content of res/values/strings.xml to define two new constants −
Following is the default content of AndroidManifest.xml −
Let’s try to run your Tutorialspoint application. I assume that, you have connected your actual Android Mobile device with your computer. To run the app from Android Studio, open one of your project’s activity files and click Run icon from the toolbar. Before starting your application, Android studio installer will display following window to select an option where you want to run your Android application.
Now to see location select Get Location Button which will display location information as follows −
Источник