我使用手机上的代码来获取地址{country,street,city},但它对许多输入没有用,为什么?有时崩溃。如何通过将经度和纬度传递给一个方法来获取完整地址,该方法将所有可用地址返回到此经度和纬度。能不能给我答案才能得到最好的结果。帮助我。
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Get_Location_Name extends Activity implements OnClickListener {
private EditText ET1;
private EditText ET2;
private TextView TV1;
private Button B1;
static String result ="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location_name);
ET1=(EditText)this.findViewById(R.id.ET1_location_name);
ET2=(EditText)this.findViewById(R.id.ET2_location_name);
TV1=(TextView)this.findViewById(R.id.TV1_Location_name);
B1=(Button)this.findViewById(R.id.B1_Location_name);
B1.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
String s=null;
if(!ET1.getText().toString().isEmpty() && !ET2.getText().toString().isEmpty())
{
if(this.isOnline())
{
for(int i=0;i<=10;i++)
{
s=getAddressFromLocation(Double.parseDouble(ET2.getText().toString()),
Double.parseDouble(ET1.getText().toString()),this);
}
if(s!=null)
{
Log.d("ssss","s"+s);
TV1.setText(s);
}
else
TV1.setText("s is null");
}
else
TV1.setText("no internet connection");
}
else
TV1.setText("Enter the Lat. and Lon.");
}
public boolean isOnline() {
// code to check connectivity // it works fine(no problems)
}
这是我要修改的方法
public static String getAddressFromLocation(final double lon,final double lat, final Context context)
{
Thread thread = new Thread() {
@Override public void run()
{
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
try {
List<Address> list = geocoder.getFromLocation(
lat, lon, 1);
if (list != null && list.size() > 0)
{
Address address = list.get(0);
result = " "+address.getAddressLine(0) + ", " + address.getLocality()+","+address.getCountryName();
}
}
catch (IOException e)
{
Log.e("fafvsafagag", "Impossible to connect to Geocoder", e);
}
}
};
thread.start();
return result;
}
}
请回答我的问题。
答案 0 :(得分:1)
有一个已知问题,即Geocoder 始终返回值。请参阅Geocoder doesn't always return a value和geocoder.getFromLocationName returns only null。您可以尝试在for循环中发送3次请求。它应该能够至少返回一次。如果没有,那么它们可能是连接问题,或者可能是服务器没有回复您的请求等其他问题。对我来说,即使它连接到互联网,有时也从未返回任何内容。然后,我使用this更可靠的方式每次获取地址:
//lat, lng are Double variables containing latitude and longitude values.
public JSONObject getLocationInfo() {
//Http Request
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
//Create a JSON from the String that was return.
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
我调用了以下函数来获取完整的地址:
JSONObject ret = getLocationInfo(); //Get the JSON that is returned from the API call
JSONObject location;
String location_string;
//Parse to get the value corresponding to `formatted_address` key.
try {
location = ret.getJSONArray("results").getJSONObject(0);
location_string = location.getString("formatted_address");
Log.d("test", "formattted address:" + location_string);
} catch (JSONException e1) {
e1.printStackTrace();
}
你可以在AsyncTask
或新线程中调用它。我使用了Asynctask
。
希望这会有所帮助。这对我有用。如果使用纬度和经度坐标替换URL,并在Web浏览器中查看返回的JSON对象。你会看到刚刚发生的事情。