我在屏幕#1 中有一个地址列表视图。单击导航到下一个屏幕的任何项目,该屏幕显示点击地址的地图。现在我想滑动屏幕查看下一个地址的地图。这是我的代码..
这是我的 map_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayoutTopBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/scarlet"
android:gravity="center_vertical|center_horizontal" >
<TextView
android:id="@+id/textViewTopBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="Map"
android:textColor="@color/white"
android:textSize="20dp" />
</LinearLayout>
<com.xxx.android.yyyy.activities.HorizontalPager
android:id="@+id/horizontal_pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</com.xxx.android.yyyy.activities.HorizontalPager>
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:background="@null"
android:scaleType="fitCenter"
android:src="@drawable/button123" />
这是我的 map.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:apiKey="xxxxxxxxxxxxxxxxxxxxx"
android:clickable="true" />
</LinearLayout>
活动 :
public class MyMapActivity extends MapActivity
{
private MapView mapView;
private Drawable mapMarker;
private MyLocationOverlay myMap;
private MapController mapController;
private List<Overlay> overlayList;
private HorizontalPager hPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_screen);
hPager = (HorizontalPager) findViewById(R.id.horizontal_pager);
View inflatedView;
for (int i = 0; i < listViewSize; i++) //// getting listviewSize from intent extras
{
inflatedView = (View) View.inflate(MyMapActivity.this, R.layout.map, null);
mapView = (MapView) inflatedView.findViewById(R.id.mapview);
showMap(latt, longt); //getting all the latt, longt values of addresses from previous activity
hPager.addView(inflatedView);
}
hPager.setCurrentScreen(selectedAddressIndexInListView, false); //<<---- intent extras
}
void showMap(double latitude , double longitude )
{
mapView.setBuiltInZoomControls(true);
mapMarker = getResources().getDrawable(R.drawable.map_marker);
overlayList = mapView.getOverlays();
myMap = new MyLocationOverlay(this, mapView);
myMap.enableMyLocation();
mapController = mapView.getController();
GeoPoint geoPoint = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
mapController.animateTo(geoPoint);
mapController.setZoom(12);
OverlayItem overlayItem = new OverlayItem(geoPoint,"xxx", "yyy");
CustomPinpoint customPinpoint = new CustomPinpoint(mapMarker,MyMapActivity.this);
customPinpoint.insertPinpoint(overlayItem);
overlayList.clear();
overlayList.add(myMap);
overlayList.add(customPinpoint);
}
@Override
protected void onResume() {
super.onResume();
myMap.enableCompass();
myMap.enableMyLocation();
}
@Override
protected void onPause() {
super.onPause();
myMap.disableCompass();
myMap.disableMyLocation();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
{ 参考:https ://github.com/ysamlan/horizontalpager/tree/master/src/com/github/ysamlan/horizontalpager }
单击任何列表视图项目时,应用程序会立即强制关闭。
Caused by: java.lang.IllegalStateException: You are only allowed to have a single MapView in a MapActivity
我哪里出错了?在以下两种情况下一切正常.. 1. 如果我只迭代一次 for 循环。2. 如果我从 xml、活动文件中删除与地图视图相关的行。
如何通过滑动在单个 MapActivity 中拥有多个地图?请帮我