【发布时间】:2012-07-11 10:31:40
【问题描述】:
我编写了一个简单的地理编码应用程序,但它不起作用。我已授予所有必要的权限。请有人告诉我哪里出错了。提前谢谢。
public class ForgeocdingActivity extends Activity {
Geocoder gc;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText ed=(EditText)findViewById(R.id.editText1);
Button b1=(Button)findViewById(R.id.button1);
final String to_add = ed.getText().toString();
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0)
{
try
{
List<Address> address2 = gc.getFromLocationName(to_add,3);
if(address2 != null && address2.size() > 0)
{
double lat1 = address2.get(0).getLatitude();
double lng1 = address2.get(0).getLongitude();
Toast.makeText(getBaseContext(), "Lat:"+lat1+"Lon:"+lng1, Toast.LENGTH_SHORT).show();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
}
【问题讨论】:
-
首先,您没有初始化 Geocoder 对象...请在此处发布堆栈跟踪,如果您需要更多帮助,还可以提高您的接受率:)
-
您是在真机还是模拟器上测试?因为
Geocoder不能在模拟器上运行 -
@AditiSharma 请看我的回答。
-
是的,我看到了你的答案。现在可以了。