2

I have a listview of addresses in screen#1. On click of any item navigating to next screen which shows the map of clicked address. Now i want to swipe the screen to view the map of next address. Here is my code..

This is my 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" />

This is my 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;   
} 
 }

{ Reference : https://github.com/ysamlan/horizontalpager/tree/master/src/com/github/ysamlan/horizontalpager }

App is immediately force closing on click of any listview item..

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

Where iam going wrong? Everything works fine in following two cases.. 1. if i iterate the for loop only one time. 2. if i remove map view related lines from xml, activity files.

How can i have multiple maps in a single MapActivity with swiping?. Please help me

2 Answers 2

2

You can have only one Mapview per map activity, however, you can have multiple map activities in one application, which in your case wouldnt be a very ideal solution.

What you should do instead of having a totally new mapview for an address, you could have a different set of overlays for them. This way you'll only need to change the set of overlays when you want to view map details for another address. Besides being possible to make this with Android, its much more optimal than having multiple mapviews.

Sign up to request clarification or add additional context in comments.

4 Comments

Could you please tell me how can i have different set of overlays for mapviews? please provide the code/link.
I dont have any code for it, youve got to figure that out yourself. overlays can be added and removed from the map dynamically. All you need to do is to have separate arraylist of overlays for each address and replace them from the map dynamically.
Solved using google static maps... now iam using webview instead of google map view in my xml file. And inflating that webview in my activity and loading the required location by passing latt, longt values to google static maps... Here is the reference : developers.google.com/maps/documentation/staticmaps/…
That's a really good alternative. It does have its drawbacks in the sense that the user wont be able to scroll around and see places nearby (if they are out of the map), but I guess in your case this should be good.
0

So far I know Currently Android supports only one MapView per MapActivity.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.