我需要一点代码方面的帮助。我正在尝试从edittext中获取用户输入的地址,并获取该地址的纬度和经度,然后将其保存在geofire中。我可以获取当前用户的经度和纬度,并将其保存到geofire中,但如果用户想使用不同的地址,而我获取了该特定地址的经度和纬度,并将其保存在geofire中,该怎么办?我希望你们不要对我的问题感到困惑。下面是我的代码,提前感谢。
FusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(final Location location) {
if (location != null) {
Double latittude = location.getLatitude();
Double longitude = location.getLongitude();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child("address");
DatabaseReference update = reference.child("Users").child(uid);
GeoFire geoFire=new GeoFire( reference);
geoFire.setLocation(uid, new GeoLocation(latittude, longitude), new GeoFire.CompletionListener() {
@Override
public void onComplete(String key, DatabaseError error) {
if (error != null) {
System.err.println("There was an error saving your location to GeoFire: " + error);
} else {
System.out.println("Location saved on the server successfully!");
}
}
});发布于 2020-01-24 12:08:02
你必须使用谷歌的places API才能实现
以下链接可能会对您有所帮助
https://medium.com/skillhive/android-google-places-autocomplete-feature-bb3064308f05
发布于 2020-01-24 12:55:07
此外,还可以使用简单的反向地理编码
Location location = appLocationService
.getLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LocationAddress locationAddress = new LocationAddress();
locationAddress.getAddressFromLocation(latitude, longitude,
getApplicationContext(), new GeocoderHandler());
} else {
showSettingsAlert();
}有关更多信息,请查看以下内容:https://javapapers.com/android/android-get-address-with-street-name-city-for-location-with-geocoding/
发布于 2020-01-24 13:03:56
试试看
Geocoder coder = new Geocoder(this);
List<Address> address;
double lat, lang;
try {
address = coder.getFromLocationName(strAddress,5);
if (address==null) {
return null;
}
Address location=address.get(0);
lat = location.getLatitude();
lang = location.getLongitude();
}https://stackoverflow.com/questions/59890142
复制相似问题