我是android的新手,我已经按照本教程进行了实时资产跟踪。 Tracking Url
现在,我想从Firebase以及应用程序中删除标记,而无需在浏览器中打开Firebase。我该怎么做?有可能吗?
以下代码用于将位置更新发送到Firebase服务器的应用。
TrackerService.java
public class TrackerService extends Service {
private static final String TAG = TrackerService.class.getSimpleName();
@Override
public IBinder onBind(Intent intent) {return null;}
@Override
public void onCreate() {
super.onCreate();
loginToFirebase();
}
private void loginToFirebase() {
// Authenticate with Firebase, and request location updates
String email = getString(R.string.firebase_email);
String password = getString(R.string.firebase_password);
FirebaseAuth.getInstance().signInWithEmailAndPassword(
email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>(){
@Override
public void onComplete(Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "firebase auth success");
requestLocationUpdates();
} else {
Log.d(TAG, "firebase auth failed");
}
}
});
}
private void requestLocationUpdates() {
LocationRequest request = new LocationRequest();
request.setInterval(10000);
request.setFastestInterval(5000);
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
FusedLocationProviderClient client = LocationServices.getFusedLocationProviderClient(this);
final String path = getString(R.string.firebase_path) + "/" + getString(R.string.transport_id);
int permission = ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION);
if (permission == PackageManager.PERMISSION_GRANTED) {
// Request location updates and when an update is
// received, store the location in Firebase
client.requestLocationUpdates(request, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(path);
Location location = locationResult.getLastLocation();
if (location != null) {
Log.d(TAG, "location update " + location);
ref.setValue(location);
}
}
}, null);
}
}
以下代码来自第二个在地图上显示标记的应用。
public class DisplayActivity extends FragmentActivity implements OnMapReadyCallback {
private static final String TAG = DisplayActivity.class.getSimpleName();
private HashMap<String, Marker> mMarkers = new HashMap<>();
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
// Authenticate with Firebase when the Google map is loaded
mMap = googleMap;
mMap.setMaxZoomPreference(16);
loginToFirebase();
}
private void loginToFirebase() {
String email = getString(R.string.firebase_email);
String password = getString(R.string.firebase_password);
// Authenticate with Firebase and subscribe to updates
FirebaseAuth.getInstance().signInWithEmailAndPassword(
email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(Task<AuthResult> task) {
if (task.isSuccessful()) {
subscribeToUpdates();
Log.d(TAG, "firebase auth success");
} else {
Log.d(TAG, "firebase auth failed");
}
}
});
}
private void subscribeToUpdates() {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(getString(R.string.firebase_path));
ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
setMarker(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
setMarker(dataSnapshot);
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onCancelled(DatabaseError error) {
Log.d(TAG, "Failed to read value.", error.toException());
}
});
}
private void setMarker(DataSnapshot dataSnapshot) {
// When a location update is received, put or update
// its value in mMarkers, which contains all the markers
// for locations received, so that we can build the
// boundaries required to show them all on the map at once
String key = dataSnapshot.getKey();
HashMap<String, Object> value = (HashMap<String, Object>) dataSnapshot.getValue();
double lat = Double.parseDouble(value.get("latitude").toString());
double lng = Double.parseDouble(value.get("longitude").toString());
LatLng location = new LatLng(lat, lng);
if (!mMarkers.containsKey(key)) {
mMarkers.put(key, mMap.addMarker(new MarkerOptions().title(key).position(location)));
} else {
mMarkers.get(key).setPosition(location);
}
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker marker : mMarkers.values()) {
builder.include(marker.getPosition());
}
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 300));
}
答案 0 :(得分:1)
在您的应用中,您可以创建一种方法来选择要删除的标记,方法是使用按钮或直接从地图中选择标记(我建议使用第二种方法),然后从mMarkers列表中删除标记,在映射并从firebase中删除“文档”。
对于“标记”部分,您可以遵循地图和标记的官方文档中的example。
对于Firebase部分,请检查how to remove data from RealTime Database,但基本上您会获得数据DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(path)
的数据库参考,并从其中调用removeValue ref.removeValue()