الخميس، 13 يونيو 2013

Android Multiple Map Views On The Same View - Google Maps v2

Starting from Android maps v1, I am trying to use more than one map in the same view but was no hope,
until Google published Android maps v2, I realized that Google must have handled this problem and made it more flexible but also faced a lot of problems, and finally I got it working, Thanks Allah :)

Firstly, you have to add reference to "google_play_services" project into yours using this link http://developer.android.com/google/play-services/setup.html

then change the api key at this manifest to get maps:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.saadroid.multiplemaps"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <permission
        android:name="com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="your_apiKey" />
    </application>

</manifest> 


After that, copy this code into your main activity:
package com.saadroid.multiplemaps;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends FragmentActivity {
private static final String MAP_FRAGMENT_TAG = "map2";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// It isn't possible to set a fragment's id programmatically so we set a
// tag instead and
// search for it using that.
for (int i = 1; i < 4; i++) {
SupportMapFragment mMapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentByTag(MAP_FRAGMENT_TAG);
FrameLayout ff = new FrameLayout(this);
ff.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.FILL_PARENT, getResources()
.getDisplayMetrics().heightPixels / 3));
ff.setId(i * 222);
((LinearLayout) findViewById(R.id.mapCont)).addView(ff);
// We only create a fragment if it doesn't already exist.
if (mMapFragment == null) {
// To programmatically add the map, we first create a
// SupportMapFragment.
mMapFragment = new SupportMapFragment() {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
GoogleMap map = getMap();
System.out.println("%%  " + map);
if (map != null) {
GoogleMap mMap = map;
if (mMap != null) {
mMap.addMarker(new MarkerOptions().position(
new LatLng(0, 0)).title("Marker"));
getView().findViewById(0x1).setVisibility(
View.INVISIBLE);
}
}
}
};

// Then we add it using a FragmentTransaction.
FragmentTransaction fragmentTransaction = getSupportFragmentManager()
.beginTransaction();
fragmentTransaction.add(ff.getId(), mMapFragment,
MAP_FRAGMENT_TAG);
fragmentTransaction.commit();
}

}
}

}

Finally, the layout xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/mapCont"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical" >
    </LinearLayout>

</RelativeLayout>


Happy Coding!

هناك تعليق واحد: