初始化地图片段

定义应用程序的基本布局并获取必要的权限后,最后一步是初始化 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");
        }
      }
    });
  }
}