0

I know that I can get distance with google api and it is very good

But is it slow sometimes and not possible for my app

Is there any way to get distance between two location?

2
  • If it is slow, it's either because your app is slow or because your network is slow. It is very unlikely that Google's api are too slow for your app Commented Apr 13, 2017 at 12:35
  • @ArthurAttout Thanks.yes i am stupid that why google's api is slow,how is it affecting my app on google's api speed? Commented Apr 13, 2017 at 18:39

4 Answers 4

1

You should use Location.distanceBetween() or Location.distanceTo()

Sign up to request clarification or add additional context in comments.

3 Comments

Location.distanceBetween() or Location.distanceTo() get distance directly two location but i need to real distance two location
I am not getting your point what do you mean by real distance?
If by real distance you mean distance by road, then even if you find it slow I think it is simpler to use Google API. You'll need Google API to get road information anyway.
1

You could easily get the lastKnownLocation, add the co-ordinates to an ArrayList and then measure the distance between the points in that list as shown below:

Prerequisites:

Add permissions to manifest:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

Import Gradle Dependencies:

 compile 'com.google.android.gms:play-services-location:10.2.1' 

Add this class to your project:

Github: android-maps-utils : SphericalUtil.java

Implement LocationListener in your Activity:

public class myClass extends Activity implements LocationListener { 

Define your global Variables:

private LocationManager locationManager; private String provider; private ArrayList<LatLng> coordList = new ArrayList<String>(); 

Get Initial Location (in onCreate() or onClick()):

locationManager = (LocationManager) getActivity().getApplicationContext().getSystemService(Context.LOCATION_SERVICE); // Define the criteria how to select the locatioin provider -> use // default Criteria criteria = new Criteria(); provider = locationManager.getBestProvider(criteria, false); Location location = locationManager.getLastKnownLocation(provider); if (coordList != null && !coordList.isEmpty()) { coordList.get(0); } else { coordList.add(0, new LatLng(location.getLatitude(), location.getLongitude())); } if (location != null) { onLocationChanged(location); } getLocationUpdates(); 

create a getLocationUpdates() method:

 private void getLocationUpdates() { if (locationManager != null) { checkPermission(); locationManager.requestLocationUpdates(provider, ONE_SECOND, ONE_METER, this); } } 

Request location updates from your locationManager (make sure you requestPermission for Android 6.0 and up)

@Override public void onResume() { super.onResume(); getLocationUpdates(); } 

Use the SphericalUtil class to compute the distance within the ArrayList inside the onLocationChange function.

@Override public void onLocationChanged(Location location) { double distance = SphericalUtil.computeLength(coordList); distance = round(distance, 2) / 1000; distanceOutput.setText(distance + "km"); } 

Add the round functionality:

public static double round(double value, int places) { if (places < 0) throw new IllegalArgumentException(); BigDecimal bd = new BigDecimal(value); bd = bd.setScale(places, RoundingMode.FLOOR); return bd.doubleValue(); } 

This will get the distance between start and end points in km and round it down to two decimal places, feel free to convert them into any other unit of distance depending on your use-case.

4 Comments

Is it accurate?
It's accurate for me, yes. The feature i've used it for is counting steps and logging the walk on a GoogleMaps Fragment in real-time (which is in production currently). you'll have to test different things such as how often the onLocationChange is called to match what you want using the requestLocationUpdates method, i'm currently updating the method every meter the user walks. You may want something different.
It does depend on the users signal strength though, but it's not caused many problems for me thus far.
A point for you.I have only two location for get distance that google's api can get distance between two location
0

Google Distance matrix API need to use for the correct distance between places.

2 Comments

I think it is the API that @Frank finds too slow.
It is slow sometimes
0

After many search and faq,Finally I understood that google's api is best and first way to get distance two location

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.