Android反向地理编码有问题

LOVE_FA 发布于 2015/05/17 00:22
阅读 1K+
收藏 2

我用google 的Geocoding API 接口来处理反向地理编码  不知道为什么  显示不出  下面是代码 ,请告诉我原因

package com.test.location;


import java.util.List;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;


import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

private TextView positionTextView;

private LocationManager locationManager;

private String provider; //定义设备确定位置的变量

private static final int SHOW_LOCATION = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
positionTextView = (TextView)
findViewById(R.id.position_text_view);
locationManager = (LocationManager) 
getSystemService(Context.LOCATION_SERVICE);
//获取所有可用的位置提供器
List<String> providerList = locationManager.getProviders(true);
if(providerList.contains(LocationManager.GPS_PROVIDER)){
provider = LocationManager.GPS_PROVIDER;
Toast.makeText(this," location provider is GPS",
Toast.LENGTH_SHORT).show();
}else if
(providerList.contains(LocationManager.NETWORK_PROVIDER)){
provider = LocationManager.NETWORK_PROVIDER; 
Toast.makeText(this," location provider is NETWORK",
Toast.LENGTH_SHORT).show();
}
else{
//当没有可用的位置提供器时,弹出Toast提示用户
Toast.makeText(this,"No location provider to use",
Toast.LENGTH_SHORT).show();
return;
}
//location对象包含了经纬度信息和海拔信息.
//将选择好的位置提供器传入getLastKnownLocation()方法中,就可以得到一个Location对象
Location location = locationManager.getLastKnownLocation(provider);
if(location !=null){
//显示设备现在的信息
showLocation(location);
showLocation_S(location);
}
//locationManager每隔5S检测一下位置的变化位置,但移动距离超过10M时,就会调用
//LocationListener的onLocationChanged()方法,并把新的位置信息作为参数传入
locationManager.requestLocationUpdates
(provider,5000,1,locationListener);
}
    
protected void onDestroy() {
super.onDestroy();
if(locationManager != null){
//关闭程序时将监听器移除,不然将会一直监听下去,赞同CPU资源
locationManager.removeUpdates(locationListener);
}
}

LocationListener locationListener = new LocationListener(){
@Override
public void onStatusChanged
(String provider, int status,Bundle extras){

}
@Override
public void onProviderEnabled(String provider){

}
@Override
public void onProviderDisabled(String provider){

}

@Override
public void onLocationChanged(Location location){
     //更新当前设备的位置信息
showLocation(location);
showLocation_S(location);
}
};

private void showLocation(Location location){
String currentPosition="latitude is " +location.getLatitude()
+"\n"+"longitude is "+location.getLongitude();
positionTextView.setText(currentPosition);
}

//对经纬度进行解析并翻译成中文地址
private void showLocation_S(final Location location){
new Thread(new Runnable(){
@Override
public void run() {
try{
//组装反向地理编码的接口地址
StringBuilder url = new StringBuilder();
url.append("http://maps.googleapis.com/maps/api/geocode/json?latlng=");
url.append(location.getLatitude()).append(",");
url.append(location.getLongitude());
url.append("&sensor=false");
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url.toString());
//在请求头信息头中指定语言,保证服务器会返回中文数据
httpGet.addHeader("Accept-Language","zh-CN");
HttpResponse httpResponse=httpclient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode() == 200){
HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity,"utf-8");

JSONObject jsonObject =new  JSONObject(response);
//获取results节点下的位置信息
JSONArray resultArray = jsonObject.getJSONArray("results");

if(resultArray.length() > 0){
JSONObject subObject = resultArray.getJSONObject(0);
//取出格式化后的位置信息
String address = subObject.getString("formatted_address");
Message message=new Message();
message.what = SHOW_LOCATION;
message.obj = address;
handler.sendMessage(message);
}
}

}catch(Exception e){
e.printStackTrace();
}
}
}).start();
}

private Handler handler = new Handler(){
public void handleMessage(Message msg){
switch(msg.what){
case SHOW_LOCATION:
String currentPosition = (String)msg.obj;
positionTextView.setText(currentPosition);
break;
default:
break;
}
}
};


}


加载中
0
Joezhiheng
Joezhiheng
请问解决了吗?我刚好也卡在这里
返回顶部
顶部