ForceFieldsForDoors
ForceFieldsForDoors

Reputation: 193

How to embed Google Maps into an activity?

I am currently trying to embed a Google Map into a custom View.

The only thing that the tutorial shows is how to make a full map xml and not a 'windowed' version.

Can anyone help me?

Upvotes: 2

Views: 2116

Answers (3)

dsc clg
dsc clg

Reputation: 31

<fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         /> 

past in your activity xml file and change the id of fragment ex:android:id="@+id/map1"

then

goto your java file and past after setcontent in on create

MapFragment  mMapFragment = MapFragment.newInstance();
        FragmentTransaction fragmentTransaction =
                getFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.map1, mMapFragment);
        fragmentTransaction.commit();
        mMapFragment.getMapAsync(this);

implements onMapReadyCallBack method and overide in the activity

Note:-this work only if you added Google map in application

Upvotes: -1

user35603
user35603

Reputation: 795

Try this in your xml:

<!-- Map -->
<FrameLayout
    android:id="@+id/map_container"
    android:layout_width="match_parent"
    android:layout_height="200dp"">
</FrameLayout>

And in your code:

// This method adds map fragment to the container.
private void addMapFragment() {
    SupportMapFragment mMapFragment = SupportMapFragment.newInstance();
    mMapFragment.getMapAsync(this);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.map_container, mMapFragment)
            .commit();
}

Also use getChildFragmentManager() instead of getSupportFragmentManager() if you are inside a fragment.

Upvotes: 3

mattfred
mattfred

Reputation: 2749

Inside your custom view xml file, try changing the size of the map fragment. In the example below I set height and width to 30dp.

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />

Upvotes: 2

Related Questions