Skip to content Skip to sidebar Skip to footer

Navigation Between Overlays

It´s possible that overlays on a map could receive focus from DPAD/Tab? I have two fragments, a listview and mapview, I want to get focus from the drawable of the overlay, but

Solution 1:

Yes, you can move from one overlay item on MapView to other but there are few things which you should consider.

  1. If you want your MapView to steer according to the Dpad directions while pressing up/down/left on Dpad, then your map will go up/down/left direction showing the map and you wont able to Dpad on overlay items since MapView is having the focus.
  2. But if you want overlay items to be focused, then you have to manually define which overlay item it should focus on which D-Pad direction using setFocus, nextFocus and getFocus methods of ItemizedOverlay class.
  3. Also you said you have listview and MapView in your activity and in order to get the focus back to listview or any other view which is outside MapView will also have to be done programmatically and it could be little tricky.

You can use StateListDrawable to define the different states on overlaid drawable for focus, pressed and default state.

Hope this answers your query.

Solution 2:

I created a sample activity below. Most of the code comes from the MapView tutorial found here: http://developer.android.com/resources/tutorials/views/hello-mapview.html

The 'focus code' is in the onKeyDown() method. When TAB is pressed, focus is shifted to the next overlay. When ENTER is pressed, it shows a Toast, but that's where you can display your content.

The setFocus() method was found in the documentation for ItemizedOverlay found here: https://developers.google.com/maps/documentation/android/reference/

Hope this works.

publicclassOverlayFocusExampleActivityextendsMapActivity {
private HelloItemizedOverlay itemizedoverlay;
private MapView mapView;
private MapController mapController;
privateint currentOverlayIndex;

/*
 * This entire method comes from the MapView tutorial.
 */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapController = mapView.getController();
    List<Overlay> mapOverlays = mapView.getOverlays();

    // overlay_draw is a selector that specifies a different image for state_focusedDrawabledrawable=this.getResources().getDrawable(R.drawable.overlay_draw);
    itemizedoverlay = newHelloItemizedOverlay(drawable, this);

    GeoPointpoint=newGeoPoint(19240000, -99120000);
    OverlayItemoverlayitem=newOverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
    itemizedoverlay.addOverlay(overlayitem);
    GeoPointpoint2=newGeoPoint(35410000, 139460000);
    OverlayItemoverlayitem2=newOverlayItem(point2, "Sekai, konichiwa!", "I'm in Japan!");

    itemizedoverlay.addOverlay(overlayitem2);


    mapOverlays.add(itemizedoverlay);
}

@OverrideprotectedbooleanisRouteDisplayed() {
    returnfalse;
}

@OverridepublicbooleanonKeyDown(int keyCode, KeyEvent event) {
    OverlayItem overlay;
    switch (keyCode) {
        case KeyEvent.KEYCODE_TAB:
            // Retrieve next overlay
            currentOverlayIndex = (currentOverlayIndex + 1) % itemizedoverlay.size();
            overlay = itemizedoverlay.getOverlayItem(currentOverlayIndex);

            itemizedoverlay.setFocus(overlay);

            // Since setFocus() doesn't center the map, we do it ourselves
            mapController.animateTo(overlay.getPoint());
            returntrue;


        case KeyEvent.KEYCODE_DPAD_CENTER:
        case KeyEvent.KEYCODE_ENTER:
            overlay = itemizedoverlay.getFocus();

            if (overlay != null) {
                // Perform associated action// Stub
                Toast.makeText(this, overlay.getSnippet(), Toast.LENGTH_SHORT).show();
                returntrue;
            }


        default:
            returnfalse;
    }
}


/*
 * This entire class comes from the MapView tutorial except getOverlayItem().
 */privateclassHelloItemizedOverlayextendsItemizedOverlay<OverlayItem> {
    private ArrayList<OverlayItem> mOverlays = newArrayList<OverlayItem>();
    private Context mContext;


    publicHelloItemizedOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
    }

    publicHelloItemizedOverlay(Drawable defaultMarker, Context context) {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
    }

    publicvoidaddOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    /*
     * Not in MapView tutorial. Added for focusability.
     */public OverlayItem getOverlayItem(int index) {
        return mOverlays.get(index);
    }

    @Overrideprotected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }

    @Overridepublicintsize() {
        return mOverlays.size();
    }

    @OverrideprotectedbooleanonTap(int index) {
        OverlayItemitem= mOverlays.get(index);
        AlertDialog.Builderdialog=newAlertDialog.Builder(mContext);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();
        returntrue;
    }
}

}

Post a Comment for "Navigation Between Overlays"