Android使用聚合数据接口解析JSON数据显示当前地址

来源:互联网 发布:网络性能管理 编辑:程序博客网 时间:2024/06/16 16:05

聚合数据经纬度解析

接口地址: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);    }@Overrideprotected 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;                }            }        };}
0 0
原创粉丝点击