Книга: Beginning Android

Zoom

Zoom

The map of the world you start with is rather broad. Usually, people looking at a map on a phone will be expecting something a bit narrower in scope, such as a few city blocks.

You can control the zoom level directly via the setZoom() method on the MapController. This takes an integer representing the level of zoom, where 1 is the world view and 21 is the tightest zoom you can get. Each level is a doubling of the effective resolution: 1 has the equator measuring 256 pixels wide, while 21 has the equator measuring 268,435,456 pixels wide. Since the phone’s display probably doesn’t have 268,435,456 pixels in either dimension, the user sees a small map focused on one tiny corner of the globe. A level of 16 will show you several city blocks in each dimension and is probably a reasonable starting point for you to experiment with.

If you wish to allow users to change the zoom level, you will need to do a few things:

• First, pick a spot on the screen where you want the zoom controls to appear. These are not huge, and they only appear when being used, so they can overlay the actual map itself if you choose. In the layout previously shown, for example, the zoom controls are placed over the map, in the lower-left corner of the screen. You should use a LinearLayout or other simple container for the zoom controls’ position in your layout.

• In your activity’s onCreate() method, get your zoom controls’ container via findViewById().

• Add the result o f map.getZoomControls() to that container.

For example, here are the lines from the NooYawk activity’s onCreate() method that accomplish the latter points:

ViewGroup zoom = (ViewGroup)findViewById(R.id.zoom);
zoom.addView(map.getZoomControls());

Then, you can manually get the zoom controls to appear by calling displayZoomControls() on your MapView, or they will automatically appear when the user pans the map as seen in Figure 34-1.


Figure 34-1. Map with zoom indicator and compass rose

Оглавление книги

Оглавление статьи/книги

Генерация: 0.877. Запросов К БД/Cache: 2 / 0
поделиться
Вверх Вниз