Getting street name from Address/Location object i

2020-05-16 04:12发布

I'm trying to get the street name of my current location but I can't seem to get it.

I use this method to retrieve the Address:

public Address getAddressForLocation(Context context, Location location) throws IOException {

        if (location == null) {
            return null;
        }
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        int maxResults = 1;

        Geocoder gc = new Geocoder(context, Locale.getDefault());
        List<Address> addresses = gc.getFromLocation(latitude, longitude, maxResults);

        if (addresses.size() == 1) {
            return addresses.get(0);
        } else {
            return null;
        }
    }

And then I can do things like. address.getLocality() and address.getPostalCode()

But what I want is the street name. Like in "Potterstreet 12". When I print the AddressLine(0) and AddressLine(1) I only get the postalcode, city and country.

How can I retrieve the street name of the position i'm currently at?

6条回答
你好瞎i
2楼-- · 2020-05-16 04:15

Try something like this in your code

String cityName=null;              
Geocoder gcd = new Geocoder(getBaseContext(),Locale.getDefault());               
List<Address>  addresses;    
try {    
   addresses = gcd.getFromLocation(location.getLatitude(), location  
                   .getLongitude(), 1);    
   if (addresses.size() > 0) 
        StreetName=addresses.get(0).getThoroughfare();
        String s = longitude+"\n"+latitude +  
        "\n\nMy Currrent Street is: "+StreetName; 
         Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();

it works for me :-) Good luck ;-)

查看更多
不美不萌又怎样
3楼-- · 2020-05-16 04:27

I had a very similar problem but with the Country name, this is the function I ended up using:

function getCountry(results) {
    var geocoderAddressComponent,addressComponentTypes,address;
    for (var i in results) {
      geocoderAddressComponent = results[i].address_components;
      for (var j in geocoderAddressComponent) {
        address = geocoderAddressComponent[j];
        addressComponentTypes = geocoderAddressComponent[j].types;
        for (var k in addressComponentTypes) {
          if (addressComponentTypes[k] == 'country') {
            return address.long_name;
          }
        }
      }
    }
   return 'Unknown';
 }

You should be able to adapt this to get the street name out without much fuss.

Inspired by this answer

查看更多
▲ chillily
4楼-- · 2020-05-16 04:34

getFromLocation wasn't working for me either. There are a couple steps you can take.

1. First off go into gradle and make sure you are using the latest play services lib.

2. Don't over specify, the reason I got no results is because I had to much info in my address. When I removed the postal code I got results every time.

3. Try the online api: http://maps.google.com/maps/api/geocode/json?address=192%20McEwan%20Dr%20E,%20Caledon,%20ON&sensor=false Just replace the address in there with yours.

Good luck

查看更多
啃猪蹄的小仙女
5楼-- · 2020-05-16 04:35

Have you tried using getAddressLine ? See here for more info on this method

Something like this should do (untested):

for (int i = 0; i < addresses.getMaxAddressLineIndex(); i++) {
 Log.d("=Adress=",addresses.getAddressLine(i));
}
查看更多
Juvenile、少年°
6楼-- · 2020-05-16 04:35
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses = 
                gcd.getFromLocation(currentLatitude, currentLongitude,100);
if (addresses.size() > 0 && addresses != null) {
                StringBuilder result = new StringBuilder();
                myaddress.setText(addresses.get(0).getFeatureName()+"-"+addresses.get(0).getLocality()+"-"+addresses.get(0).getAdminArea()+"-"+addresses.get(0).getCountryName());
}
  • getfeaturename() return Streetname
  • getlocality() return city
  • getadminarea() return State

That's All..!

查看更多
倾城 Initia
7楼-- · 2020-05-16 04:39

If you have a complete address (city + street), in

address.getAddressLine(0)

you find the street name and number.

查看更多
登录 后发表回答