Android使用聚合資料介面解析JSON資料顯示當前地址
聚合資料經緯度解析
介面地址:http://apis.juhe.cn/geo/
支援格式:JSON/XML
請求方式:GET
請求示例:http://apis.juhe.cn/geo/?key=您申請的APPKEY&lat=39.907314&lng=116.391279&type=1
介面備註:實現GPS/百度經緯度/谷歌經緯度 解析成地理位置資訊
請求引數:
名稱 型別 必填 說明
lng String Y 經度 (如:119.9772857)
lat String Y 緯度 (如:27.327578)
key String Y 申請的APPKEY
type Int Y 傳遞的座標型別,1:GPS 2:百度經緯度 3:谷歌經緯度
dtype String N 返回資料格式:json或xml,預設json
JSON返回示例:
{
“resultcode”:”200”,
“reason”:”Successed!”,
“result”:{
“lat”:”39.915065193348”,
“lng”:”116.40389843345”,
“type”:”1”,
“address”:”北京市東城區中華路甲10號”,
“business”:”天安門”,
“citycode”:131
}
}
由介面文件可知,只要我們獲取當前經緯度併發送到聚合資料介面,可解析返回的JSON資料得到當前地址,程式碼如下:
public class MainActivity extends Activity {
private TextView positionTextView;//顯示地址
private LocationManager locationmanager;//位置管理器
private String provider;//GPS或Network提供
private static final int SHOW_LOCATION=0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CommonFun.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
positionTextView = (TextView) findViewById(R.id.position_text_view);
locationmanager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//獲取當前可用GPS或Network
List<String> providerList = locationmanager.getProviders(true);
if(providerList.contains(LocationManager.GPS_PROVIDER)){
provider = LocationManager.GPS_PROVIDER;
}
else if(providerList.contains(LocationManager.NETWORK_PROVIDER)){
provider = LocationManager.NETWORK_PROVIDER;
}
else{
Toast.makeText(this, "No location provider to use", Toast.LENGTH_SHORT).show();
return;
}
//得到地址例項
Location location = locationmanager.getLastKnownLocation(provider);
if(location!=null){
showLocation(location);//呼叫顯示地址方法
}
locationmanager.requestLocationUpdates(provider, 5000, 10, locationListener);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(locationmanager!=null){
locationmanager.removeUpdates(locationListener);
}
}
LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
showLocation(location);
}
};
private void showLocation(final Location location){
new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
try{
StringBuilder url = new StringBuilder();
url.append("http://apis.juhe.cn/geo/?key=af0de3a4a0c37181a285d1606869b59d&lat=");
//key值為聚合資料上申請的開發者key
url.append(location.getLatitude()).append("&lng="); url.append(location.getLongitude()).append("&type=1");
url.append("&sensor=false");
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url.toString());
HttpResponse httpResponse = httpClient.execute(httpGet);
//利用get方法傳送url得到JSON資料
if(httpResponse.getStatusLine().getStatusCode()==200){
//得到JSON資料
HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity, "utf-8");
//根據JSON資料格式進行解析
JSONObject jsonObject = new JSONObject(response);
JSONObject result = jsonObject.getJSONObject("result");
String position= result.getString("address");
//子執行緒中不能設定TextView屬性,非同步資訊處理
Message message = new Message();
message.what = SHOW_LOCATION;
message.obj = position;
handler.sendMessage(message);
}
}catch(Exception e){
e.printStackTrace();
}
}
}).start();
}
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case SHOW_LOCATION:
String currentPosition =(String) msg.obj;
positionTextView.setText(currentPosition);
break;
default:
break;
}
}
};
}