“定位查询”locate()方法,增加一个线程,在该线程中处理查询地点的功能,请参考代码清单16-10,完整代码请参考chapter16_7工程中src/com/work/map/MyMapActivity.java文件locate()方法代码部分。
【代码清单16-1】
- /**
- * 定位查询
- */
- private void locate() {
- LayoutInflater factory = LayoutInflater.from(MyMapActivity.this);
- View locationView = factory.inflate(R.layout.find_dialog, null);
- final EditText findText = (EditText) locationView
- .findViewById(R.id.dailog_find);
- new AlertDialog.Builder(this).setTitle(R.string.dialog_find).setView(
- locationView).setPositiveButton(R.string.button_ok,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- findString = findText.getText().toString();
- progDialog = ProgressDialog.show(MyMapActivity.this,
- "处理中...", "定位" + findString, true, false);
- new Thread() {
- @Override
- public void run() {
- try {
- Geocoder geocoder = new Geocoder(
- MyMapActivity.this);
- addresses = geocoder.getFromLocationName(
- findString, 1);
- if (addresses.size() > 0) {
- List<OverlayItem> overlayitems = new ArrayList<OverlayItem>();
- double lat = addresses.get(0)
- .getLatitude();
- double lng = addresses.get(0)
- .getLongitude();
- // 设定中心点
- centerPoit = new GeoPoint(
- (int) (lat * 1E6),
- (int) (lng * 1E6)); // 地理坐标
- mc.setCenter(centerPoit);
- Log.i(TAG, " lat " + lat + " lng "
- + lng);
- int intMaxAddressLineIndex = addresses
- .get(0)
- .getMaxAddressLineIndex();
- String address = "地址:";
- for (int j = 0; j <= intMaxAddressLineIndex; j++) {
- if (addresses.get(0) == null)
- continue;
- address += addresses.get(0)
- .getAddressLine(j)
- + ",";
- }
- if (address.endsWith(",")) {
- address = address.substring(0,
- address.length() - 1);
- }
- String title = "";
- if (addresses.get(0).getFeatureName() == null) {
- title = "";
- } else {
- title = addresses.get(0)
- .getFeatureName();
- }
- overlayitems.add(new OverlayItem(
- centerPoit, title, address));
- Drawable marker = getResources()
- .getDrawable(
- R.drawable.markermap2);
- locs = new LocationItemsOverlay(marker,
- overlayitems);
- handler.sendEmptyMessage(0);
- } else {
- handler.sendEmptyMessage(1);
- }
- } catch (Exception e) {
- e.printStackTrace();
- handler.sendEmptyMessage(1);
- }
- }
- }.start();
- }
- }).setNegativeButton(R.string.button_cancel,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- }
- }).show();
- }
通过下面的代码是实现显示进度条:
progDialog =ProgressDialog.show(MyMapActivity.this, "处理中...", "定位" + findString, true, false);
启动一个子线程,在该线程中实现地点查询,但是不能有更新UI的处理,如果查询成功调用handler.sendEmptyMessage(0),如果失败调用handler.sendEmptyMessage(1)。
- new Thread() {
- @Override
- public void run() {
- … …
- }
- }.start();
在Hander的handleMessage方法中处理更新UI操作,其中成功(case 0)时候清除屏幕上原来的图层,重新添加图层,最后progDialog.dismiss()方法关闭进度条对话框。如果是查询失败(case 1)弹出Toast说明一下,也要通过progDialog.dismiss()方法关闭进度条对话框,否则进度条对话框不会关闭。
- private Handler handler = new Handler() {
- @Override
- public voidhandleMessage(Message msg) {
- switch (msg.what) {
- case 0:
- mapView.getOverlays().clear();
- mapView.getOverlays().add(locs);
- progDialog.dismiss();
- break;
- case 1:
- Toast.makeText(MyMapActivity.this,"暂时无法" + findString + "信息。",
- Toast.LENGTH_SHORT).show();
- progDialog.dismiss();
- }
- }
- };
出自《Android开发案例驱动教程》第十六章