我正在尝试在 Firebase 中保存位置。我找不到有关如何在 Firebase 中保存位置的任何信息。我在 Android Studio 中用 Java 开发。我在布局中放置了一个按钮,当我点击这个按钮时,它应该将坐标发送到 Firebase。但我对将数据保存到 Firebase 一无所知。
请如果你能帮助我我会很高兴!
好的,这样你就知道你的问题太笼统了,这段代码无论如何都不完整;与响应一样大。这足以让你前进,你需要做更多的研究。我希望这有帮助。不包括允许用户从地图或任何来源保存位置的 UI。
也不包括身份验证执行此操作 [Oauth],这是一个完全独立的问题。如果我是你,我会从一个添加和显示数据的教程开始,让它工作,然后用位置替换数据
有很多步骤,你可能想从教程开始,那里有很多好的。
你的数据库参考:
mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
final DatabaseReference locationsRef = mFirebaseDatabaseReference
.child(USERS_CHILD.concat("/" + mFirebaseAuth.getUid()).concat("/locations"));
FirebaseRecyclerOptions<Location> options =
new FirebaseRecyclerOptions.Builder<Location>()
.setQuery(locationsRef
.orderByChild("name"), parser)
.build();
mFirebaseAdapter = new FirebaseRecyclerAdapter<Location,
PlacesFragment.LocationViewHolder>(options) {
@Override
public PlacesFragment.LocationViewHolder onCreateViewHolder(ViewGroup viewGroup,
final int position) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
PlacesFragment.LocationViewHolder holder = new
PlacesFragment.LocationViewHolder(inflater.inflate(R.layout.location_item,
viewGroup, false));
Log.d(TAG, "viewHolder created");
return holder;
}
@Override
protected void onBindViewHolder(final PlacesFragment.LocationViewHolder viewHolder,
int position, Location location) {
if (location.getName() != null) {
viewHolder.locationNameTextView.setText(location.getName());
viewHolder.locationNameTextView.setVisibility(TextView.VISIBLE);
viewHolder.locationAddressTextView.setText(location.getAddress());
viewHolder.locationLatTextView.setText(String.valueOf(location.getLatitude()));
viewHolder.locationLongTextView.setText(String.valueOf(location.getLongitude()));
}
}
@Override
public int getItemCount() {
int count = super.getItemCount();
return count;
}
@Override
public void onDataChanged() {
Log.d(TAG, "OnDataChanged");
mLandmarks.clear();
ArrayList<Location> arr = new ArrayList<Location>();
for (int i = 0; i < mFirebaseAdapter.getSnapshots().toArray().length; i++) {
Location loc = mFirebaseAdapter.getItem(i);
arr.add(loc);
mLandmarks.put(loc.getName(), new LatLng(loc.getLatitude(),
loc.getLongitude()));
}
if (mService != null) {
mService.setLocationsData(arr);
Log.d(TAG, "onDataChanged: updating locations");
} else {
mService_not_updated = true;
Log.d(TAG, "onDataChanged: not updating locations");
}
// Get the geofences used.
populateGeofenceList();
super.onDataChanged();
}
};
mFirebaseAdapter.startListening();
数据库规则,因此每个用户都可以拥有自己的位置列表:
// These rules grant access to a node matching the authenticated
// user's ID from the Firebase auth token
{
"rules": {
"users": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
}
}
}
这是您存储位置的方式:
private void storeLocation(com.wfs.android.walkingjinni.Location location) {
com.wfs.android.walkingjinni.Location loc = new com.wfs.android.walkingjinni.Location(
mFirebaseUserId,
location.getName(),
location.getAddress(),
location.getLatitude(), location.getLongitude()
,location.getPlaceId());
mFirebaseDatabaseReference
.child(PlacesFragment.USERS_CHILD).child(mFirebaseUserId)
.child(PlacesFragment.LOCATIONS_CHILD).push().setValue(loc);
Log.d(TAG, location.getAddress() + " stored from map");
}
这可能是一个位置:
@Keep
public class Location implements Parcelable {
public static final Parcelable.Creator CREATOR = new Parcelable.Creator<Location>() {
public Location createFromParcel(Parcel in) {
return new Location(in);
}
public Location[] newArray(int size) {
return new Location[size];
}
};
private String id;
private String user_id;
private String name;
private String address;
private double latitude, longitude;
private String place_id = "";
private Location(Parcel in) {
id = in.readString();
user_id = in.readString();
name = in.readString();
address = in.readString();
latitude = in.readDouble();
longitude = in.readDouble();
place_id = in.readString();
}
public Location() {
}
/// used for onCreate for the home location, nothing else
public Location(String name, String address,
double latitude, double longitude, String placeId) {
this.name = name;
this.address = address;
this.latitude = latitude;
this.longitude = longitude;
this.place_id = placeId;
}
public Location(
String userId,
String name,
String address,
double latitude, double longitude, String placeId) {
this.user_id = userId;
this.name = name;
this.address = address;
this.latitude = latitude;
this.longitude = longitude;
this.place_id = placeId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUserId() {
return user_id;
}
public void setUserId(String userId) {
this.user_id = userId;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public String getPlaceId() {
return place_id;
}
public void setPlaceId(String placeId) {
this.place_id = placeId;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(id);
parcel.writeString(user_id);
parcel.writeString(name);
parcel.writeString(address);
parcel.writeDouble(latitude);
parcel.writeDouble(longitude);
parcel.writeString(place_id);
}
@Override
public String toString() {
return "Location{" +
" id='" + id + '\'' +
", user_id='" + id + '\'' +
", name='" + name + '\'' +
", address='" + address + '\'' +
", latitude='" + latitude + '\'' +
", longitude='" + longitude + '\'' +
", place_id='" + place_id + '\'' +
'}';
}
public android.location.Location getLocation() {
android.location.Location loc = new android.location.Location("");
loc.setLatitude(this.getLatitude());
loc.setLongitude(this.getLongitude());
return loc;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句