gpt4 book ai didi

java - 向 Google map 中的搜索框添加条件?

转载 作者:行者123 更新时间:2023-12-01 13:21:21 24 4
gpt4 key购买 nike

我是 Android 新手,正在使用 Google map V2 开发应用程序。我设计了 map ,一切都很好。我在顶部放置了一个文本框和按钮,帮助用户搜索他想要的任何位置,并使用这个底部方法进行搜索操作。当用户输入无效位置时应用程序崩溃的问题。我想在这个方法中添加toast条件,当用户输入无效位置时,toast出现(请输入有效位置)

请帮助我

public void search(View v) throws IOException{

EditText textbox = (EditText) findViewById(R.id.editText1);
String location = textboxt.getText().toString();
if (location.length() == 0) {
Toast.makeText(this, "Please Enter a location", Toast.LENGTH_SHORT).show();
return;
}

Geocoder gc = new Geocoder(this);
List<Address> list = gc.getFromLocationName(location, 1);
Address add = list.get(0);
String locality = add.getLocality();
double lat = add.getLatitude();
double lng = add.getLongitude();
LatLng ll = new LatLng(lat, lng);
Gmap.moveCamera(CameraUpdateFactory.newLatLng(ll));
// zoom in Google map
Gmap.animateCamera(CameraUpdateFactory.zoomTo(20));

最佳答案

你可以查一下你List<Address> listlist.size()==0如果我没记错的话那就可以了。尝试像:

try{

if(list.size()!=0){
//Your code
}else{
//Error Message
}

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

}

更新:尝试像这样实现您的代码:

public void search(View v){

EditText textbox = (EditText) findViewById(R.id.editText1);
String location = textboxt.getText().toString();
if (location.length() == 0) {
Toast.makeText(this, "Please Enter a location", Toast.LENGTH_SHORT).show();
}else{

try{

Geocoder gc = new Geocoder(this);

List<Address> list = gc.getFromLocationName(location, 1);


if(list.size()!=0){

Address add = list.get(0);
String locality = add.getLocality();
double lat = add.getLatitude();
double lng = add.getLongitude();
LatLng ll = new LatLng(lat, lng);
Gmap.moveCamera(CameraUpdateFactory.newLatLng(ll));
// zoom in Google map
Gmap.animateCamera(CameraUpdateFactory.zoomTo(20));
}else{
Toast.makeText(this, "Location not found", Toast.LENGTH_SHORT).show();
}

}catch(Exception e){
e.printstacktrace();

}
}
}

关于java - 向 Google map 中的搜索框添加条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22010326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com