Join David Gassner for an in-depth discussion in this video Displaying different types of maps, part of Adding Google Maps to Android Apps.
- In a previous exercise, I showed how to control the type of map that's displayed using XML code in the layout file, but you can also control the map type with Java code. In this project named MapTypes, I've changed the menu for the main activity to display the five types of maps; normal, satellite, terrain, hybrid, and none, and I've assigned an ID for each of these menu items. Now to manage this, I've changed my onOptionsItemSelected method in the main activity.
I've taken out the default code to handle the settings item 'cause that's no longer there in the menu, and I've just added a comment indicating where we'll need to handle this menu. In the app's current state, when I touch the Options icon in the top right corner, I see the menu items displayed, but when I choose one of them nothing happens. It's my job now to add the code that will explicitly control the map type in reaction to those menu options being selected. So I'll go back to Android Studio and I'll add the code here.
I'm going to use a switch statement. I already have code in place on Line 66 that's getting an integer value named id from the getItemID method of the menu item object. So I'll pass that into the switch statement and then I'll add some cases, one for each menu item and therefore, one for each map type. I'll start with case and then R.id.mapTypeNone. So if the user selected that option, I'll set the map type to none with mMap, that's my reference to my map object .setMapType.
Then I'll use a constant of the Google Map class. There are five of these constants, one for each type of map, and so I'll choose the one that matches the menu item the user selected, mapTypeNone. And then I'll add a break statement to make sure I don't continue executing any other cases I might add. Now I'll take those three lines of code and I'll duplicate them four times. And then I'll go through and change the ids and the constants for the various types of maps.
For the next one, I'll look for an id of mapTypeNormal and I'll react by setting the map type to the constant that matches that. Then I'll handle a request for a satellite map, and then something called a terrain map, and finally a hybrid map. And that's all the code I need. I don't need a default case because I've handled all of the options that might show up from my Options menu.
So I'll rerun the app on the device. When the app appears, I'll go to my Options menu and I'll choose satellite, and that's an obvious change. Instead of seeing the vector-based graphical map, I'm now seeing a satellite image of the requested area. Next, I'll change to a hybrid, and this is a combination of the vector-based map and the satellite image. Next I'll choose terrain, and on first glance this might look pretty much the same as the normal map, but that's only because I'm looking at an urbanized core.
I'm going to pan and zoom out a little bit until I get to an area where there are obvious changes in elevation and that's what a terrain map can look like. And then I'll switch back to a normal map for the same area, and now you can clearly see a difference. Now if the user chooses none, that's exactly what they'll get, a blank screen. So instead, I'll go back to my normal map and I still have my geocoding code in place so I'll search for London, and then I'll look at a satellite map of London.
So that's a look at how to control map types using Java code. Because this is entirely controlled by your custom code, you can decide which types of maps your app is going to support.
- Getting a Google Maps API key
- Adding the Google Play services library
- Setting a map's initial state
- Geocoding addresses
- Displaying different types of maps
- Working with the current location
- Adding and customizing map markers
- Drawing on maps
- Preparing map apps for deployment