gpt4 book ai didi

java - 来自 Firebase 的纬度和经度坐标不正确,并且所有帖子的纬度和经度坐标均相同

转载 作者:行者123 更新时间:2023-12-01 18:50:00 24 4
gpt4 key购买 nike

用户从 Firebase 检索特定帖子的位置总是返回相同的错误位置,而且始终是相同的位置。我已经尝试了此代码的各种替代方法,试图解决它,以便用户为其事件创建的保存到 GoogleMap 的特定位置返回,但所有帖子都保持相同的位置,这是不正确的。

我在这里使用了一些其他有位置问题的帖子,但没有帮助我。

请告诉我我做错了什么。

Firebase

MapActivityUser.java

public class MapsActivityUser extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps_user);

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;

DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
if (snapshot.hasChild("location")) {
Post post = snapshot.getValue(Post.class);
double latitude = post.getLatitude();
double longitude = post.getLongitude();

LatLng location = new LatLng(latitude, longitude);
map.addMarker(new MarkerOptions().position(location).title("Event location"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 10));
}
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}
}

PostActivity.java

    package com.e.events.Model;

public class Post {

private String postid;
private String postimage;
private String description;
private String publisher;
private String text_event;
private String text_location;
private String text_date_time;
private Long timestamp;
private MyLocation location;

public Post(String description, String postId, String postImage, String publisher, Long timestamp,
String text_event, String text_location, String text_date_time, MyLocation location) {
this.postid = postid;
this.postimage = postimage;
this.description = description;
this.publisher = publisher;
this.text_event = text_event;
this.text_location = text_location;
this.text_date_time = text_date_time;
this.timestamp = timestamp;
this.location = location;
}

public class MyLocation {

private double latitude;
private double longitude;
}

public Post() {
}

public String getPostid() {
return postid;
}

public void setPostid(String postid) {
this.postid = postid;
}

public String getPostimage() {
return postimage;
}

public void setPostimage(String postimage) {
this.postimage = postimage;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getPublisher() {
return publisher;
}

public void setPublisher(String publisher) {
this.publisher = publisher;
}

public String getText_event() {
return text_event;
}

public void setText_event(String text_event) {
this.text_event = text_event;
}

public String getText_location() {
return text_location;
}

public void setText_location(String text_location) {
this.text_location = text_location;
}

public String getText_date_time() {
return text_date_time;
}

public void setText_date_time(String text_date_time) {
this.text_date_time = text_date_time;
}

public Long getTimestamp() {
return timestamp;
}

public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}

public MyLocation getLocation() {
return location;
}

public void setLocation(MyLocation location) {
this.location = location;
}
}

最佳答案

您没有正确设置模型。更新您的帖子,如下所示:

Post.java:

public class Post {

private String postid;
private String postimage;
private String description;
private String publisher;
private String text_event;
private String text_location;
private String text_date_time;
private Long timestamp;
private MyLocation location;

//getter-setter

}

MyLocation.java:

public class MyLocation {

private double latitude;
private double longitude;


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;
}
}

然后像这样解析

Post post = snapshot.getValue(Post.class);
double latitude = post.getLocation().getLatitude();
double longitude = post.getLocation().getLongitude();

关于java - 来自 Firebase 的纬度和经度坐标不正确,并且所有帖子的纬度和经度坐标均相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59756402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com