ANDROID 获得地理位置

使用android的定位服务,需要在manifest文件里增加相应的权限,这里不赘述。
下面是两个类的代码,第一个是activity,完成的功能是获取经纬度,然后提供查询对应的地址的按钮。
第二个是工具类,从经纬度获取到地址。

Java代码
  1. /*
  2. *@(#)LocationActivity.javaProject:androidDevices
  3. *Date:2012-6-5
  4. *
  5. *Copyright(c)2011CFuture09,InstituteofSoftware,
  6. *GuangdongOceanUniversity,Zhanjiang,GuangDong,China.
  7. *Allrightsreserved.
  8. *
  9. *LicensedundertheApacheLicense,Version2.0(the"License");
  10. *youmaynotusethisfileexceptincompliancewiththeLicense.
  11. *YoumayobtainacopyoftheLicenseat
  12. *
  13. *http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
  16. *distributedundertheLicenseisdistributedonan"ASIS"BASIS,
  17. *WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
  18. *SeetheLicenseforthespecificlanguagegoverningpermissionsand
  19. *limitationsundertheLicense.
  20. */
  21. packagecom.sinaapp.msdxblog.android.location;
  22. importandroid.app.Activity;
  23. importandroid.content.Context;
  24. importandroid.location.Location;
  25. importandroid.location.LocationListener;
  26. importandroid.location.LocationManager;
  27. importandroid.os.Bundle;
  28. importandroid.view.View;
  29. importandroid.widget.Toast;
  30. importcom.sinaapp.msdxblog.android.R;
  31. /**
  32. *@authorGeek_Soledad(66704238@51uc.com)
  33. */
  34. publicclassLocationActivityextendsActivity{
  35. privatedoublemLongitude;
  36. privatedoublemLatitude;
  37. @Override
  38. protectedvoidonCreate(BundlesavedInstanceState){
  39. super.onCreate(savedInstanceState);
  40. setContentView(R.layout.location);
  41. }
  42. publicvoidonButtonClick(Viewv){
  43. switch(v.getId()){
  44. caseR.id.get_location:
  45. getLocation();
  46. break;
  47. caseR.id.query:
  48. Toast.makeText(this,
  49. LocationUtil.getAddress(mLatitude,mLongitude),1000)
  50. .show();
  51. break;
  52. default:
  53. break;
  54. }
  55. }
  56. privatevoidgetLocation(){
  57. LocationManagerlocationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
  58. locationManager.requestLocationUpdates(
  59. LocationManager.NETWORK_PROVIDER,0,0,newLocationListener(){
  60. @Override
  61. publicvoidonStatusChanged(Stringprovider,intstatus,
  62. Bundleextras){
  63. }
  64. @Override
  65. publicvoidonProviderEnabled(Stringprovider){
  66. Toast.makeText(LocationActivity.this,"enable",1000)
  67. .show();
  68. System.out.println("enable");
  69. }
  70. @Override
  71. publicvoidonProviderDisabled(Stringprovider){
  72. Toast.makeText(LocationActivity.this,"disable",1000)
  73. .show();
  74. System.out.println("disable");
  75. }
  76. @Override
  77. publicvoidonLocationChanged(Locationlocation){
  78. mLatitude=location.getLatitude();
  79. mLongitude=location.getLongitude();
  80. System.out.println(location.getLatitude());
  81. System.out.println(location.getLongitude());
  82. Toast.makeText(LocationActivity.this,
  83. mLatitude+":"+mLongitude,1000).show();
  84. }
  85. });
  86. }
  87. }


