android - Android 中的 MapActivity 中的多个 map (通过滑动)

标签 android google-maps horizontal-scrolling

我在 screen#1 中有一个地址 ListView 。单击任何项​​目后,导航到下一个屏幕,其中显示单击地址的 map 。现在我想滑动屏幕查看下一个地址的 map 。这是我的代码..

这是我的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>

Activity :

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 }

单击任何 ListView 项目后,应用程序将立即强制关闭。

Caused by: java.lang.IllegalStateException: You are only allowed to have a single MapView in a MapActivity

我哪里出错了?在以下两种情况下一切正常。 1.如果我仅迭代 for 循环一次。 2.如果我从xml、 Activity 文件中删除 map View 相关行。

如何通过滑动在单个 MapActivity 中拥有多个 map ?请帮助我

最佳答案

每个 map Activity 只能有一个 map View ,但是,您可以有 multiple map activities in one application ,在你的情况下这不是一个非常理想的解决方案。

您应该做什么,而不是为某个地址使用全新的 map View ,您可以为它们使用一组不同的叠加层。这样,当您想要查看另一个地址的 map 详细信息时,只需更改叠加层集。除了可以用 Android 实现这一点之外,它比拥有多个 map View 更优化。

关于android - Android 中的 MapActivity 中的多个 map (通过滑动),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12362645/

相关文章:

angularjs - 谷歌地图不显示

javascript - 静态 map 在使用 wifi 时工作,但在使用蜂窝数据时不工作

android - 自动滚动到 Horizo​​ntalScrollView

javascript - 使用鼠标按下的持续时间进行滚动

html - 固定宽度的水平滚动条

android - Apache Http MultipartEntityBuilder 的 Maven 存储库

java - 从fragment添加到recyclerview并从recyclerview内部删除recyclerview

android - 如何在 Oracle MAF 的移动应用程序中创建 excel 文件

java - 如何在保留 Activity 中滚动功能的同时显示键盘?

android - 我可以在 Android 版 Google map v2 上使用 Routeboxer 库吗?