day 13: google maps android api v2 part 2

12
Google Maps API V2 Part 2 Ahsanul Karim [email protected] http://droidtraining.wordpress.com Android Application Development MapFragment Class GoogleMap Class

Upload: ahsanul-karim

Post on 03-Sep-2014

7.890 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Day 13: Google Maps Android API V2  Part 2

Google Maps API V2 Part 2

Ahsanul [email protected]

http://droidtraining.wordpress.com

Android Application Development

● MapFragment Class● GoogleMap Class

Page 2: Day 13: Google Maps Android API V2  Part 2

The key class is the GoogleMap class. GoogleMap handles the following operations automatically:

Google Maps API V2 The Map Object

1. Connecting to the Google Maps service.2. Downloading map tiles.3. Displaying tiles on the device screen.4. Displaying various controls such as pan and zoom.5. Responding to pan and zoom gestures by moving the map and zooming in or out.

Page 3: Day 13: Google Maps Android API V2  Part 2

1. MapFragment, a subclass of the Android Fragment class, allows to place a map in an Android Fragment. MapFragment objects act as containers for the map, and provide access to the GoogleMap object.

2. The Google Maps Android API requires API level 12 or higher for the support of MapFragment objects. If you are targeting an application earlier than API level 12, you can access the same functionality through the SupportMapFragment class. You will also have to include the Android Support Library.

[We'll check this later]

Google Maps API V2 MapFragment

Page 4: Day 13: Google Maps Android API V2  Part 2

The basic steps for adding a map are:

1. Follow the steps in from Part 1 to get the API key and add the required attributes to your Android Manifest. [Part 1]

2. Add a Fragment object to the Activity. We added a <fragment> element to the layout file for the Activity. [Part 1]

3. In the Activity's onCreate() method, get the GoogleMap object in the MapFragment. We can set view options for a map object.

4. The last step is to add permissions and other settings to your application's manifest, AndroidManifest.xml.

Once we've followed these steps, we can set the initial options for the GoogleMap object. The MapFragment automatically displays the map at the completion of the onCreate() method.

Google Maps API V2 Add a Map to App

Page 5: Day 13: Google Maps Android API V2  Part 2

Google Maps API V2Add a Map to App

Add a Fragment [1]

To define a Fragment object in an Activity's layout file, add a <fragment> element. In this element, set the android:name attribute to"com.google.android.gms.maps.MapFragment". This automatically attaches a MapFragment to the Activity. [Part 1]

Page 6: Day 13: Google Maps Android API V2  Part 2

Google Maps API V2Add a Map to App

We can also add a MapFragment to an Activity in code. To do this, create a new MapFragment instance, and then call FragmentTransaction.add() to add the Fragment to the current Activity.

For now we are considering minSDKVersion="12"

Add a Fragment [2]

Page 7: Day 13: Google Maps Android API V2  Part 2

Google Maps API V2Add a Map to App

Getting GoogleMap object:Add a Fragment [3]

If MapFragment is included in layout (activity_main.xml):

With the handle to the GoogleMap object for the MapFragment, we can set initial options for the map.

Page 8: Day 13: Google Maps Android API V2  Part 2

Google Maps API V2Add a Map to App

Verify Map Availability:Add a Fragment [3]

Before interacting with GoogleMap object, confirm map availability and Google Play services components are correctly installed on the device.

Page 9: Day 13: Google Maps Android API V2  Part 2

Google Maps API V2Add a Map to App

Map Types [1]

1. Normal: Typical road map. Roads, some man-made features, and important natural features such as rivers are shown. Road and feature labels are also visible.2. Hybrid: Satellite photograph data with road maps added. Road and feature labels are also visible.3. Satellite: Satellite photograph data. Road and feature labels are not visible.4. Terrain: Topographic data. The map includes colors, contour lines and labels, and perspective shading. Some roads and labels are also visible.5. None: No tiles. The map will be rendered as an empty grid with no tiles loaded.

Page 10: Day 13: Google Maps Android API V2  Part 2

Google Maps API V2Add a Map to App

Map Types [2]

Normal Hybrid Terrain

Page 11: Day 13: Google Maps Android API V2  Part 2

Google Maps API V2Add a Map to App

Set Properties Programmatically

GoogleMapOptions object can be used

● If using a MapFragment, use the MapFragment.newInstance(GoogleMapOptions options) static factory method to construct the fragment and pass in your custom configured options.

● If you are using a MapView, use the MapView(Context, GoogleMapOptions) constructor and pass in your custom configured options.

Page 12: Day 13: Google Maps Android API V2  Part 2

Google Maps API V2Add a Map to App

Set Properties in XML Attributes