4

Hello I am using google map with LocationListener. I am able to draw path between points using Fused API

    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1000 * 60 * 1);
        mLocationRequest.setFastestInterval(1000 * 60 * 1);
                                            }
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

and here I draw the path:

    public void onLocationChanged(Location location) {
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(latLng);
                routePoints.add(latLng);
                Polyline route = mGoogleMap.addPolyline(new PolylineOptions()
                        .width(5)
                        .color(Color.BLUE)
                        .geodesic(false)
                        .zIndex(3));
                route.setPoints(routePoints);

                                            }

The point is, I need to draw live path while user is moving and stop when user stop regardless of interval time of LocationRequest.

Charuක
  • 12,953
  • 5
  • 50
  • 88
Radwa
  • 325
  • 4
  • 21

2 Answers2

5

Try this this method , so you can decide and change the values as you need. How much time interval and how much min distance is need to call this method. Works without internet too.

    private LocationManager locationManager;
    private android.location.LocationListener myLocationListener;

    public void checkLocation() {

        String serviceString = Context.LOCATION_SERVICE;
        locationManager = (LocationManager) getSystemService(serviceString);


        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }


        myLocationListener = new android.location.LocationListener() {
            public void onLocationChanged(Location locationListener) {

                if (isGPSEnabled(YourActivityName.this)) {
                    if (locationListener != null) {
                        if (ActivityCompat.checkSelfPermission(YourActivityName.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(YourActivityName.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                            return;
                        }

                        if (locationManager != null) {
                            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                } else if (isInternetConnected(YourActivityName.this)) {
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }



            }

            public void onProviderDisabled(String provider) {

            }

            public void onProviderEnabled(String provider) {

            }

            public void onStatusChanged(String provider, int status, Bundle extras) {

            }
        }; 

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, myLocationListener);  //  here the min time interval and min distance 
}

isInternetConnected method

public static boolean isInternetConnected(Context ctx) {
        ConnectivityManager connectivityMgr = (ConnectivityManager) ctx
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifi = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        // Check if wifi or mobile network is available or not. If any of them is
        // available or connected then it will return true, otherwise false;
        if (wifi != null) {
            if (wifi.isConnected()) {
                return true;
            }
        }
        if (mobile != null) {
            if (mobile.isConnected()) {
                return true;
            }
        }
        return false;
    }

isGpsEnabled method

  public boolean isGPSEnabled(Context mContext) {
        LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

Logic: check lastlocation is the currantLocation by keeping variables and if yes it means you are not moved if no draw the path

Charuක
  • 12,953
  • 5
  • 50
  • 88
  • thank you but I want a method without time interval I mean draw line while user is moving not after interval , I cant make it very small interval due to battery issues. I want a procedure working only when user is moving. – Radwa Dec 26 '16 at 11:26
  • @Radwa please note `locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, myLocationListener);` this does not means it gets called after every second it means , the difference needs to be at least a 1 second and 1 meter to call that method again.If the location does not change again it never gets called more – Charuක Dec 26 '16 at 11:35
  • I get your idea my problem is i made interval 1 minute it's draw the path but ignoring points I checked in less than minute I.e if I check at A, then turn to B ,then C it draw from point A to point C ignoring point b as it take less than minute – Radwa Dec 26 '16 at 11:37
  • @Radwa Its because the way you draw path is wrong.you are adding currant position and draw a line.It should be from last location to your currant location. so every time you call it it will draw a one from previous location to currant location – Charuක Dec 26 '16 at 11:39
  • @Radwa check the logic that i have used -->http://stackoverflow.com/a/41294229/5188159 – Charuක Dec 26 '16 at 11:41
  • ok I am gonna check it but I draw the path from last location the problem is point B as I mentioned was within the minute and after minute user was at point C so it simply connect last loc A with new loc C ignoring B you get me – Radwa Dec 26 '16 at 11:46
  • @Radwa yes if you call get location after a minute whatever the locations you pass through that minute wont be added. Correct. Thats why i suggest you to get the location based on the change of the position/distance instead of the time When ever you chage your possition get that location.Advantage is if you use a timer even you are in the same spot it gets called but this doesnt. Otherwise you wont have a solution i guess – Charuක Dec 26 '16 at 11:50
  • Thanks a lot I thought so too is it good approach I mean for battery ? – Radwa Dec 26 '16 at 12:00
  • @Radwa it works fine i used it for main taxi app along with the method i provided so far results are good. – Charuක Dec 26 '16 at 12:02
  • @Radwa call that method once after that `locationManager` listener will call it depending on the data you gave to it's params eg: after 1 meter distance change – Charuක Dec 26 '16 at 12:11
  • @Charu, can you plz provide working example with poly line draw. In my case it is drawing line though my mobile on same position. I followed your method. – Tara Jan 10 '20 at 13:53
0

in you class define this method of google api :

private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
    @Override
    public void onMyLocationChange(Location location) {
     // make your code to draw path , and refresh the map to not hve a ot of path .

     }

for more details take see here

Community
  • 1
  • 1