0

I've got an options menu that allows the user to change map type on google maps. That works fine, but I'd like to offer the functionality to change the markers presently showing which I control with a boolean value. Here is my current code.

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.map_styles_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);

    switch(item.getItemId()) {
    case R.id.normal_map:
        mapType=GoogleMap.MAP_TYPE_NORMAL;
        break;

    case R.id.satellite_map:
        mapType=GoogleMap.MAP_TYPE_SATELLITE;
        break;

    case R.id.terrain_map:
        mapType=GoogleMap.MAP_TYPE_TERRAIN;
        break;

    case R.id.hybrid_map:
        mapType=GoogleMap.MAP_TYPE_HYBRID;
        break;
    }

    googleMap.setMapType(mapType);
    return true;
}

I guess what I'd like to do is create another case statement like so

case R.id.present:
        mapChange=true;
        break;
case R.id.past:
        mapChange=false;
        break;

I use items in the menu XML file to represent the maps, could I use items for changing a boolean too?

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/map_types"
    android:title="Menu"
    android:showAsAction="always">
    <menu>
        <item android:id="@+id/normal_map"
            android:title="Mormal map"/>
        <item android:id="@+id/satellite_map"
            android:title="Satellite map"/>
        <item android:id="@+id/terrain_map"
            android:title="Terrain map"/>
        <item android:id="@+id/hybrid_map"
            android:title="Hybrid map"/>
    </menu>     
</item>   

So I would add an item below the above like

<item android:id="@+id/past"
            android:title="Change to past markers"/>
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
algorhythm
  • 3,304
  • 6
  • 36
  • 56
  • Not sure what you want (e.g. why do you have 2 separate ids ("present" and "past" and what are they supposed to do?) If you looking for checked items: http://stackoverflow.com/questions/6239163/android-checkable-menu-item – Michael Dec 18 '13 at 12:15
  • Well I have a boolean value, and when true it runs code to display current markers, and if false those markers are then hidden on the map and a set of other markers are displayed to the user – algorhythm Dec 18 '13 at 12:17
  • So that sounds like you want a single menu item that toggles when the user selects it... Try the checkable item from the link above. – Michael Dec 18 '13 at 12:23
  • Yeah defo a menu item, but I already have the 4 map types so would it be possible to add the toggle as well as it's a different type? – algorhythm Dec 18 '13 at 12:43
  • Sure, you just add one more item, make it checkable="true" and then toggle the check like item.setChecked(!item.isChecked()) in onOptionsItemSelected. – Michael Dec 18 '13 at 17:00

2 Answers2

0

First of all I would suggest to try everything that you think can work. And if still not finding an answer then post on SO. With the explanation that you have mentioned and with my limited knowledge I conclude that you need one option in your option menu which will toggle between your markers screen and other markers screen.

So add the option in your xml:

<item android:id="@+id/past"
        android:title="Change to past markers"/>

then in your java file add this code:

case R.id.past:
        if(mapChange){
        mapChange=false;
        //your code to show other markers
        }else{
            mapChange=true;
            //your code to show same markers etc
        }
        break;
AabidMulani
  • 2,325
  • 1
  • 28
  • 47
0

Well if you want you can just create another method named "changeState()" and put your suggested switch case in that method

Private void changeState() {

Switch(VARIABLE-HERE) 
{
case R.id.present: 
mapChange=true; 
break;
case R.id.past: 
mapChange=false; 
break;
} 
} 

And call it after.

googleMap.setMapType(mapType);

Mihir Solanki
  • 308
  • 2
  • 11
  • Would this add the extra 2 buttons to the end of the options menu though? Surely this would require a new options menu? Essentially I need 6 buttons on top of each other when the options menu is pressed – algorhythm Dec 18 '13 at 12:41
  • It would not add two new buttons. I am fairly new to android development but intermediate in java aspects so i can help only till java extent.. Sorry :) – Mihir Solanki Dec 18 '13 at 15:37