Page 65

Android Programming Cookbook

57 / 143

} } } }

Let’s take a closer look: private void initilizeMap() { googleMap = ((SupportMapFragment) getSupportFragmentManager(). ←findFragmentById(R.id.mapFragment)).getMap(); if (googleMap == null) { Toast.makeText(getApplicationContext(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show(); } (findViewById(R.id.mapFragment)).getViewTreeObserver(). ←addOnGlobalLayoutListener( new android.view.ViewTreeObserver.OnGlobalLayoutListener() {

←-

←-

@Override public void onGlobalLayout() { // gets called after layout has been done ←but before display // so we can get the height then hide the ←view if (android.os.Build.VERSION.SDK_INT >= 16) ←{ (findViewById(R.id.mapFragment)). ←getViewTreeObserver(). ←removeOnGlobalLayoutListener( ←this); } else { (findViewById(R.id.mapFragment)). ←getViewTreeObserver(). ←removeGlobalOnLayoutListener( ←this); } setCustomMarkerOnePosition(); setCustomMarkerTwoPosition(); // plotMarkers(markerList); } }); }

With this method initilizeMap(), we set up our map in the Fragment. We use SupportMapFragment in order to make our map compatible with versions older than the Android version 4.0. Also by using the OnGlobalLayoutListener that is called after layout has been done, but before display, in order to avoid making any changes to the map, before the map is fully set up in our layout. Otherwise an error that says, that map is not ready yet, may appear. public void initializeUiSettings() { googleMap.getUiSettings().setCompassEnabled(true); googleMap.getUiSettings().setRotateGesturesEnabled(false); googleMap.getUiSettings().setTiltGesturesEnabled(true); googleMap.getUiSettings().setZoomControlsEnabled(true); googleMap.getUiSettings().setMyLocationButtonEnabled(true); }

SKY MAGZ  

This is Yahya Christian School Magazine

Advertisement