使用android的定位服务,需要在manifest文件里增加相应的权限,这里不赘述。
下面是两个类的代码,第一个是activity,完成的功能是获取经纬度,然后提供查询对应的地址的按钮。
第二个是工具类,从经纬度获取到地址。
蛋疼的是,不知道为什么,我都那样写了,获取到的数据还是英文的,而从浏览器上获取到的却是中文的。求解。
下面是获取到的json数据的格式:
纠结了一个小时,在网上转了一大圈之后,才发现应该这样子写:“http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&language=zh-CN&sensor=true”。。真的是估计只有经常使用它的人,和它的开发人员,才知道。。貌似文档中也没有提供相关说明。。(
下面是两个类的代码,第一个是activity,完成的功能是获取经纬度,然后提供查询对应的地址的按钮。
第二个是工具类,从经纬度获取到地址。
- /*
- *@(#)LocationActivity.javaProject:androidDevices
- *Date:2012-6-5
- *
- *Copyright(c)2011CFuture09,InstituteofSoftware,
- *GuangdongOceanUniversity,Zhanjiang,GuangDong,China.
- *Allrightsreserved.
- *
- *LicensedundertheApacheLicense,Version2.0(the"License");
- *youmaynotusethisfileexceptincompliancewiththeLicense.
- *YoumayobtainacopyoftheLicenseat
- *
- *http://www.apache.org/licenses/LICENSE-2.0
- *
- *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
- *distributedundertheLicenseisdistributedonan"ASIS"BASIS,
- *WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
- *SeetheLicenseforthespecificlanguagegoverningpermissionsand
- *limitationsundertheLicense.
- */
- packagecom.sinaapp.msdxblog.android.location;
- importandroid.app.Activity;
- importandroid.content.Context;
- importandroid.location.Location;
- importandroid.location.LocationListener;
- importandroid.location.LocationManager;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.widget.Toast;
- importcom.sinaapp.msdxblog.android.R;
- /**
- *@authorGeek_Soledad(66704238@51uc.com)
- */
- publicclassLocationActivityextendsActivity{
- privatedoublemLongitude;
- privatedoublemLatitude;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.location);
- }
- publicvoidonButtonClick(Viewv){
- switch(v.getId()){
- caseR.id.get_location:
- getLocation();
- break;
- caseR.id.query:
- Toast.makeText(this,
- LocationUtil.getAddress(mLatitude,mLongitude),1000)
- .show();
- break;
- default:
- break;
- }
- }
- privatevoidgetLocation(){
- LocationManagerlocationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
- locationManager.requestLocationUpdates(
- LocationManager.NETWORK_PROVIDER,0,0,newLocationListener(){
- @Override
- publicvoidonStatusChanged(Stringprovider,intstatus,
- Bundleextras){
- }
- @Override
- publicvoidonProviderEnabled(Stringprovider){
- Toast.makeText(LocationActivity.this,"enable",1000)
- .show();
- System.out.println("enable");
- }
- @Override
- publicvoidonProviderDisabled(Stringprovider){
- Toast.makeText(LocationActivity.this,"disable",1000)
- .show();
- System.out.println("disable");
- }
- @Override
- publicvoidonLocationChanged(Locationlocation){
- mLatitude=location.getLatitude();
- mLongitude=location.getLongitude();
- System.out.println(location.getLatitude());
- System.out.println(location.getLongitude());
- Toast.makeText(LocationActivity.this,
- mLatitude+":"+mLongitude,1000).show();
- }
- });
- }
- }
- /*
- *@(#)LocationUtil.javaProject:androidDevices
- *Date:2012-6-5
- *
- *Copyright(c)2011CFuture09,InstituteofSoftware,
- *GuangdongOceanUniversity,Zhanjiang,GuangDong,China.
- *Allrightsreserved.
- *
- *LicensedundertheApacheLicense,Version2.0(the"License");
- *youmaynotusethisfileexceptincompliancewiththeLicense.
- *YoumayobtainacopyoftheLicenseat
- *
- *http://www.apache.org/licenses/LICENSE-2.0
- *
- *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
- *distributedundertheLicenseisdistributedonan"ASIS"BASIS,
- *WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
- *SeetheLicenseforthespecificlanguagegoverningpermissionsand
- *limitationsundertheLicense.
- */
- packagecom.sinaapp.msdxblog.android.location;
- importjava.nio.charset.Charset;
- importorg.json.JSONException;
- importorg.json.JSONObject;
- importorg.json.JSONTokener;
- importcom.sinaapp.msdxblog.androidkit.net.DownloadUtil;
- /**
- *@authorGeek_Soledad(66704238@51uc.com)
- */
- publicclassLocationUtil{
- publicstaticStringgetAddress(doublelat,doublelng){
- Stringurl=String
- .format("http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&language=CN&sensor=true",
- lat,lng);
- System.out.println(url);
- Stringresult=DownloadUtil.downloadByUrl(url,
- Charset.defaultCharset());
- returnjsonSax(result);
- }
- privatestaticStringjsonSax(Stringin){
- Stringaddress=null;
- try{
- JSONTokenertokener=newJSONTokener(in);
- JSONObjectresults=(JSONObject)tokener.nextValue();
- if(!results.getString("status").equals("OK")){
- return"无法获取具体地址。";
- }
- System.out.println(results.toString());
- JSONObjectresult=(JSONObject)results.getJSONArray("results")
- .get(0);
- address=result.getString("formatted_address");
- }catch(JSONExceptione){
- e.printStackTrace();
- }
- returnaddress;
- }
- }
蛋疼的是,不知道为什么,我都那样写了,获取到的数据还是英文的,而从浏览器上获取到的却是中文的。求解。
下面是获取到的json数据的格式:
- {
- "results":[
- {
- "address_components":[
- {
- "long_name":"外环西路",
- "short_name":"外环西路",
- "types":["route"]
- },
- {
- "long_name":"麻章区",
- "short_name":"麻章区",
- "types":["sublocality","political"]
- },
- {
- "long_name":"湛江",
- "short_name":"湛江",
- "types":["locality","political"]
- },
- {
- "long_name":"广东省",
- "short_name":"广东省",
- "types":["administrative_area_level_1","political"]
- },
- {
- "long_name":"中国",
- "short_name":"CN",
- "types":["country","political"]
- }
- ],
- "formatted_address":"中国广东省湛江市麻章区外环西路",
- "geometry":{
- "bounds":{
- "northeast":{
- "lat":21.15437220,
- "lng":110.29786920
- },
- "southwest":{
- "lat":21.15323980,
- "lng":110.29655030
- }
- },
- "location":{
- "lat":21.15385030,
- "lng":110.29716670
- },
- "location_type":"APPROXIMATE",
- "viewport":{
- "northeast":{
- "lat":21.15515498029150,
- "lng":110.2985587302915
- },
- "southwest":{
- "lat":21.15245701970850,
- "lng":110.2958607697085
- }
- }
- },
- "types":["route"]
- },
- {
- "address_components":[
- {
- "long_name":"麻章区",
- "short_name":"麻章区",
- "types":["sublocality","political"]
- },
- {
- "long_name":"湛江",
- "short_name":"湛江",
- "types":["locality","political"]
- },
- {
- "long_name":"广东省",
- "short_name":"广东省",
- "types":["administrative_area_level_1","political"]
- },
- {
- "long_name":"中国",
- "short_name":"CN",
- "types":["country","political"]
- }
- ],
- "formatted_address":"中国广东省湛江市麻章区",
- "geometry":{
- "bounds":{
- "northeast":{
- "lat":21.32472990,
- "lng":110.64709280
- },
- "southwest":{
- "lat":20.85674290,
- "lng":110.12168620
- }
- },
- "location":{
- "lat":21.2633380,
- "lng":110.33420
- },
- "location_type":"APPROXIMATE",
- "viewport":{
- "northeast":{
- "lat":21.32472990,
- "lng":110.64709280
- },
- "southwest":{
- "lat":20.85674290,
- "lng":110.12168620
- }
- }
- },
- "types":["sublocality","political"]
- },
- {
- "address_components":[
- {
- "long_name":"湛江",
- "short_name":"湛江",
- "types":["locality","political"]
- },
- {
- "long_name":"广东省",
- "short_name":"广东省",
- "types":["administrative_area_level_1","political"]
- },
- {
- "long_name":"中国",
- "short_name":"CN",
- "types":["country","political"]
- }
- ],
- "formatted_address":"中国广东省湛江市",
- "geometry":{
- "bounds":{
- "northeast":{
- "lat":21.95539610,
- "lng":110.97175080
- },
- "southwest":{
- "lat":20.2210810,
- "lng":109.66829810
- }
- },
- "location":{
- "lat":21.2707020,
- "lng":110.3593870
- },
- "location_type":"APPROXIMATE",
- "viewport":{
- "northeast":{
- "lat":21.40704480,
- "lng":110.5320740
- },
- "southwest":{
- "lat":21.09339670,
- "lng":110.20797730
- }
- }
- },
- "types":["locality","political"]
- },
- {
- "address_components":[
- {
- "long_name":"广东省",
- "short_name":"广东省",
- "types":["administrative_area_level_1","political"]
- },
- {
- "long_name":"中国",
- "short_name":"CN",
- "types":["country","political"]
- }
- ],
- "formatted_address":"中国广东省",
- "geometry":{
- "bounds":{
- "northeast":{
- "lat":25.51677140,
- "lng":117.31808370
- },
- "southwest":{
- "lat":20.2210810,
- "lng":109.66829810
- }
- },
- "location":{
- "lat":23.1321910,
- "lng":113.2665310
- },
- "location_type":"APPROXIMATE",
- "viewport":{
- "northeast":{
- "lat":25.51677140,
- "lng":117.31808370
- },
- "southwest":{
- "lat":20.2210810,
- "lng":109.66829810
- }
- }
- },
- "types":["administrative_area_level_1","political"]
- },
- {
- "address_components":[
- {
- "long_name":"中国",
- "short_name":"CN",
- "types":["country","political"]
- }
- ],
- "formatted_address":"中国",
- "geometry":{
- "bounds":{
- "northeast":{
- "lat":53.56097399999999,
- "lng":134.772810
- },
- "southwest":{
- "lat":18.15352160,
- "lng":73.49941369999999
- }
- },
- "location":{
- "lat":35.861660,
- "lng":104.1953970
- },
- "location_type":"APPROXIMATE",
- "viewport":{
- "northeast":{
- "lat":53.56097399999999,
- "lng":134.772810
- },
- "southwest":{
- "lat":18.15352160,
- "lng":73.49941369999999
- }
- }
- },
- "types":["country","political"]
- }
- ],
- "status":"OK"
- }
纠结了一个小时,在网上转了一大圈之后,才发现应该这样子写:“http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&language=zh-CN&sensor=true”。。真的是估计只有经常使用它的人,和它的开发人员,才知道。。貌似文档中也没有提供相关说明。。(