我知道如何获取当前位置的经度和纬度并将其保存在Geofire中,但是如何获取特定地址的经度和纬度并将其保存。我测试下面的代码,但未保存任何内容。
我在做什么错?
String tx7=txt7.getText().toString();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("locals");
DatabaseReference update = rootRef.child("Users").child(uid);
GeoFire geoFire=new GeoFire(rootRef);
Geocoder geocoder = new Geocoder(AfterRegistrationActivity.this, Locale.getDefault());
String result = null;
List<Address> addressList= null;
try {
addressList = geocoder.getFromLocationName(tx7, 1);
Double latt=addressList.get(0).getLatitude();
Double lonn=addressList.get(0).getLongitude();
geoFire.setLocation(uid, new GeoLocation(latt, lonn), new GeoFire.CompletionListener() {
@Override
public void onComplete(String key, DatabaseError error) {
if (error != null) {
System.err.println("There was an error saving the location to GeoFire: " + error);
} else {
System.out.println("Location saved on server successfully!");
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
您可以使用地址解析器执行此操作。
private void geoLocate(){
Log.d(TAG, "geoLocate: geolocating");
String yourPlace = mSearchText.getText().toString();
Geocoder geocoder = new Geocoder(YourActivity.this);
List<Address> list = new ArrayList<>();
try{
list = geocoder.getFromLocationName(searchString, 1);
// passing 1 means you will get one result of that place you can pass any number
}catch (IOException e){
Log.e(TAG, "geoLocate: IOException: " + e.getMessage() );
}
if(list.size() > 0){
Address address = list.get(0);
Log.d(TAG, "geoLocate: found a location: " + address.toString());
// from address object you can get all info like address.latituted etc
//Toast.makeText(this, address.toString(), Toast.LENGTH_SHORT).show();
}
}