<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:orientation="horizontal" >
<fragment
android:id="@+id/map1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="1"
class="com.baidu.mapapi.map.SupportMapFragment" />
<fragment
android:id="@+id/map2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.baidu.mapapi.map.SupportMapFragment" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<fragment
android:id="@+id/map3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="1"
class="com.baidu.mapapi.map.SupportMapFragment" />
<fragment
android:id="@+id/map4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.baidu.mapapi.map.SupportMapFragment" />
</LinearLayout>
</LinearLayout>
/**
* 在一个Activity中创建多个地图展示
*/
public class MultiMapViewDemo extends FragmentActivity {
private static final LatLng GEO_BEIJING = new LatLng(39.945, 116.404);
private static final LatLng GEO_SHANGHAI = new LatLng(31.227, 121.481);
private static final LatLng GEO_GUANGZHOU = new LatLng(23.155, 113.264);
private static final LatLng GEO_SHENGZHENG = new LatLng(22.560, 114.064);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multimap);
initMap();
}
/**
* 初始化Map
*/
private void initMap() {
/**
* MapStatusUpdate:描述地图状态将要发生的变化
* newLatLng(LatLng latLng):设置地图新中心点
* */
MapStatusUpdate u1 = MapStatusUpdateFactory.newLatLng(GEO_BEIJING);
SupportMapFragment map1 = (SupportMapFragment) (getSupportFragmentManager()
.findFragmentById(R.id.map1));
/**
* getBaiduMap():获取百度地图控制器,当执行完 onCreateView函数之后调用,否则返回 null.
* BaiduMap.setMapStatus(MapStatusUpdate update):改变地图状态
* */
map1.getBaiduMap().setMapStatus(u1);
MapStatusUpdate u2 = MapStatusUpdateFactory.newLatLng(GEO_SHANGHAI);
SupportMapFragment map2 = (SupportMapFragment) (getSupportFragmentManager()
.findFragmentById(R.id.map2));
map2.getBaiduMap().setMapStatus(u2);
MapStatusUpdate u3 = MapStatusUpdateFactory.newLatLng(GEO_GUANGZHOU);
SupportMapFragment map3 = (SupportMapFragment) (getSupportFragmentManager()
.findFragmentById(R.id.map3));
map3.getBaiduMap().setMapStatus(u3);
MapStatusUpdate u4 = MapStatusUpdateFactory.newLatLng(GEO_SHENGZHENG);
SupportMapFragment map4 = (SupportMapFragment) (getSupportFragmentManager()
.findFragmentById(R.id.map4));
map4.getBaiduMap().setMapStatus(u4);
}
}