Java代码
  1. /*
  2. *@(#)LocationUtil.javaProject:androidDevices
  3. *Date:2012-6-5
  4. *
  5. *Copyright(c)2011CFuture09,InstituteofSoftware,
  6. *GuangdongOceanUniversity,Zhanjiang,GuangDong,China.
  7. *Allrightsreserved.
  8. *
  9. *LicensedundertheApacheLicense,Version2.0(the"License");
  10. *youmaynotusethisfileexceptincompliancewiththeLicense.
  11. *YoumayobtainacopyoftheLicenseat
  12. *
  13. *http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
  16. *distributedundertheLicenseisdistributedonan"ASIS"BASIS,
  17. *WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
  18. *SeetheLicenseforthespecificlanguagegoverningpermissionsand
  19. *limitationsundertheLicense.
  20. */
  21. packagecom.sinaapp.msdxblog.android.location;
  22. importjava.nio.charset.Charset;
  23. importorg.json.JSONException;
  24. importorg.json.JSONObject;
  25. importorg.json.JSONTokener;
  26. importcom.sinaapp.msdxblog.androidkit.net.DownloadUtil;
  27. /**
  28. *@authorGeek_Soledad(66704238@51uc.com)
  29. */
  30. publicclassLocationUtil{
  31. publicstaticStringgetAddress(doublelat,doublelng){
  32. Stringurl=String
  33. .format("http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&language=CN&sensor=true",
  34. lat,lng);
  35. System.out.println(url);
  36. Stringresult=DownloadUtil.downloadByUrl(url,
  37. Charset.defaultCharset());
  38. returnjsonSax(result);
  39. }
  40. privatestaticStringjsonSax(Stringin){
  41. Stringaddress=null;
  42. try{
  43. JSONTokenertokener=newJSONTokener(in);
  44. JSONObjectresults=(JSONObject)tokener.nextValue();
  45. if(!results.getString("status").equals("OK")){
  46. return"无法获取具体地址。";
  47. }
  48. System.out.println(results.toString());
  49. JSONObjectresult=(JSONObject)results.getJSONArray("results")
  50. .get(0);
  51. address=result.getString("formatted_address");
  52. }catch(JSONExceptione){
  53. e.printStackTrace();
  54. }
  55. returnaddress;
  56. }
  57. }


蛋疼的是,不知道为什么,我都那样写了,获取到的数据还是英文的,而从浏览器上获取到的却是中文的。求解。

