Android获取所在地城市名

  1. http://blog.csdn.net/litton_van/article/details/7101422
  2. //public static String cityName "深圳";  //城市名    
  3.     public static String cityName  //城市名    
  4.         
  5.     private static Geocoder geocoder;   //此对象能通过经纬度来获取相应的城市等信息    
  6.         
  7.         
  8.     public static void getCNBylocation(Context context){    
  9.             
  10.         geocoder new Geocoder(context);    
  11.         //用于获取Location对象,以及其他    
  12.         LocationManager locationManager;     
  13.         String serviceName Context.LOCATION_SERVICE;    
  14.         //实例化一个LocationManager对象    
  15.         locationManager (LocationManager)context.getSystemService(serviceName);    
  16.         //provider的类型    
  17.         String provider LocationManager.NETWORK_PROVIDER;    
  18.     
  19.         Criteria criteria new Criteria();    
  20.         criteria.setAccuracy(Criteria.ACCURACY_FINE);   //高精度    
  21.         criteria.setAltitudeRequired(false);    //不要求海拔    
  22.         criteria.setBearingRequired(false); //不要求方位    
  23.         criteria.setCostAllowed(false); //不允许有话费    
  24.         criteria.setPowerRequirement(Criteria.POWER_LOW);   //低功耗    
  25.             
  26.         //通过最后一次的地理位置来获得Location对象    
  27.         Location location locationManager.getLastKnownLocation(provider);    
  28.             
  29.         String queryed_name updateWithNewLocation(location);    
  30.         if((queryed_name != null&& (0 != queryed_name.length())){    
  31.                 
  32.             cityName queryed_name;    
  33.            
  34.             
  35.             
  36.         locationManager.requestLocationUpdates(provider, 3000050   
  37.                 locationListener);    
  38.         //移除监听器,在只有一个widget的时候,这个还是适用的    
  39.         locationManager.removeUpdates(locationListener);    
  40.        
  41.         
  42.         
  43.     private final static LocationListener locationListener new LocationListener()    
  44.         String tempCityName;    
  45.         public void onLocationChanged(Location location)    
  46.                 
  47.             tempCityName updateWithNewLocation(location);    
  48.             if((tempCityName != null&& (tempCityName.length() != 0)){    
  49.                     
  50.                 cityName tempCityName;    
  51.                
  52.            
  53.     
  54.         public void onProviderDisabled(String provider)    
  55.             tempCityName updateWithNewLocation(null);    
  56.             if ((tempCityName != null&& (tempCityName.length() != 0))    
  57.     
  58.                 cityName tempCityName;    
  59.                
  60.            
  61.     
  62.         public void onProviderEnabled(String provider)    
  63.            
  64.     
  65.         public void onStatusChanged(String provider, int status, Bundle extras)    
  66.            
  67.     };    
  68.     
  69.         
  70.     private static String updateWithNewLocation(Location location)    
  71.         String mcityName ""   
  72.         double lat 0   
  73.         double lng 0   
  74.         List<Address> addList null   
  75.         if (location != null   
  76.             lat location.getLatitude();    
  77.             lng location.getLongitude();    
  78.         else    
  79.     
  80.             System.out.println("无法获取地理信息");    
  81.            
  82.              
  83.         try    
  84.                 
  85.             addList geocoder.getFromLocation(lat, lng, 1);    //解析经纬度    
  86.                 
  87.         catch (IOException e)    
  88.             // TODO Auto-generated catch block    
  89.             e.printStackTrace();    
  90.            
  91.         if (addList != null && addList.size() 0   
  92.             for (int 0addList.size(); i++)    
  93.                 Address add addList.get(i);    
  94.                 mcityName += add.getLocality();    
  95.                
  96.            
  97.         if(mcityName.length()!=0){    
  98.                 
  99.             return mcityName.substring(0(mcityName.length()-1));    
  100.         else    
  101.             return mcityName;    
  102.            
  103.        
  104.     


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值