【问题标题】:How to save coordinates to Firebase [closed]如何将坐标保存到 Firebase [关闭]
【发布时间】:2020-05-24 10:56:34
【问题描述】:

我正在尝试在 Firebase 中保存位置。我找不到有关如何在 Firebase 中保存位置的任何信息。我用 Java 在 Android Studio 中开发我的。我在布局中放置了一个按钮,当我单击此按钮时,它应该将坐标发送到 Firebase。但我对将数据保存到 Firebase 一无所知。

如果你能帮助我,我会很高兴!

【问题讨论】:

  • 请发表您到目前为止尝试过的内容
  • 当我点击应用内按钮时,它应该将坐标发送到 firebase
  • 更多信息,例如您正在为哪个平台(即 Web、iOS、Android 等)编程,以及您的“位置”是如何表示的。
  • 我正在 Android Studio 中开发应用程序。

标签: javascript java android firebase android-studio


【解决方案1】:

好的,你知道你的问题太笼统了,这段代码无论如何都不完整;与响应一样大。这足以让你继续前进,你需要做更多的研究。我希望这有帮助。不包括允许用户从地图或任何来源保存位置的 UI。

同样不包括身份验证做这个 [Oauth] 这是一个完全独立的问题。如果我是你,我会从一个添加和显示数据的教程开始,让它工作,然后用位置替换数据

tutorial

有很多步骤,您可能想从教程开始,那里有很多好的步骤。

你的数据库参考:

 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;


}

}

【讨论】:

  • 非常感谢!现在我可以做我的项目了!还有一个问题 db ref 是什么?
  • 更新了,祝你好运
  • 什么是 private void storeLocation(com.wfs.android.walkingjinni.Location location)?
  • 这只是您在活动中可能需要执行的操作以在 Firebase 中存储新位置的示例。然后观察者在添加新位置时更新。如果您有位置的回收站视图...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-18
  • 2021-03-14
  • 1970-01-01
  • 2021-07-12
相关资源
最近更新 更多