Java 来自Firebase的纬度和经度坐标不正确,所有立柱的纬度和经度坐标相同
用户从Firebase检索特定Java 来自Firebase的纬度和经度坐标不正确,所有立柱的纬度和经度坐标相同,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,用户从Firebase检索特定post的位置总是作为相同的错误位置返回,总是相同的位置。我已经尝试了各种方法来解决这个问题,这样用户为他们的事件创建的特定位置就会保存到GoogleMap中,但是所有帖子都会得到相同的位置,这是不正确的 我一直在使用其他一些关于位置问题的帖子,但都没有帮到我 请告诉我我做错了什么 MapActivityUser.java public class MapsActivityUser extends FragmentActivity implements OnMap
post
的位置总是作为相同的错误位置返回,总是相同的位置。我已经尝试了各种方法来解决这个问题,这样用户为他们的事件创建的特定位置就会保存到GoogleMap
中,但是所有帖子都会得到相同的位置,这是不正确的
我一直在使用其他一些关于位置问题的帖子,但都没有帮到我
请告诉我我做错了什么
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) {
}
});
}
}
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;
}
}
PostActivity.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) {
}
});
}
}
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
}
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;
}
}
MyLocation.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
}
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();
从何处将位置保存到firebase?您是否保存了正确的位置?检查您的数据库,从my
MapsActivityPublisher.java
中手动添加Post
bean类。@Md.Asaduzzaman。所以坐标被保存到Firebase,然后其他想要知道事件位置的用户单击TextView,这会将他带到在事件位置设置标记的地图。我贴了一张Firebase的照片。我能做些什么来修复它?字面上没有idea@Md.Asaduzzaman添加了PostActivity.java
你是说36.62和119.30不来了?我不明白这部分私人MyLocation位置代码>什么是MyLocation
是变量类型吗?它不应该是双精度的吗?检查一下你的数据库,那里的纬度和经度不是Post
的直接子对象。它是一个名为location
的对象。因此,您必须创建一个对象来保存该数据检查我的答案,我创建了一个名为MyLocation的类,它保存纬度
和经度
有一个错误。在此代码中,双纬度=post.getLocation().getLatitude()
getLatitude()的getLatitude()代码>返回红色。对于LongitudeDid,您在代码中实现了MyLocation
和它的属性的getter setter,也是这样吗?