Search code examples
androidgoogle-mapspreferenceactivity

setMapType for google map V2


I want to make a PreferenceActivity and set map type of google map. I see that it can be with command mGoogleMap.setMapType(Googlemap.MAP_TYPE_NORMAL);

How i can do it with SharedPreferences?

I use a ListPreference at preferences.xml with an array for values

 <string-array name="listValues">
 <item>"MAP_TYPE_NORMAL"</item>
 <item>"MAP_TYPE_HYBRID"</item>
 <item>"MAP_TYPE_TERRAIN"</item>
</string-array>

so in main activity i made a function to get these values.

private void getPrefs() {
        // Get the xml/preferences.xml preferences  
        SharedPreferences prefs = PreferenceManager
                        .getDefaultSharedPreferences(getBaseContext());

How now i can set the type as argument like

mGoogleMap.setMapType(Googlemap.arg);

Solution

  • Read the preference, map to GoogleMap.MAP_TYPE_XXX and send to GoogleMap.setMapType

    String prefValue = prefs.getString(...);
    int mapType;
    if ("MAP_TYPE_NORMAL".equals(prefValue)) {
        mapType = GoogleMap.MAP_TYPE_NORMAL;
    } else if (...) {
        ...
    }
    googleMap.setMapType(mapType);