下面是获取到的json数据的格式:
Json代码
  1. {
  2. "results":[
  3. {
  4. "address_components":[
  5. {
  6. "long_name":"外环西路",
  7. "short_name":"外环西路",
  8. "types":["route"]
  9. },
  10. {
  11. "long_name":"麻章区",
  12. "short_name":"麻章区",
  13. "types":["sublocality","political"]
  14. },
  15. {
  16. "long_name":"湛江",
  17. "short_name":"湛江",
  18. "types":["locality","political"]
  19. },
  20. {
  21. "long_name":"广东省",
  22. "short_name":"广东省",
  23. "types":["administrative_area_level_1","political"]
  24. },
  25. {
  26. "long_name":"中国",
  27. "short_name":"CN",
  28. "types":["country","political"]
  29. }
  30. ],
  31. "formatted_address":"中国广东省湛江市麻章区外环西路",
  32. "geometry":{
  33. "bounds":{
  34. "northeast":{
  35. "lat":21.15437220,
  36. "lng":110.29786920
  37. },
  38. "southwest":{
  39. "lat":21.15323980,
  40. "lng":110.29655030
  41. }
  42. },
  43. "location":{
  44. "lat":21.15385030,
  45. "lng":110.29716670
  46. },
  47. "location_type":"APPROXIMATE",
  48. "viewport":{
  49. "northeast":{
  50. "lat":21.15515498029150,
  51. "lng":110.2985587302915
  52. },
  53. "southwest":{
  54. "lat":21.15245701970850,
  55. "lng":110.2958607697085
  56. }
  57. }
  58. },
  59. "types":["route"]
  60. },
  61. {
  62. "address_components":[
  63. {
  64. "long_name":"麻章区",
  65. "short_name":"麻章区",
  66. "types":["sublocality","political"]
  67. },
  68. {
  69. "long_name":"湛江",
  70. "short_name":"湛江",
  71. "types":["locality","political"]
  72. },
  73. {
  74. "long_name":"广东省",
  75. "short_name":"广东省",
  76. "types":["administrative_area_level_1","political"]
  77. },
  78. {
  79. "long_name":"中国",
  80. "short_name":"CN",
  81. "types":["country","political"]
  82. }
  83. ],
  84. "formatted_address":"中国广东省湛江市麻章区",
  85. "geometry":{
  86. "bounds":{
  87. "northeast":{
  88. "lat":21.32472990,
  89. "lng":110.64709280
  90. },
  91. "southwest":{
  92. "lat":20.85674290,
  93. "lng":110.12168620
  94. }
  95. },
  96. "location":{
  97. "lat":21.2633380,
  98. "lng":110.33420
  99. },
  100. "location_type":"APPROXIMATE",
  101. "viewport":{
  102. "northeast":{
  103. "lat":21.32472990,
  104. "lng":110.64709280
  105. },
  106. "southwest":{
  107. "lat":20.85674290,
  108. "lng":110.12168620
  109. }
  110. }
  111. },
  112. "types":["sublocality","political"]
  113. },
  114. {
  115. "address_components":[
  116. {
  117. "long_name":"湛江",
  118. "short_name":"湛江",
  119. "types":["locality","political"]
  120. },
  121. {
  122. "long_name":"广东省",
  123. "short_name":"广东省",
  124. "types":["administrative_area_level_1","political"]
  125. },
  126. {
  127. "long_name":"中国",
  128. "short_name":"CN",
  129. "types":["country","political"]
  130. }
  131. ],
  132. "formatted_address":"中国广东省湛江市",
  133. "geometry":{
  134. "bounds":{
  135. "northeast":{
  136. "lat":21.95539610,
  137. "lng":110.97175080
  138. },
  139. "southwest":{
  140. "lat":20.2210810,
  141. "lng":109.66829810
  142. }
  143. },
  144. "location":{
  145. "lat":21.2707020,
  146. "lng":110.3593870
  147. },
  148. "location_type":"APPROXIMATE",
  149. "viewport":{
  150. "northeast":{
  151. "lat":21.40704480,
  152. "lng":110.5320740
  153. },
  154. "southwest":{
  155. "lat":21.09339670,
  156. "lng":110.20797730
  157. }
  158. }
  159. },
  160. "types":["locality","political"]
  161. },
  162. {
  163. "address_components":[
  164. {
  165. "long_name":"广东省",
  166. "short_name":"广东省",
  167. "types":["administrative_area_level_1","political"]
  168. },
  169. {
  170. "long_name":"中国",
  171. "short_name":"CN",
  172. "types":["country","political"]
  173. }
  174. ],
  175. "formatted_address":"中国广东省",
  176. "geometry":{
  177. "bounds":{
  178. "northeast":{
  179. "lat":25.51677140,
  180. "lng":117.31808370
  181. },
  182. "southwest":{
  183. "lat":20.2210810,
  184. "lng":109.66829810
  185. }
  186. },
  187. "location":{
  188. "lat":23.1321910,
  189. "lng":113.2665310
  190. },
  191. "location_type":"APPROXIMATE",
  192. "viewport":{
  193. "northeast":{
  194. "lat":25.51677140,
  195. "lng":117.31808370
  196. },
  197. "southwest":{
  198. "lat":20.2210810,
  199. "lng":109.66829810
  200. }
  201. }
  202. },
  203. "types":["administrative_area_level_1","political"]
  204. },
  205. {
  206. "address_components":[
  207. {
  208. "long_name":"中国",
  209. "short_name":"CN",
  210. "types":["country","political"]
  211. }
  212. ],
  213. "formatted_address":"中国",
  214. "geometry":{
  215. "bounds":{
  216. "northeast":{
  217. "lat":53.56097399999999,
  218. "lng":134.772810
  219. },
  220. "southwest":{
  221. "lat":18.15352160,
  222. "lng":73.49941369999999
  223. }
  224. },
  225. "location":{
  226. "lat":35.861660,
  227. "lng":104.1953970
  228. },
  229. "location_type":"APPROXIMATE",
  230. "viewport":{
  231. "northeast":{
  232. "lat":53.56097399999999,
  233. "lng":134.772810
  234. },
  235. "southwest":{
  236. "lat":18.15352160,
  237. "lng":73.49941369999999
  238. }
  239. }
  240. },
  241. "types":["country","political"]
  242. }
  243. ],
  244. "status":"OK"
  245. }

纠结了一个小时,在网上转了一大圈之后,才发现应该这样子写:“http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&language=zh-CN&sensor=true”。。真的是估计只有经常使用它的人,和它的开发人员,才知道。。貌似文档中也没有提供相关说明。。(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值