【问题标题】:How to get the latitude and longitude from address that users input in the text box如何从用户在文本框中输入的地址获取经纬度
【发布时间】:2020-01-24 03:48:39
【问题描述】:

我的代码需要一点帮助。我正在尝试获取他们从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!");
                                }
                            }
                        });

【问题讨论】:

    标签: android geolocation geocoding google-geocoder geofire


    【解决方案1】:
    【解决方案2】:

    你也可以使用简单的反向地理编码

     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/

    【讨论】:

      【解决方案3】:

      试试看

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-08
        • 2013-07-04
        • 1970-01-01
        • 2010-12-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多