【问题标题】:Multiple maps (with swiping) in a MapActivity in androidandroid中的MapActivity中的多个地图(带有滑动)
【发布时间】:2012-09-11 04:10:41
【问题描述】:

我在屏幕#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 中拥有多个地图?请帮帮我

【问题讨论】:

    标签: android google-maps horizontal-scrolling


    【解决方案1】:

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

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

    【讨论】:

    • 您能告诉我如何为地图视图设置不同的叠加层吗?请提供代码/链接。
    • 我没有任何代码,你必须自己弄清楚。可以动态地从地图中添加和删除叠加层。您需要做的就是为每个地址设置单独的叠加层列表,并从地图中动态替换它们。
    • 使用谷歌静态地图解决...现在我在我的 xml 文件中使用 webview 而不是谷歌地图视图。并在我的活动中膨胀该 webview 并通过将 latt、longt 值传递给谷歌静态地图来加载所需的位置......这是参考:developers.google.com/maps/documentation/staticmaps/…
    • 这是一个非常好的选择。从某种意义上说,它确实有其缺点,即用户无法滚动并查看附近的地方(如果它们不在地图上),但我想在你的情况下这应该是好的。
    【解决方案2】:

    到目前为止,我知道目前 Android 每个 MapActivity 仅支持一个 MapView。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      • 2012-12-20
      相关资源
      最近更新 更多