后退按钮无法在Android谷歌地图中工作

时间:2011-07-26 04:46:45

标签: android android-maps

我的简单应用程序有谷歌地图与叠加。还有一个onDraw()方法可以在我的位置放置一个图标。当我单击后退按钮时,应用程序不会关闭或转到活动前:(这是我在活动中使用的方法。

public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_I) {
            gMapView.getController().setZoom(gMapView.getZoomLevel() + 1);
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_O) {
            gMapView.getController().setZoom(gMapView.getZoomLevel() - 1);
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_S) {
            gMapView.setSatellite(true);
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_T) {
            gMapView.setTraffic(true);
            return true;
        }
        return false;
    }



/* Class overload draw method which actually plot a marker,text etc. on Map */

protected class MyLocationOverlay extends com.google.android.maps.Overlay {

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
        Paint paint = new Paint();

        super.draw(canvas, mapView, shadow);
        // Converts lat/lng-Point to OUR coordinates on the screen.
        Point myScreenCoords = new Point();
        mapView.getProjection().toPixels(p, myScreenCoords);

        paint.setStrokeWidth(1);
        paint.setARGB(255, 255, 255, 255);
        paint.setStyle(Paint.Style.STROKE);

        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.passenger);

        canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
        canvas.drawText("I am here...", myScreenCoords.x, myScreenCoords.y, paint);
        return true;

    }

3 个答案:

答案 0 :(得分:1)

public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_I) {
            gMapView.getController().setZoom(gMapView.getZoomLevel() + 1);
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_O) {
            gMapView.getController().setZoom(gMapView.getZoomLevel() - 1);
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_S) {
            gMapView.setSatellite(true);
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_T) {
            gMapView.setTraffic(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

试试这个

答案 1 :(得分:1)

您应该删除方法末尾的return true;,而是在其中添加else语句,其中包含return super.onKeyDown(keyCode, event);

答案 2 :(得分:0)

在onKeyDown方法的else部分添加以下代码。

   return super.onKeyDown(keyCode, event);