2

问题的标题几乎概括了我想问的内容。所以我有代码来获取当前城市的名称,LocationManager.NETWORK_PROVIDER它就像一个魅力。但是出现了一个问题:“如果手机无法获取城市名称会怎样LocationManager.NETWORK_PROVIDER?”。当然,我找到了那部手机:由于某些原因,联想手机无法使用LocationManager.NETWORK_PROVIDER. 所以我的问题是如何让我的应用程序首先查找坐标以获取城市名称,LocationManager.NETWORK_PROVIDER如果结果为 null 以获取坐标LocationManager.GPS_PROVIDER

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_where_am_i);   

    if (android.os.Build.VERSION.SDK_INT > 9) 
    {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }


    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);       

} 

我得到了这个城市的名字:

public void onLocationChanged(Location location)
{       
    curLatitude = location.getLatitude();
    curLongitude = location.getLongitude();     

    try{
         //Get the name of the city the user is currently in base on the current latitude and Longitude
            Geocoder gcd = new Geocoder(this, Locale.UK);
            List<Address> addresses = gcd.getFromLocation(curLatitude, curLongitude,1);

            if (addresses.size() > 0) 
            {
                StringBuilder cityName = new StringBuilder();                
                cityName.append(addresses.get(0).getLocality());
                CityName = cityName.toString();                
                //Check if the user is in allowed cities                   
            }            

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

}
4

2 回答 2

0

使用此参数:

  1. 获取前提();
  2. getSubAdminArea();
    3.getSubLocality();

如果您想详细了解,您应该更喜欢

于 2014-02-24T09:34:33.743 回答
0

当提供商请求位置更新时,不能保证返回位置更新。onLocationChanged() 方法仅在位置更改时调用。如果您使用 Network_provider 来获取位置更新,那么返回位置更新将需要很长时间。在这种情况下,您可以启动一个计时器,当它到期时,您可以从 GPS_provider 获取位置更新。否则只需简单地执行以下操作

myLocObj =  new MyLocationListener();
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

nw = manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if( nw ){
    Toast.makeText(BasicArchitectActivity.this, "fetching from NW", Toast.LENGTH_LONG).show();
    manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, myLocObj);
}

gps = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if( gps ){
    Toast.makeText(BasicArchitectActivity.this, "fetching from GPS", Toast.LENGTH_LONG).show();
    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocObj);
}


if(!gps && !nw){
    Toast.makeText( MainActivity.this, "Please enable GPS and Network positioning in your Settings ", Toast.LENGTH_LONG ).show();
}

这是位置监听器

class MyLocationListener implements LocationListener{

    double currentlat, currentlng;

    @Override
    public void onLocationChanged(Location location) {

        if(location != null){

            currentlat = location.getLatitude();
            currentlng = location.getLongitude();

            Toast.makeText(BasicArchitectActivity.this, "Current location \nlat= " +currentlat+",  lng= " + currentlng , Toast.LENGTH_SHORT).show();

    }

}
于 2014-02-24T09:50:10.993 回答