【发布时间】:2017-04-07 13:47:57
【问题描述】:
我正在尝试在 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);
}
}
【问题讨论】:
-
发布堆栈跟踪。
-
寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建 minimal reproducible example。具体来说:崩溃并不是我们可以提供太多帮助的描述。
-
在我看来,您必须尝试在单独的线程中执行反向地理编码。对 Manifest 中的权限进行交叉检查并尝试发布异常转储。
标签: java android maps latitude-longitude