当前位置: 代码迷 >> Android >> Android获取所在地城市名二
  详细解决方案

Android获取所在地城市名二

热度:85   发布时间:2016-05-01 20:46:11.0
Android获取所在地城市名2

?

Android获取所在地城市名

分类:?Android网络开发?61人阅读?评论(0)?收藏?举报
[java]?view plaincopy
  1. <span?style="font-size:24px;">public?class?LocationUtils?{????
  2. ????
  3. ????//public?static?String?cityName?=?"深圳";??//城市名????
  4. ????public?static?String?cityName?;??//城市名????
  5. ????????
  6. ????private?static?Geocoder?geocoder;???//此对象能通过经纬度来获取相应的城市等信息????
  7. ????????
  8. ????/**??
  9. ?????*?通过地理坐标获取城市名??其中CN分别是city和name的首字母缩写??
  10. [email protected]?
  11. ?????*/????
  12. ????public?static?void?getCNBylocation(Context?context){????
  13. ????????????
  14. ????????geocoder?=?new?Geocoder(context);????
  15. ????????//用于获取Location对象,以及其他????
  16. ????????LocationManager?locationManager;?????
  17. ????????String?serviceName?=?Context.LOCATION_SERVICE;????
  18. ????????//实例化一个LocationManager对象????
  19. ????????locationManager?=?(LocationManager)context.getSystemService(serviceName);????
  20. ????????//provider的类型????
  21. ????????String?provider?=?LocationManager.NETWORK_PROVIDER;????
  22. ????
  23. ????????Criteria?criteria?=?new?Criteria();????
  24. ????????criteria.setAccuracy(Criteria.ACCURACY_FINE);???//高精度????
  25. ????????criteria.setAltitudeRequired(false);????//不要求海拔????
  26. ????????criteria.setBearingRequired(false);?//不要求方位????
  27. ????????criteria.setCostAllowed(false);?//不允许有话费????
  28. ????????criteria.setPowerRequirement(Criteria.POWER_LOW);???//低功耗????
  29. ????????????
  30. ????????//通过最后一次的地理位置来获得Location对象????
  31. ????????Location?location?=?locationManager.getLastKnownLocation(provider);????
  32. ????????????
  33. ????????String?queryed_name?=?updateWithNewLocation(location);????
  34. ????????if((queryed_name?!=?null)?&&?(0?!=?queryed_name.length())){????
  35. ????????????????
  36. ????????????cityName?=?queryed_name;????
  37. ????????}????
  38. ????????????
  39. ????????/*??
  40. ?????????*?第二个参数表示更新的周期,单位为毫秒;第三个参数的含义表示最小距离间隔,单位是米??
  41. ?????????*?设定每30秒进行一次自动定位??
  42. ?????????*/????
  43. ????????locationManager.requestLocationUpdates(provider,?30000,?50,????
  44. ????????????????locationListener);????
  45. ????????//移除监听器,在只有一个widget的时候,这个还是适用的????
  46. ????????locationManager.removeUpdates(locationListener);????
  47. ????}????
  48. ????????
  49. ????/**??
  50. ?????*?方位改变时触发,进行调用??
  51. ?????*/????
  52. ????private?final?static?LocationListener?locationListener?=?new?LocationListener()?{????
  53. ????????String?tempCityName;????
  54. ????????public?void?onLocationChanged(Location?location)?{????
  55. ????????????????
  56. ????????????tempCityName?=?updateWithNewLocation(location);????
  57. ????????????if((tempCityName?!=?null)?&&?(tempCityName.length()?!=?0)){????
  58. ????????????????????
  59. ????????????????cityName?=?tempCityName;????
  60. ????????????}????
  61. ????????}????
  62. ????
  63. ????????public?void?onProviderDisabled(String?provider)?{????
  64. ????????????tempCityName?=?updateWithNewLocation(null);????
  65. ????????????if?((tempCityName?!=?null)?&&?(tempCityName.length()?!=?0))?{????
  66. ????
  67. ????????????????cityName?=?tempCityName;????
  68. ????????????}????
  69. ????????}????
  70. ????
  71. ????????public?void?onProviderEnabled(String?provider)?{????
  72. ????????}????
  73. ????
  74. ????????public?void?onStatusChanged(String?provider,?int?status,?Bundle?extras)?{????
  75. ????????}????
  76. ????};????
  77. ????
  78. ????/**??
  79. ?????*?更新location??
  80. [email protected]?
  81. [email protected]?
  82. ?????*/????
  83. ????private?static?String?updateWithNewLocation(Location?location)?{????
  84. ????????String?mcityName?=?"";????
  85. ????????double?lat?=?0;????
  86. ????????double?lng?=?0;????
  87. ????????List<Address>?addList?=?null;????
  88. ????????if?(location?!=?null)?{????
  89. ????????????lat?=?location.getLatitude();????
  90. ????????????lng?=?location.getLongitude();????
  91. ????????}?else?{????
  92. ????
  93. ????????????System.out.println("无法获取地理信息");????
  94. ????????}????
  95. ?????????????
  96. ????????try?{????
  97. ????????????????
  98. ????????????addList?=?geocoder.getFromLocation(lat,?lng,?1);????//解析经纬度????
  99. ????????????????
  100. ????????}?catch?(IOException?e)?{????
  101. ????????????//?TODO?Auto-generated?catch?block????
  102. ????????????e.printStackTrace();????
  103. ????????}????
  104. ????????if?(addList?!=?null?&&?addList.size()?>?0)?{????
  105. ????????????for?(int?i?=?0;?i?<?addList.size();?i++)?{????
  106. ????????????????Address?add?=?addList.get(i);????
  107. ????????????????mcityName?+=?add.getLocality();????
  108. ????????????}????
  109. ????????}????
  110. ????????if(mcityName.length()!=0){????
  111. ????????????????
  112. ????????????return?mcityName.substring(0,?(mcityName.length()-1));????
  113. ????????}?else?{????
  114. ????????????return?mcityName;????
  115. ????????}????
  116. ????}????
  117. ????
  118. ????/**??
  119. ?????*?通过经纬度获取地址信息的另一种方法??
  120. [email protected]?
  121. [email protected]?
  122. [email protected]?
  123. ?????*/????
  124. ????public?static?String?GetAddr(String?latitude,?String?longitude)?{??????
  125. ????????String?addr?=?"";??????
  126. ????????????
  127. ????????/*??
  128. ?????????*?也可以是http://maps.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s,不过解析出来的是英文地址??
  129. ?????????*?密钥可以随便写一个key=abc??
  130. ?????????*?output=csv,也可以是xml或json,不过使用csv返回的数据最简洁方便解析??????
  131. ?????????*/????
  132. ????????String?url?=?String.format(??????
  133. ????????????"http://ditu.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s",??????
  134. ????????????latitude,?longitude);??????
  135. ????????URL?myURL?=?null;??????
  136. ????????URLConnection?httpsConn?=?null;??????
  137. ????????try?{??????
  138. ????????????????
  139. ????????????myURL?=?new?URL(url);??????
  140. ????????}?catch?(MalformedURLException?e)?{??????
  141. ??????????e.printStackTrace();??????
  142. ??????????return?null;??????
  143. ????????}??????
  144. ????????????
  145. ????????try?{??????
  146. ????????????
  147. ????????????httpsConn?=?(URLConnection)?myURL.openConnection();??????
  148. ????????????????
  149. ????????????if?(httpsConn?!=?null)?{??????
  150. ????????????????InputStreamReader?insr?=?new?InputStreamReader(??????
  151. ????????????????????????httpsConn.getInputStream(),?"UTF-8");??????
  152. ????????????????BufferedReader?br?=?new?BufferedReader(insr);??????
  153. ????????????????String?data?=?null;??????
  154. ????????????????if?((data?=?br.readLine())?!=?null)?{??????
  155. ????????????????????String[]?retList?=?data.split(",");??????
  156. ????????????????????if?(retList.length?>?2?&&?("200".equals(retList[0])))?{??????
  157. ????????????????????????addr?=?retList[2];??????
  158. ????????????????????}?else?{??????
  159. ????????????????????????addr?=?"";??????
  160. ????????????????????}??????
  161. ????????????????}??????
  162. ????????????????insr.close();??????
  163. ????????????}??????
  164. ????????}?catch?(IOException?e)?{??????
  165. ????????????
  166. ????????????e.printStackTrace();??????
  167. ???????????return?null;??????
  168. ????????}??????
  169. ???????????return?addr;??????
  170. ????}????
  171. ????????
  172. }??</span>??

?

?

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

  相关解决方案