Get Map address or Location Address in Android

前端 未结 3 1856
我寻月下人不归
我寻月下人不归 2020-12-16 04:12

I am writing an app that requires to get the current map location. My Map file works fine by it self, but I need to get the address (see addressString below at

相关标签:
3条回答
  • 2020-12-16 04:47

    Either pass the address to your second Activity in the Intent, or if you need to do lookups from your second Activity, just call Geocoder from there.

    If you're somehow instantiating the above class (GetMapActivity) from your second Activity and calling the getAddress method, that won't work.

    0 讨论(0)
  • 2020-12-16 04:48

    The following might work [move the code from onCreate and put it in a method]

    public String getAddressString() {
            if (addressString.equals("default") {
              LocationManager lm = (LocationManager)act.getSystemService(Context.LOCATION_SERVICE);
              Criteria crit = new Criteria();
              crit.setAccuracy(Criteria.ACCURACY_FINE);
              String provider = lm.getBestProvider(crit, true);
              updateWithNewLocation(lm.getLastKnownLocation(provider));              
            }
            return addressString;
        }
    
    0 讨论(0)
  • 2020-12-16 04:54

    I have used the following code in my app and its working absolutely fine for me. Try this. Hope this will help you.

        //
        //  Write the location name.
        //
    
        try {
    
            Geocoder geo = new Geocoder(youractivityclassname.this.getApplicationContext(), Locale.getDefault());
            List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
            if (addresses.isEmpty()) {
                yourtextfieldname.setText("Waiting for Location");
            }
            else {
                if (addresses.size() > 0) {
                    yourtextfieldname.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName());
                    //Toast.makeText(getApplicationContext(), "Address:- " + addresses.get(0).getFeatureName() + addresses.get(0).getAdminArea() + addresses.get(0).getLocality(), Toast.LENGTH_LONG).show();
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace(); // getFromLocation() may sometimes fail
        }
    
    0 讨论(0)
提交回复
热议问题