2016-01-28 148 views
1

我已完成以下操作以獲取當前位置。但有時getAddressLine()方法給我空值。這種情況非常罕見。但我想永久解決它。任何人都可以給我一個解決方案!Android中的當前位置

LocationManager locationManager; 
    String provider; 
    Location location; 

    double lat; 
    double lng; 

    String address; 
    String city; 

    // How many Geocoder should return our GPSTracker 
    int geocoderMaxResults = 1; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     view = inflater.inflate(R.layout.fragment_home, container, false); 

     getActivity().getWindow().getDecorView().setSystemUiVisibility(
       View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
         | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
         | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 
         | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); 


     toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar); 
     statusBar = (FrameLayout) getActivity().findViewById(R.id.statusBar); 
     ((MainActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(true); 

locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 
     // Define the criteria how to select the locatioin provider -> use 
     // default 
     Criteria criteria = new Criteria(); 
     provider = locationManager.getBestProvider(criteria, false); 
     location = locationManager.getLastKnownLocation(provider); 

     // Initialize the location fields 
     if (location != null) { 
      System.out.println("Provider " + provider + " has been selected."); 
      onLocationChanged(location); 

      address = getAddressLine(getActivity()); 
      city = getLocality(getActivity()); 


      ((MainActivity) getActivity()).getSupportActionBar().setTitle(address + ", " + city); 

     } else { 
      Toast.makeText(getActivity(), "Impossible to connect to Geocoder !!", 
        Toast.LENGTH_LONG).show(); 
     } 
} 


    @Override 
    public void onLocationChanged(Location location) { 

     lat = (double) location.getLatitude(); 
     lng = (double) location.getLongitude(); 
    } 

    @Override 
    public void onStatusChanged(String s, int i, Bundle bundle) { 
     //To change body of implemented methods use File | Settings | File Templates. 
    } 

    @Override 
    public void onProviderEnabled(String s) { 
     //To change body of implemented methods use File | Settings | File Templates. 
    } 

    @Override 
    public void onProviderDisabled(String s) { 
     //To change body of implemented methods use File | Settings | File Templates. 
    } 

    /** 
    * Get list of address by latitude and longitude 
    * @return null or List<Address> 
    */ 
    public List<Address> getGeocoderAddress(Context context) { 
     if (location != null) { 

      Geocoder geocoder = new Geocoder(context, Locale.ENGLISH); 

      try { 
       /** 
       * Geocoder.getFromLocation - Returns an array of Addresses 
       * that are known to describe the area immediately surrounding the given latitude and longitude. 
       */ 
       List<Address> addresses = geocoder.getFromLocation(lat, lng, this.geocoderMaxResults); 

       return addresses; 
      } catch (IOException e) { 
       e.printStackTrace(); 
      // Log.e(TAG, "Impossible to connect to Geocoder", e); 
      } 
     } 

     return null; 
    } 

    /** 
    * Try to get AddressLine 
    * @return null or addressLine 
    */ 
    public String getAddressLine(Context context) { 
     List<Address> addresses = getGeocoderAddress(context); 

     if (addresses != null && addresses.size() > 0) { 
      Address address = addresses.get(0); 
      String addressLine = address.getAddressLine(geocoderMaxResults); 

      return addressLine; 
     } else { 
      return null; 
     } 
    } 

    public String getLocality(Context context) { 
     List<Address> addresses = getGeocoderAddress(context); 

     if (addresses != null && addresses.size() > 0) { 
      Address address = addresses.get(0); 
      String locality = address.getLocality(); 

      return locality; 
     } 
     else { 
      return null; 
     } 
    } 
+0

你必須處理它。並非每個地點都有地址。 – Henry

+0

但我在這裏有一個問題。爲什麼地址大部分時間都是使用相同的代碼?有時它不會。我已經測試過它一次又一次地在同一個位置。 –

回答

0

每個地點可能沒有地址。如果您確定自己的地址有地址,但您的Geocoder無法提供,請重新啓動手機並重試(我發現沒有真正解決此解決方案的問題,雖然它發生的情況非常罕見)。在這些特殊情況下,您必須提供'else'塊來執行一些操作。

0

使用下面的代碼獲取用戶的當前位置。這是Google最新的API(日期:2016年1月28日)。

mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 

Log.e(LOG, mLastLocation.toString()); 

float latitude = (float) mLastLocation.getLatitude(); 
float longitude = (float) mLastLocation.getLongitude(); 

Geocoder geocoder = new Geocoder(this); 

try { 
    List<Address> address = geocoder.getFromLocation(latitude, longitude, 1); 
    Address address1 = address.get(0); 
    String loc = address1.getAddressLine(0) + address1.getAddressLine(1); 
    editLocationView.setText(loc); 
    Log.d(LOG, address.toString()); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
+0

它有什麼作用? –

+0

獲取用戶的當前位置。它的谷歌最新api。 –