2016-12-26 3 views
3

Hallo Ich benutze Google Map mit LocationListener. Ich bin in der Lage Weg zwischen den Punkten zu zeichnen mit Fused APIAndroid Google Maps zeichnen Pfad beim Verschieben

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

und hier habe ich den Pfad ziehen:

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); 

              } 

Der Punkt ist, ich brauche Live-Pfad zu zeichnen, während Benutzer bewegt und zu stoppen wenn der Benutzer unabhängig von der Intervallzeit von LocationRequest stoppt.

Antwort

4

Probieren Sie diese Methode aus, damit Sie die Werte je nach Bedarf festlegen und ändern können. Wieviel Zeitintervall und wie viel Mindestabstand muss diese Methode aufrufen. Funktioniert auch ohne Internet.

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 Methode

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 Methode

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

Logic: Check lastlocation die currantLocation indem Variablen ist, und wenn ja, es bedeutet, sind Sie nicht bewegt, wenn kein Zeichnen Sie den Pfad

+0

Ihnen danken, aber ich möchte ein Verfahren ohne Zeitintervall Linie meine ich zeichnen, während Benutzer nicht nach bewegt Intervall, ich kann es wegen Batterieproblemen nicht sehr klein machen. Ich möchte, dass eine Prozedur nur funktioniert, wenn der Benutzer sich bewegt. – Radwa

+0

@Radwa bitte beachten Sie 'locationManager.requestLocationUpdates (LocationManager.GPS_PROVIDER, 1000, 1, myLocationListener);' dies bedeutet nicht, dass es nach jeder Sekunde aufgerufen wird, bedeutet es, dass die Differenz mindestens 1 Sekunde und 1 Meter betragen muss diese Methode wieder.Wenn sich der Standort nicht wieder ändert, wird er nie mehr aufgerufen –

+0

Ich bekomme deine Idee mein Problem ist, ich mache Intervall 1 Minute es ist den Weg zeichnen aber ignorieren Punkte, die ich in weniger als Minute Ie checkte, wenn ich bei A check, dann Wende nach B, dann C zeichne von Punkt A nach Punkt C und ignoriere Punkt b, da es weniger als Minute dauert – Radwa

0

in Ihnen Klasse diese Methode von Google api definieren:

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 . 

    } 

für nehmen weitere Details sehen here

+0

danke aber es ist veraltet und es braucht auch ein Zeitintervall – Radwa