Getting street name from Address/Location object in Android

前端 未结 6 757
滥情空心
滥情空心 2021-01-31 11:34

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 getAddre         


        
相关标签:
6条回答
  • 2021-01-31 11:44

    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

    0 讨论(0)
  • 2021-01-31 11:50
    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..!

    0 讨论(0)
  • 2021-01-31 11:51

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

    address.getAddressLine(0)
    

    you find the street name and number.

    0 讨论(0)
  • 2021-01-31 11:56

    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

    0 讨论(0)
  • 2021-01-31 11:57

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

    0 讨论(0)
  • 2021-01-31 12:05

    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));
    }
    
    0 讨论(0)
提交回复
热议问题