Following @ Glenn-- from October 1 to October 13 at 6:05 to create this piece of source code.
My implementation replaces SupportMapFragment with MapFragment and supports Google Maps Version 2
I want to remember again:
GoogleMap can only be obtained using getMap () when the base map system is loaded and there is a basic view in the fragment. This class automatically initializes the map system and view; however, you cannot be sure when it will be ready, because it depends on the availability of the Google Play APK services. If GoogleMap is not available, getMap () will return null.
My implementation:
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-feature android:glEsVersion="0x00020000" android:required="true"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyATC4WBLLewjdwYDFVTnJH8hA18gG_GgvY" /> </application>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="mapa.bg.MapaMainActivity" android:background="#ccc"> <RelativeLayout android:id="@+id/google_map_container" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>
ApplicationMapFragment.java
public class ApplicationMapFragment extends MapFragment { private MapCallback callback; public void setMapCallback(MapCallback callback) { this.callback = callback; } public static interface MapCallback { public void onMapReady(GoogleMap map); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if(callback != null) callback.onMapReady(getMap()); } public GoogleMapOptions initializeGoogleMapsOptions() { GoogleMapOptions googleMapOptions = new GoogleMapOptions() .mapType(GoogleMap.MAP_TYPE_HYBRID); return googleMapOptions; } }
MainActivity.java
public class MainActivity extends Activity implements ApplicationMapFragment.MapCallback {
source share