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>
下载地址: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>
相关文章推荐
- android通过httpClient请求获取JSON数据并且解析
- android通过httpClient请求获取JSON数据并且解析
- Android 获取Google Weather API 并通过Xml和JSON解析数据
- Android编程实现根据经纬度查询地址并对获取的json数据进行解析的方法
- android:通过url向网络端获取json格式数据并解析显示
- JavaWeb将数据包装成JSON以及JSP通过ajax对JSON的获取与解析(代码实例)
- android通过httpClient请求获取JSON数据并且解析
- JavaWeb将数据包装成JSON以及JSP通过ajax对JSON的获取与解析(代码实例)
- Android通过http方式获取JSON字符串并解析的注意事项(乱码,小黑框)
- Android 获取Google Weather API 并通过Xml和JSON解析数据
- JavaWeb将数据包装成JSON以及JSP通过ajax对JSON的获取与解析(代码实例)
- html中通过js获取接口JSON格式数据解析以及跨域问题
- 【Android应用开发】-(17)获取Google天气预报通过Xml和JSON解析数据
- android通过httpClient请求获取JSON数据并且解析
- android通过httpClient请求获取JSON数据并且解析
- android通过httpClient请求获取JSON数据并且解析
- Android LocationManager定位之获取经纬度以及城市信息
- 中国天气网Android XmlPULL解析:通过城市名获取weatherCode
- Android json解析动态获取key以及解析技巧
- html中通过js获取接口JSON格式数据解析以及跨域问题