我正在应用程序中设置当前位置。我需要在哪里设置编码?
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
//Geo Fire for current location
String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference refAvailable = FirebaseDatabase.getInstance().getReference("driversAvailable");
DatabaseReference refWorking = FirebaseDatabase.getInstance().getReference("driversWorking");
GeoFire geoFireAvailable = new GeoFire(refAvailable);
GeoFire geoFireWorking = new GeoFire(refWorking);
switch (customerId){
case "":
geoFireWorking.removeLocation(userId);
geoFireAvailable.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
@Override
public void onComplete(String key, DatabaseError error) {
}
});
break;
default:
geoFireAvailable.removeLocation(userId);
geoFireWorking.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
@Override
public void onComplete(String key, DatabaseError error) {
}
});
break;
}
}
以下是我得到的错误:
java.lang.NoSuchMethodError:com.google.firebase.database.DatabaseReference.setValue 在com.firebase.geofire.GeoFire.removeLocation(GeoFire.java:215) 在com.firebase.geofire.GeoFire.removeLocation(GeoFire.java:192) 在com.example.webforest.quickaidlikeuber2nd.DriverMapActivity.onLocationChanged(DriverMapActivity.java:184)
答案 0 :(得分:0)
当您使用setLocation()
方法而不使用GeoFire.CompletionListener()
时,此错误似乎自动显现。如果您实施CompletionListener
,则应该可以解决该问题。
因此,替换
geoFire.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()));
使用
geoFire.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener(){ });
编辑2:
如果仍然遇到困难,您可能需要适应其他方法。
替换以下代码:
geoFireAvailable.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
@Override
public void onComplete(String key, DatabaseError error) {
}
});
使用以下代码:
GeoHash geoHash = new GeoHash(new GeoLocation(location.getLatitude(),location.getLongitude()));
Map<String, Object> updates = new HashMap<>();
updates.put("g", geoHash.getGeoHashString());
updates.put("l", Arrays.asList(location.getLatitude(),location.getLongitude()));
geoFireAvailable.setValue(updates,geoHash.getGeoHashString());
答案 1 :(得分:0)
Geofire进行了一些更改,现在要使removeLocation起作用,您必须添加一个侦听器,如下所示:
apply