如何从通过谷歌纵横及其得到充分的地址? [英] how to get full address from passing Latitude and Latitude?
问题描述
我使用code我的手机上波纹管得到的地址{乡村,街道,城市},但它没有工作了很多投入,为什么呢?有时崩溃即可。请如何通过传递经纬度到返回所有可用的地址,这个经度和纬度的方法获得的完整的地址即可。可以请你给我提供的答案,得到最好的结果。帮帮我吧。
i used the code on my phone bellow to get address { country, street, city}, but it didnt work for many inputs, why? and sometimes crashing. please how to get the full address by passing Longitude and Latitude to a method that returns all the available address to this Longitude and Latitude. can you please provide me with the answer to get best result. help me.
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)
}
在这里,我要修改的方法
here is the method that i want to modify
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;
}
}
请回答我的问题。
推荐答案
有一个已知的问题,地缘$ C Cr不总是返回一个值。请参见地理codeR并不总是返回一个值和<一个href=\"http://stackoverflow.com/questions/4567216/geo$c$cr-getfromlocationname-returns-only-null\">geo$c$cr.getFromLocationName只返回空。你可以尝试发送一个请求3次在for循环中。它应该能够ATLEAST一次回归。如果没有的话,他们可能是连接问题,也可以是其他的问题,如服务器没有回复您的请求。对我来说,有时它再也没有回来,即使它是连接互联网任何东西。然后,我用这的更可靠的方式来获得该地址每次:
There is a known issue that Geocoder doesn't always returns a value. See Geocoder doesn't always return a value and geocoder.getFromLocationName returns only null. You can try to send a request 3 times in a for loop. It should be able to return atleast once. If not then, their might be a connection issue or can be other issues like server did not reply to your request. For me sometimes it never returned anything even if it was connected to internet. Then, I used this much more reliable way to get the address everytime:
//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;
}
我所谓的功能如下,以获得完整地址:
I called the function as follows to get the complete address:
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
的一样。
希望这helps.This为我工作。如果您要更换的纬度和经度坐标的网址,看到在Web浏览器返回的JSON对象。你会看到刚刚发生了什么。
You can call this inside AsyncTask
or a new thread. I used Asynctask
for the same.
Hope this helps.This worked for me. If you replace the URL with the lat and longitude coordinates and see the returned JSON object in a web browser. You'll see what just happened.
这篇关于如何从通过谷歌纵横及其得到充分的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!