您的位置:首页 > 移动开发 > Android开发

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

2014-10-06 18:39 691 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: