Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps in middle area of an Activity

I am working with Android studio 2.1.2 . I have checked around and most of the questions either use older versions of Android studio and some old classes that don't apply to my situation.

From file > new Project > I used option Google Maps Activity. I have not changed any of the default code that are in it. Its XML has a Fragment. Now is there a way to bring in this activity as it is into another Activity by means of a Fragment? Basically I am building an app that shows a map in the middle portion of the screen. This middle portion I hope would be a Fragment. The bottom and top area has some components for the user to interact.

I am not trying to make the map come up in a new Activity but rather remain in the middle portion of a particualar Activity.

Or do I just add new UI components to the default Google Maps Activity.

At first I had an idea of creating a Fragment(with its own .java file, XML Layout)then placing the map code in there and taking it over to the Activity where I want it. What would be the best way to get this done? I appreciate any help.

like image 832
user3650467 Avatar asked Dec 19 '22 14:12

user3650467


1 Answers

I have realised that either way one can arrive at the same result, a map inside an Activity, alongside other UI Components. Whether Google Maps Activity is used or if an Empty Activity is created and map code implemented in it. My code is included below. I chose an Empty Activity (Android 2.1.2) and then brought in the map code into the .java file and placed the map fragment with tag in the XML Layout of the activity.

The error was that I had used

android:name="com.google.android.gms.maps.MapFragment"

in the XML Layout but declared and initialised mapFragment(the object instance) with SupportMapFragment. The mix up came while copying and pasting code from the Google Documentation. As I was trying to update my post here I spotted the error. The correct thing to do is if you are using MapFragment(API level 12 and over) use it all through. If you decide to use SupportMapFragment(below API level 12) use SupportMapFragment all through.

The code below is the correction. Just in case anyone has the same issue.

XML Layout (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myapp.MainActivity">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/teachers_link"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/parent_link"
            android:layout_marginLeft="20dp" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="340dp"
        android:orientation="vertical">
        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="135dp"
        android:layout_marginTop="12dp"
        android:orientation="vertical">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/search_term" />
        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:id="@+id/select_level"
            android:entries="@array/select_level"></Spinner>
        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/select_course"
            android:entries="@array/select_course"></Spinner>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_search" />
    </LinearLayout>


</LinearLayout>

</ScrollView>

</LinearLayout>

.java file (MainActivity.java)

package name;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
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 AppCompatActivity implements OnMapReadyCallback{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


@Override
public void onMapReady(GoogleMap map) {
    map.addMarker(new MarkerOptions()
            .position(new LatLng(0, 0))
            .title("Marker"));
}

}
like image 77
user3650467 Avatar answered Jan 04 '23 23:01

user3650467