多个mapviews android v2

时间:2013-12-04 13:55:53

标签: android google-maps

我想要一个mapview,它显示缩小的地图和另一个mapview 它显示了放大的地图。 我可以用任何方式实现这个吗?

韩国社交协会

1 个答案:

答案 0 :(得分:0)

是的,你可以......

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="vertical"
    android:weightSum="2">

    <fragment
        android:id="@+id/map1"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:layout_weight="1"/>

  <fragment
        android:id="@+id/map2"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:layout_weight="1"/>

</LinearLayout>

在MainActivity.java中

公共类MainActivity扩展了Activity {

// Google Map
private GoogleMap googleMap1,googleMap2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
        // Loading map
        initilizeMap();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

/**
 * function to load map. If map is not created it will create it for you
 * */
private void initilizeMap() {
    if (googleMap1 == null) {
        googleMap1 = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map1)).getMap();

        // check if map is created successfully or not
        if (googleMap1 == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }

      if (googleMap2 == null) {
        googleMap2 = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map2)).getMap();

        // check if map is created successfully or not
        if (googleMap2 == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

@Override
protected void onResume() {
    super.onResume();
    initilizeMap();
}
 }