2016-07-07 69 views
0

我已創建一個Android應用,該用戶的用戶登錄和註冊鏈接到我的Firebase數據庫。將Android用戶位置存儲在Firebase數據庫中,並使用Geofire將其顯示在地圖上

我想使用Geofire來存儲並顯示在地圖上登錄的用戶,我已經使用了SF車輛示例,如果我是誠實的,我不太瞭解它。我使用完全相同的代碼來測試,看看它是否工作,我得到了提供的圖像中的錯誤。 Error on device when I open the app

我正在尋找幫助,編寫一個函數來檢測登錄用戶的位置,並實時顯示在地圖上,隨着用戶移動更新,我在這個星期卡住了,並嘗試了所有在那裏在那裏(這不是很多)。

一些幫助將不勝感激,任何進一步的信息只需要問。

這是我使用的SF Vehicle示例的代碼,原件連接到San Fransisco巴士部門並顯示它們,所以我需要代碼將位置寫入我的數據庫並將它們顯示在相同的位置辦法。

package nixer.nixer; 

import android.app.AlertDialog; 
import android.app.Fragment; 
import android.content.Intent; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.SystemClock; 
import android.support.v4.app.FragmentActivity; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.animation.AccelerateDecelerateInterpolator; 
import android.view.animation.Interpolator; 
import com.firebase.client.Firebase; 
import com.firebase.client.FirebaseError; 
import com.firebase.geofire.GeoFire; 
import com.firebase.geofire.GeoLocation; 
import com.firebase.geofire.GeoQuery; 
import com.firebase.geofire.GeoQueryEventListener; 
import com.firebase.geofire.LocationCallback; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.*; 
import java.util.HashMap; 
import java.util.Map; 


public class MapActivity extends AppCompatActivity implements GeoQueryEventListener, GoogleMap.OnCameraChangeListener { 

private Firebase mRef; 
private String mUserId; 
private String itemsUrl; 
private static final GeoLocation INITIAL_CENTER = new GeoLocation(53.349805, -6.260309); 
private static final int INITIAL_ZOOM_LEVEL = 14; 
private static final String GEO_FIRE_REF = "https://nixer.firebaseio.com/"; 
private GoogleMap map; 
private Circle searchCircle; 
private GeoFire geoFire; 
private GeoQuery geoQuery; 

private Map<String,Marker> markers; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_map); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    // setup map and camera position 
    SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 
    this.map = mapFragment.getMap(); 
    LatLng latLngCenter = new LatLng(INITIAL_CENTER.latitude, INITIAL_CENTER.longitude); 
    this.searchCircle = this.map.addCircle(new CircleOptions().center(latLngCenter).radius(1000)); 
    this.searchCircle.setFillColor(Color.argb(66, 255, 0, 255)); 
    this.searchCircle.setStrokeColor(Color.argb(66, 0, 0, 0)); 
    this.map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLngCenter, INITIAL_ZOOM_LEVEL)); 
    this.map.setOnCameraChangeListener(this); 

    Firebase.setAndroidContext(this); 

    // setup GeoFire 
    this.geoFire = new GeoFire(new Firebase(GEO_FIRE_REF)); 
    // radius in km 
    this.geoQuery = this.geoFire.queryAtLocation(INITIAL_CENTER, 1); 

    // setup markers 
    this.markers = new HashMap<String, Marker>();  




    // Check Authentication 
    mRef = new Firebase(Constants.FIREBASE_URL); 
    if (mRef.getAuth() == null) { 
     loadLoginView(); 
    } 


    try { 
     mUserId = mRef.getAuth().getUid(); 
    } catch (Exception e) { 
     loadLoginView(); 
    } 

} 




@Override 
protected void onStop() { 
    super.onStop(); 
    // remove all event listeners to stop updating in the background 
    this.geoQuery.removeAllListeners(); 
    for (Marker marker: this.markers.values()) { 
     marker.remove(); 
    } 
    this.markers.clear(); 
} 

@Override 
protected void onStart() { 
    super.onStart(); 
    // add an event listener to start updating locations again 
    this.geoQuery.addGeoQueryEventListener(this); 
} 

