`
貌似掉线
  • 浏览: 257024 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

android获取地址及json数据解析

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

/*
 * @(#)LocationActivity.java		       Project:androidDevices
 * Date:2012-6-5
 *
 * Copyright (c) 2011 CFuture09, Institute of Software, 
 * Guangdong Ocean University, Zhanjiang, GuangDong, China.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sinaapp.msdxblog.android.location;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.sinaapp.msdxblog.android.R;

/**
 * @author Geek_Soledad (66704238@51uc.com)
 */
public class LocationActivity extends Activity {
	private double mLongitude;
	private double mLatitude;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.location);
	}

	public void onButtonClick(View v) {
		switch (v.getId()) {
		case R.id.get_location:
			getLocation();
			break;
		case R.id.query:
			Toast.makeText(this,
					LocationUtil.getAddress(mLatitude, mLongitude), 1000)
					.show();
			break;
		default:
			break;
		}
	}

	private void getLocation() {
		LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(
				LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
					@Override
					public void onStatusChanged(String provider, int status,
							Bundle extras) {
					}

					@Override
					public void onProviderEnabled(String provider) {
						Toast.makeText(LocationActivity.this, "enable", 1000)
								.show();
						System.out.println("enable");
					}

					@Override
					public void onProviderDisabled(String provider) {
						Toast.makeText(LocationActivity.this, "disable", 1000)
								.show();
						System.out.println("disable");
					}

					@Override
					public void onLocationChanged(Location location) {
						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.java		       Project:androidDevices
 * Date:2012-6-5
 *
 * Copyright (c) 2011 CFuture09, Institute of Software, 
 * Guangdong Ocean University, Zhanjiang, GuangDong, China.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sinaapp.msdxblog.android.location;

import java.nio.charset.Charset;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.sinaapp.msdxblog.androidkit.net.DownloadUtil;

/**
 * @author Geek_Soledad (66704238@51uc.com)
 */
public class LocationUtil {

	public static String getAddress(double lat, double lng) {
		String url = String
				.format("http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&language=CN&sensor=true",
						lat, lng);
		System.out.println(url);
		String result = DownloadUtil.downloadByUrl(url,
				Charset.defaultCharset());
		return jsonSax(result);
	}

	private static String jsonSax(String in) {
		String address = null;
		try {
			JSONTokener tokener = new JSONTokener(in);
			JSONObject results = (JSONObject) tokener.nextValue();
			if (!results.getString("status").equals("OK")) {
				return "无法获取具体地址。";
			}
			System.out.println(results.toString());
			JSONObject result = (JSONObject) results.getJSONArray("results")
					.get(0);
			address = result.getString("formatted_address");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return address;
	}
}


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

下面是获取到的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”。。真的是估计只有经常使用它的人,和它的开发人员,才知道。。貌似文档中也没有提供相关说明。。(这个答案来自:http://gdgzzch.blog.163.com/blog/static/3764045220118354154284/)
1
2
分享到:
评论
4 楼 貌似掉线 2013-04-08  
xiaozhi6156 写道
一看,差点以为1楼是我呢..- -!

没准是你失散多年的兄弟
3 楼 xiaozhi6156 2013-04-07  
一看,差点以为1楼是我呢..- -!
2 楼 貌似掉线 2012-06-08  
xiaoxin0424 写道
没仔细看文档的结果

?
1 楼 xiaoxin0424 2012-06-06  
没仔细看文档的结果

相关推荐

Global site tag (gtag.js) - Google Analytics