首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从用户在文本框中输入的地址中获取经纬度

如何从用户在文本框中输入的地址中获取经纬度
EN

Stack Overflow用户
提问于 2020-01-24 11:48:40
回答 3查看 264关注 0票数 0

我需要一点代码方面的帮助。我正在尝试从edittext中获取用户输入的地址,并获取该地址的纬度和经度,然后将其保存在geofire中。我可以获取当前用户的经度和纬度,并将其保存到geofire中,但如果用户想使用不同的地址,而我获取了该特定地址的经度和纬度,并将其保存在geofire中,该怎么办?我希望你们不要对我的问题感到困惑。下面是我的代码,提前感谢。

代码语言:javascript
运行
复制
 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!");
                                }
                            }
                        });
EN

回答 3

Stack Overflow用户

发布于 2020-01-24 12:55:07

此外,还可以使用简单的反向地理编码

代码语言:javascript
运行
复制
 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/

票数 0
EN

Stack Overflow用户

发布于 2020-01-24 13:03:56

试试看

代码语言:javascript
运行
复制
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();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59890142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档