android 获取经纬度城市名(通过baidumapapi 以及 json解析)

jar包:volley.jar

下载地址:http://download.csdn.net/detail/u011248571/8007003

这个主要是因为对网络请求不熟,这个包封装的很方便。


工具:需要在baidu map申请一个appkey。(其他amap之类也是可以的,道理类似)



思路是先获取经纬度,再根据经纬度,对

“http://api.map.baidu.com/geocoder?location=latitude,longitude&output=json&key=key”

发出请求,获取json数据。(output换成其他格式比如xml也可以)

得到的结果如下:


接着对json进行解析就可以了。

<span style="font-size:14px;">package com.example.gpstest;

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

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;

public class POSUtil {
	LocationManager locationManager;
	double latitude=0.0;  
	double longitude =0.0;
	private String cityName;
	private String district;
	Context context;
	String srtUri;
	RequestQueue mQueue;
	private String networkError = "获取城市名失败!\n网络错误!";
	private static final String MAPKEY = "";
	
	//构造方法,直接获取当前经纬度
	public POSUtil(Context context){
		this.context = context;
		this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		this.mQueue = Volley.newRequestQueue(context);
		
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_COARSE);
		String provider = locationManager.getBestProvider(criteria, true);
		if(provider != null){
			Location l = locationManager.getLastKnownLocation(provider);
			this.latitude = l.getLatitude();
			this.longitude = l.getLongitude();
			Log.v("LOG", this.latitude+"---"+this.longitude);
		}
		srtUri = "http://api.map.baidu.com/geocoder?location="+latitude+","+longitude+"&output=json&key=MAPKEY";
	}
	
	//将经纬度转换成城市名字
	public void get(){
		/*
		 * srtUri:即地址
		 * 两个listener:监听器
		 * */
		StringRequest request = new StringRequest(srtUri, 
				new Response.Listener<String>() {
					public void onResponse(String response) {
//						Log.d("TAG", response);
						try {
							//根据json的格式,一步一步解析下来
							JSONObject json = new JSONObject(response);
							JSONObject result = json.getJSONObject("result");
							JSONObject addressComponent = result.getJSONObject("addressComponent");
							setCityName(addressComponent.getString("city"));
							setDistrict(addressComponent.getString("district"));
							toast(getCityName()+getDistrict());
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				},new Response.ErrorListener() {
					public void onErrorResponse(VolleyError error) {  
						Log.e("TAG", error.getMessage(), error);
						toast(networkError);
					}
				});
		mQueue.add(request);
	}
	
	private void toast(String string) {
		Toast.makeText(context, string, Toast.LENGTH_LONG).show();
	}
	
	public String getCityName() {
		return cityName;
	}

	public String getDistrict() {
		return district;
	}

	public void setDistrict(String district) {
		this.district = district;
	}

	public void setCityName(String cityName) {
		this.cityName = cityName;
	}
}</span>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值