繼2013年10月1日在6:05創建這段源代碼的評論。
我的實現與MapFragment更換SupportMapFragment,支持谷歌地圖版本2
我想再次記住:
一個GoogleMap的只能使用的GetMap()當底層的地圖系統是被收購加載並且片段中的底層視圖存在。這個類自動初始化地圖系統和視圖;但無法保證它準備就緒,因爲這取決於Google Play服務APK的可用性。如果GoogleMap不可用,則getMap()將返回null。
我的實現:
AndroidManifest.xml中
<!-- Permissions -->
<!-- Used by the Google Maps API to download map tiles from Google Maps servers. -->
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<!-- Allows the Google Maps API to check the connection status in order to determine whether data can be downloaded. -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!-- Allows the Google Maps API to cache map tile data in the device's external storage area. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- Allows the Google Maps API to use WiFi or mobile cell data (or both) to determine the device's location. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<!-- Allows the Google Maps API to use the Global Positioning System (GPS) to determine the device's location to within a very small area. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- Permissions -->
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<!--
The Google Maps Android API uses OpenGL ES version 2 to render the map.
If OpenGL ES version 2 is not installed, your map will not appear.
sWe recommend that you add the following <uses-feature> element as a child of the <manifest> element in AndroidManifest.xml:
-->
<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" >
<!-- Google Play Services -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!-- Goolge Maps API Key -->
<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">
<!-- Google Map Container -->
<RelativeLayout
android:id="@+id/google_map_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- Google Map Container -->
</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());
}
/**
* Initialize default Google Maps Options for our Application
* @return GoogleMapOptions
*/
public GoogleMapOptions initializeGoogleMapsOptions() {
GoogleMapOptions googleMapOptions = new GoogleMapOptions()
.mapType(GoogleMap.MAP_TYPE_HYBRID);
return googleMapOptions;
}
}
MainActivity.java
public class MainActivity extends Activity implements ApplicationMapFragment.MapCallback {
// Get Class Name
private static String TAG = MainActivity.class.getName();
// Create a new Map fragment
private ApplicationMapFragment mapFragment;
// Google Map Fragment Name
private static String MAP_FRAGMENT_TAG = "google_maps_fragment";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
initilizeMapFragment();
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "Google Maps can't be loaded", e);
}
}
/**
* Initialize a new Map Fragment
*/
private void initilizeMapFragment() {
// Try to get Map Fragment
mapFragment = (ApplicationMapFragment) getFragmentManager()
.findFragmentByTag(MAP_FRAGMENT_TAG);
// We only create a fragment if it doesn't already exist.
if (mapFragment == null) {
mapFragment = new ApplicationMapFragment();
mapFragment.initializeGoogleMapsOptions();
// This activity will receive the Map object once the map fragment is fully loaded
mapFragment.setMapCallback(this);
// Then we add it using a FragmentTransaction.
FragmentTransaction fragmentTransaction =
getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.google_map_container, mapFragment, MAP_FRAGMENT_TAG);
fragmentTransaction.commit();
} else {
// This activity will receive the Map object once the map fragment is fully loaded
mapFragment.setMapCallback(this);
}
}
@Override
public void onMapReady(GoogleMap map) {
Log.d(TAG, "Google Map is loaded");
MarkerOptions marker = new MarkerOptions()
.position(new LatLng(10, 10))
.title("Hello World");
map.addMarker(marker);
}
}
它應該是這樣的MMAP = mMapFragment.getMap() –
對不起,先生,這是打錯了這裏,但我有寫代碼 –
正確@RanaRaman我對你有幫助,只是想分析一下。你能擴展'SupportMapFragment'嗎?在'onCreate()'中放入一個'Log'。在'mMap = mMapFragment.getMap()'之前放一個日誌。讓我知道「日誌」被首先調用。 – Geros