2016-12-26 81 views
3

你好,我正在使用谷歌地圖LocationListener。我能點之間繪製路徑中使用融合APIAndroid谷歌地圖在移動時繪製路徑

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

這裏我繪製的路徑:

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

              } 

的一點是,我需要同時用戶移動繪製實時路徑和停止當用戶停止時,無論間隔時間爲LocationRequest

回答

4

試試這個方法,這樣您就可以根據需要決定和更改這些值。需要多少時間間隔以及多少分鐘距離才能調用此方法。沒有互聯網的工作。

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方法

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方法

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

邏輯:檢查lastlocationcurrantLocation通過保持變數,而且如果是的,它意味着你沒有,如果移動不繪製路徑

+0

謝謝你,但我想沒有時間間隔我的意思是畫線,而用戶後,移動不是一個方法間隔,由於電池問題,我不能使它間隔很小。我只想在用戶移動時使用一個程序。 – Radwa

+0

@Radwa請注意'locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,1,myLocationListener);'這並不意味着它在每秒意味着它被調用,差異需要至少1秒和1米的通話該方法再次。如果位置不再更改它永遠不會被調用更多 –

+0

我得到你的想法我的問題是我做了間隔1分鐘它繪製的路徑,但忽略點我檢查在不到分鐘即如果我檢查在A,然後轉到B,然後C從點A畫到點C忽略點b,因爲它不到分鐘 – Radwa

0
在你類

定義谷歌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 . 

    } 

瞭解更多詳細信息請參見here

+0

謝謝但它已被棄用,並且它也需要時間間隔 – Radwa