【问题标题】:Andorid How to get the name of the current city using coordinatesAndroid 如何使用坐标获取当前城市的名称
【发布时间】:2014-02-24 09:25:37
【问题描述】:

问题的标题几乎概括了我想问的问题。所以我有使用LocationManager.NETWORK_PROVIDER 获取当前城市名称的代码,它就像一个魅力。然而,出现了一个问题:“如果手机无法使用LocationManager.NETWORK_PROVIDER 获取城市名称会怎样?”。当然,我找到了那部手机:由于某些原因,联想手机无法使用LocationManager.NETWORK_PROVIDER 获取坐标。所以我的问题是如何让我的应用程序首先查找坐标以使用LocationManager.NETWORK_PROVIDER 获取城市名称,如果结果为空,则使用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();
         }

}

【问题讨论】:

标签: android localization gps provider city


【解决方案1】:

使用这个参数:

  1. getPremises();
  2. getSubAdminArea();
    3.getSubLocality();

If you want in detail you should prefer

【讨论】:

  • 这很好,但这将如何帮助我在提供商之间切换?
  • 这是你自己的编码方式,我认为你应该使用方法并为两个提供者调用你的 onLocationChanged()。
【解决方案2】:

当提供商请求位置更新时,不能保证返回位置更新。 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();

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    相关资源
    最近更新 更多