2016-12-29 65 views
0

我正在製作一個Capstone項目我非常需要幫助,現在已經停留了幾天,我想在用戶點擊一個按鈕時向Firebase發送一個位置更新。更新設置爲每3秒發送一次位置更改。但問題是,只有按下按鈕纔會發送座標,而不會持續發送位置更改的更新。這裏是我的代碼OnLocationChange發送座標到Firebase

Start = (Button) findViewById(R.id.btnLoc); 
Stop = (Button) findViewById(R.id.btnStopSend); 

    mRef = new Firebase ("FIREBASE URL"); 

    Start.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

     listener = new LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 

       Firebase busCoords = mRef.child("Location"); 
       busCoords.setValue(location.getLatitude()+ ", "+location.getLongitude()); 
      } 

      @Override 
      public void onStatusChanged(String s, int i, Bundle bundle) { 
      } 

      @Override 
      public void onProviderEnabled(String s) { 
      } 

      @Override 
      public void onProviderDisabled(String s) { 
      } 
     }; 

      locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); 

      //noinspection MissingPermission 
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,1000,listener); 
     } 
    }); 

    Stop.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if(locationManager != null){ 
       //noinspection MissingPermission 
       locationManager.removeUpdates(listener); 
      } 

     } 
    }); 

} 
+0

您是否希望將此運行作爲服務在應用程序關閉時持續運行,或者您希望它僅在應用程序打開時運行?我認爲你可能會遇到Android的Activity Lifecycle問題。 –

+0

我的活動有兩個按鈕,發送和停止。當用戶點擊發送它不斷地發送當前位置在每個時間間隔 –

+0

同樣,你是否期望在活動在後臺或當活動在屏幕上可見時運行?我認爲您遇到了「活動生命週期」中的問題。 –

回答

0

有兩件事情,我看是怎麼回事:

1)只有最後記錄位置被記錄下來。當您有:

Firebase busCoords = mRef.child("Location"); 
busCoords.setValue(location.getLatitude()+ ", "+location.getLongitude()); 

你應該把:

Firebase busCoords = mRef.child("Location"); 
busCoords.push().setValue(location.getLatitude()+ ", "+location.getLongitude()); 

這將推動每個新位置作爲列表項的火力地堡數據庫。

2)這是可能的座標之間的距離過大,以及:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,1000,listener); 

這是預期至少1000米的距離,它記錄的另一個位置之前。在測試過程中,您可能需要考慮現在將其更改爲2或3米分隔。

這有幫助嗎?

0

使用GeoFire通過API提供火力得到位置更新。在你的onLocationChanged()方法你必須這樣做。

@Override 
public void onLocationChanged(Location location) { 
    geoFire.setLocation("location", new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() { 
     @Override 
     public void onComplete(String key, DatabaseError error) { 
      if (error != null) { 
       Toast.makeText(MapsActivity.this, "There was an error saving the location to GeoFire: " + error, Toast.LENGTH_LONG).show(); 
      } else { 
       Toast.makeText(MapsActivity.this, "Location saved on server successfully!", Toast.LENGTH_LONG).show(); 

      } 
     } 
    }); 
} 

這個方法在每次改變時都會給你LatLong。

+0

感謝您的幫助,我是否可以知道我應該在何處將代碼發送到我的Firebase, –

+0

已經編寫onLocation更改了方法,但在項目中添加geofire api之前 –

+0

我在哪裏插入此代碼? Firebase busCoords = mRef.child(「位置」); busCoords.setValue(location.getLatitude()+「,」+ location.getLongitude()); –