聚合數據經緯度解析
接口地址: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;
}
}
};
}