ANDROID 获得地理位置

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

Java代码   收藏代码
  1. /* 
  2.  * @(#)LocationActivity.java               Project:androidDevices 
  3.  * Date:2012-6-5 
  4.  * 
  5.  * Copyright (c) 2011 CFuture09, Institute of Software,  
  6.  * Guangdong Ocean University, Zhanjiang, GuangDong, China. 
  7.  * All rights reserved. 
  8.  * 
  9.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  10.  *  you may not use this file except in compliance with the License. 
  11.  * You may obtain a copy of the License at 
  12.  * 
  13.  *     http://www.apache.org/licenses/LICENSE-2.0 
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing, software 
  16.  * distributed under the License is distributed on an "AS IS" BASIS, 
  17.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  18.  * See the License for the specific language governing permissions and 
  19.  * limitations under the License. 
  20.  */  
  21. package com.sinaapp.msdxblog.android.location;  
  22.   
  23. import android.app.Activity;  
  24. import android.content.Context;  
  25. import android.location.Location;  
  26. import android.location.LocationListener;  
  27. import android.location.LocationManager;  
  28. import android.os.Bundle;  
  29. import android.view.View;  
  30. import android.widget.Toast;  
  31.   
  32. import com.sinaapp.msdxblog.android.R;  
  33.   
  34. /** 
  35.  * @author Geek_Soledad (66704238@51uc.com) 
  36.  */  
  37. public class LocationActivity extends Activity {  
  38.     private double mLongitude;  
  39.     private double mLatitude;  
  40.   
  41.     @Override  
  42.     protected void onCreate(Bundle savedInstanceState) {  
  43.         super.onCreate(savedInstanceState);  
  44.         setContentView(R.layout.location);  
  45.     }  
  46.   
  47.     public void onButtonClick(View v) {  
  48.         switch (v.getId()) {  
  49.         case R.id.get_location:  
  50.             getLocation();  
  51.             break;  
  52.         case R.id.query:  
  53.             Toast.makeText(this,  
  54.                     LocationUtil.getAddress(mLatitude, mLongitude), 1000)  
  55.                     .show();  
  56.             break;  
  57.         default:  
  58.             break;  
  59.         }  
  60.     }  
  61.   
  62.     private void getLocation() {  
  63.         LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
  64.         locationManager.requestLocationUpdates(  
  65.                 LocationManager.NETWORK_PROVIDER, 00new LocationListener() {  
  66.                     @Override  
  67.                     public void onStatusChanged(String provider, int status,  
  68.                             Bundle extras) {  
  69.                     }  
  70.   
  71.                     @Override  
  72.                     public void onProviderEnabled(String provider) {  
  73.                         Toast.makeText(LocationActivity.this"enable"1000)  
  74.                                 .show();  
  75.                         System.out.println("enable");  
  76.                     }  
  77.   
  78.                     @Override  
  79.                     public void onProviderDisabled(String provider) {  
  80.                         Toast.makeText(LocationActivity.this"disable"1000)  
  81.                                 .show();  
  82.                         System.out.println("disable");  
  83.                     }  
  84.   
  85.                     @Override  
  86.                     public void onLocationChanged(Location location) {  
  87.                         mLatitude = location.getLatitude();  
  88.                         mLongitude = location.getLongitude();  
  89.                         System.out.println(location.getLatitude());  
  90.                         System.out.println(location.getLongitude());  
  91.                         Toast.makeText(LocationActivity.this,  
  92.                                 mLatitude + ":" + mLongitude, 1000).show();  
  93.                     }  
  94.                 });  
  95.     }  
  96. }  


Java代码   收藏代码
  1. /* 
  2.  * @(#)LocationUtil.java               Project:androidDevices 
  3.  * Date:2012-6-5 
  4.  * 
  5.  * Copyright (c) 2011 CFuture09, Institute of Software,  
  6.  * Guangdong Ocean University, Zhanjiang, GuangDong, China. 
  7.  * All rights reserved. 
  8.  * 
  9.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  10.  *  you may not use this file except in compliance with the License. 
  11.  * You may obtain a copy of the License at 
  12.  * 
  13.  *     http://www.apache.org/licenses/LICENSE-2.0 
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing, software 
  16.  * distributed under the License is distributed on an "AS IS" BASIS, 
  17.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  18.  * See the License for the specific language governing permissions and 
  19.  * limitations under the License. 
  20.  */  
  21. package com.sinaapp.msdxblog.android.location;  
  22.   
  23. import java.nio.charset.Charset;  
  24.   
  25. import org.json.JSONException;  
  26. import org.json.JSONObject;  
  27. import org.json.JSONTokener;  
  28.   
  29. import com.sinaapp.msdxblog.androidkit.net.DownloadUtil;  
  30.   
  31. /** 
  32.  * @author Geek_Soledad (66704238@51uc.com) 
  33.  */  
  34. public class LocationUtil {  
  35.   
  36.     public static String getAddress(double lat, double lng) {  
  37.         String url = String  
  38.                 .format("http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&language=CN&sensor=true",  
  39.                         lat, lng);  
  40.         System.out.println(url);  
  41.         String result = DownloadUtil.downloadByUrl(url,  
  42.                 Charset.defaultCharset());  
  43.         return jsonSax(result);  
  44.     }  
  45.   
  46.     private static String jsonSax(String in) {  
  47.         String address = null;  
  48.         try {  
  49.             JSONTokener tokener = new JSONTokener(in);  
  50.             JSONObject results = (JSONObject) tokener.nextValue();  
  51.             if (!results.getString("status").equals("OK")) {  
  52.                 return "无法获取具体地址。";  
  53.             }  
  54.             System.out.println(results.toString());  
  55.             JSONObject result = (JSONObject) results.getJSONArray("results")  
  56.                     .get(0);  
  57.             address = result.getString("formatted_address");  
  58.         } catch (JSONException e) {  
  59.             e.printStackTrace();  
  60.         }  
  61.         return address;  
  62.     }  
  63. }  


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

下面是获取到的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”。。真的是估计只有经常使用它的人,和它的开发人员,才知道。。貌似文档中也没有提供相关说明。。(
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值