初始化地圖片段

定義應用程式的基本佈局並獲取必要的許可權後,最後一步是初始化 MapFragment 類的例項,從而建立 Map 並將其與 activity_main.xml 檔案中宣告的 MapFragment 相關聯。

public class BasicMapActivity extends Activity {

  // map embedded in the map fragment
  private Map map = null;

  // map fragment embedded in this activity
  private MapFragment mapFragment = null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initialize();
  }

  private void initialize() {
    setContentView(R.layout.activity_main);

    mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.mapfragment);
    mapFragment.init(new OnEngineInitListener() {
      @Override
      public void onEngineInitializationCompleted(OnEngineInitListener.Error error)
      {
        if (error == OnEngineInitListener.Error.NONE) {
          map = mapFragment.getMap();  
          map.setCenter(new GeoCoordinate(49.196261, -123.004773, 0.0),
                  Map.Animation.NONE);  
          map.setZoomLevel(
            (map.getMaxZoomLevel() + map.getMinZoomLevel()) / 2);
        } else {
          System.out.println("ERROR: Cannot initialize Map Fragment");
        }
      }
    });
  }
}