我正在尝试使用Android中的纬度和经度来获取物理地址
一切正常,但是对于某些坐标(如这一个:32.696627,74.8798822)不起作用,我的应用程序崩溃了。
这是我的代码:
import java.util.List;
import java.util.Locale;
import static infolabs.localite.R.id.longitude;
public class MainActivity extends AppCompatActivity {
String s ="";
EditText lati , longi;
TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public void show_me_the_details(View v) throws Exception
{
lati =(EditText)findViewById(R.id.latitude);
longi =(EditText)findViewById(R.id.longitude);
double a = Double.parseDouble(lati.getText().toString());
double b = Double.parseDouble(longi.getText().toString());
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(a, b, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName();
TextView t = (TextView)findViewById(R.id.details);
String full_address = address+"\n"+city+"\n"+state+"\n"+country+"\n"+postalCode+"\n"+knownName+"\n";
t.setText(full_address);
}
}
答案 0 :(得分:0)
Andriod的地理编码器API是免费的,会给你正确的地址。
根据崩溃而言,你需要调试它。尝试将Logging和封装代码放入try catch中。
观察:我观察到你写的地理编码器代码是在UI线程中,这不是应该在后台服务中计算的方式,如果API花费大量时间,程序将会挂起。
尝试通过Andriod标准练习,因为地理编码器是最受欢迎的API。