@Override 
public void onKeyEntered(String key, GeoLocation location) { 
    // Add a new marker to the map 
    Marker marker = this.map.addMarker(new MarkerOptions().position(new LatLng(location.latitude, location.longitude))); 
    this.markers.put(key, marker); 
} 

@Override 
public void onKeyExited(String key) { 
    // Remove any old marker 
    Marker marker = this.markers.get(key); 
    if (marker != null) { 
     marker.remove(); 
     this.markers.remove(key); 
    } 
} 

@Override 
public void onKeyMoved(String key, GeoLocation location) { 
    // Move the marker 
    Marker marker = this.markers.get(key); 
    if (marker != null) { 
     this.animateMarkerTo(marker, location.latitude, location.longitude); 
    } 
} 

@Override 
public void onGeoQueryReady() { 
} 

@Override 
public void onGeoQueryError(FirebaseError error) { 
    new AlertDialog.Builder(this) 
      .setTitle("Error") 
      .setMessage("There was an unexpected error querying GeoFire: " + error.getMessage()) 
      .setPositiveButton(android.R.string.ok, null) 
      .setIcon(android.R.drawable.ic_dialog_alert) 
      .show(); 
} 

// Animation handler for old APIs without animation support 
private void animateMarkerTo(final Marker marker, final double lat, final double lng) { 
    final Handler handler = new Handler(); 
    final long start = SystemClock.uptimeMillis(); 
    final long DURATION_MS = 3000; 
    final Interpolator interpolator = new AccelerateDecelerateInterpolator(); 
    final LatLng startPosition = marker.getPosition(); 
    handler.post(new Runnable() { 
     @Override 
     public void run() { 
      float elapsed = SystemClock.uptimeMillis() - start; 
      float t = elapsed/DURATION_MS; 
      float v = interpolator.getInterpolation(t); 

      double currentLat = (lat - startPosition.latitude) * v + startPosition.latitude; 
      double currentLng = (lng - startPosition.longitude) * v + startPosition.longitude; 
      marker.setPosition(new LatLng(currentLat, currentLng)); 

      // if animation is not finished yet, repeat 
      if (t < 1) { 
       handler.postDelayed(this, 16); 
      } 
     } 
    }); 
} 

private double zoomLevelToRadius(double zoomLevel) { 
    // Approximation to fit circle into view 
    return 16384000/Math.pow(2, zoomLevel); 
} 

@Override 
public void onCameraChange(CameraPosition cameraPosition) { 
    // Update the search criteria for this geoQuery and the circle on the map 
    LatLng center = cameraPosition.target; 
    double radius = zoomLevelToRadius(cameraPosition.zoom); 
    this.searchCircle.setCenter(center); 
    this.searchCircle.setRadius(radius); 
    this.geoQuery.setCenter(new GeoLocation(center.latitude, center.longitude)); 
    // radius in km 
    this.geoQuery.setRadius(radius/1000); 
} 

更新:

火力地堡規則:

{ 
"rules": { 
    "users": { 
    "$uid": { 
     ".read": "auth != null && auth.uid == $uid", 
     ".write": "auth != null && auth.uid == $uid", 
     "items": { 
     "$item_id": { 
      "title": { 
      ".validate": "newData.isString() && newData.val().length > 0" 
      } 
     } 
     } 
    } 
    } 

    } 
} 
+0

從您連接到錯誤的圖像,以火力安全和規則有關。如果您需要更多信息,請將規則添加到問題中。 – adolfosrs

+0

@adolfosrs我認爲我自己,但我不確定從哪裏開始。我希望代碼自動將位置添加到該數據庫中,而不必手動執行任何想法?乾杯。我添加了規則 –

+0

@adolfosrs我添加了規則。 –

回答

0
{ 
    "rules": { 
     "users": { 
      "$uid": { 
       ".read": true, 
       ".write": true, 
       "items": { 
        "$item_id": { 
         "title": { 
          ".validate": "newData.isString() && newData.val().length > 0" 
         } 
        } 
       } 
      } 
     } 
    } 
} 
相關問題