2013-05-07 40 views
0

我正在做一个应用程序,我必须显示当前位置的名称。我获得了经度和纬度,但无法获取位置的名称。为什么我不能检索当前位置的名称?

我试图做到这一点的代码是:

context = getApplicationContext(); 
Location location; 
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
double lon, lat; 
try 
{ 
    location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    lon = location.getLongitude(); 
    lat = location.getLatitude(); 

    Toast.makeText(context, "Lat: "+lat+"\nLon: "+lon, Toast.LENGTH_LONG).show(); 

    List<Address> mAddresses = null; 

    Geocoder gcd = new Geocoder(getApplicationContext(), Locale.getDefault()); 

    try 
    { 
     mAddresses = gcd.getFromLocation(location.getLatitude(), 
       location.getLongitude(), 1); 
     } 

    catch (IOException e) 
     { 
     e.printStackTrace(); 
    } 

    @SuppressWarnings("unchecked") 
    String cityName = (mAddresses != null) ? ((List<Address>) mAddresses).get(0) 
     .getLocality() : TimeZone.getDefault().getID(); 
    @SuppressWarnings("unchecked") 
    String countryName = (mAddresses != null) ? ((List<Address>) mAddresses).get(0) 
     .getCountryName() : Locale.getDefault().getDisplayCountry() 
     .toString(); 

    try 
    { 
     lm.clearTestProviderLocation(LocationManager.GPS_PROVIDER); 
     } 
    catch(Exception e) 
    {} 

    Toast.makeText(context, "Using GPS Provider", Toast.LENGTH_SHORT).show();  } 

    catch(Exception exp1){ 

    try 
    { 
     location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
      lon= location.getLongitude(); 
      lat = location.getLatitude(); 
      Toast.makeText(context, "Lat: "+lat+"\nLon: "+lon, Toast.LENGTH_LONG).show(); 

      List<Address> mAddresses = null; 

     Geocoder gcd = new Geocoder(getApplicationContext(), Locale.getDefault()); 

     try 
     { 
      mAddresses = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1); 
     } 

     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 

     @SuppressWarnings("unchecked") 
     String cityName = (mAddresses != null) ? ((List<Address>) mAddresses).get(0).getLocality() : TimeZone.getDefault().getID(); 

      @SuppressWarnings("unchecked") 
     String countryName = (mAddresses != null) ? ((List<Address>) mAddresses).get(0).getCountryName() : Locale.getDefault().getDisplayCountry().toString(); 

      try 
      { 
        lm.clearTestProviderLocation(LocationManager.NETWORK_PROVIDER); 
      } 
      catch(Exception e) 
      {} 

      Toast.makeText(context, "Using Network Provider", Toast.LENGTH_SHORT).show(); 

    } 
+0

你是否得到像.. Geocoder:服务不可用? – 2013-05-07 06:41:08

回答

0

这是更好地做到这一点在异步任务一样

private class LocationTask extends AsyncTask<String, Void, String> { 
     @Override 
     protected String doInBackground(String... parms) { 

      LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
      String provider = locationManager.getBestProvider(new Criteria(), 
        true); 

      Location locations = locationManager.getLastKnownLocation(provider); 
      List<String> providerList = locationManager.getAllProviders(); 
      if (null != locations && null != providerList 
        && providerList.size() > 0) { 
       double longitude = locations.getLongitude(); 
       double latitude = locations.getLatitude(); 
       Geocoder geocoder = new Geocoder(getApplicationContext(), 
         Locale.getDefault()); 
       try { 
        List<Address> listAddresses = geocoder.getFromLocation(
          latitude, longitude, 1); 
        if (null != listAddresses && listAddresses.size() > 0) { 
         String _Location = listAddresses.get(0).getAddressLine(
           1); 

         // 1000).show(); 
         return _Location; 

        } 
       } catch (IOException e) { 

              e.printStackTrace(); 

       } 

      } 
      return " "; 
     } 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 

     } 

     @Override 
     protected void onPostExecute(String locOutcome) { 

      //Use locOutcome here 
     } 
    } 

不要忘记添加权限

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
相关问题