How to add google map on navigation drawer activity? - java

Problem with this, This code is also not working..
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
mapView = (MapView)findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(37.7750, 122.4183))
.title("San Francisco")
.snippet("Population: 776733"));
}
});

I found a solution today.
Implement OnMapReadyCallback to MainActivity.
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener,
OnMapReadyCallback
{
// ...
}
Implement onMapReady(). Keep it empty.
#Override
public void onMapReady(GoogleMap googleMap)
{
}
In onCreate() method:
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
supportMapFragment.getMapAsync(this);

You are mixing up two processes. You have to change either in your xml or activity class code:
1) Either replace your map fragment in content_main.xml with the MapView (As Below):
<com.google.android.gms.maps.MapView
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
2) Or replace this:
mapView = (MapView)findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(37.7750, 122.4183))
.title("San Francisco")
.snippet("Population: 776733"));
}
});
With:
((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(37.7750, 122.4183))
.title("San Francisco")
.snippet("Population: 776733"));
}
}
In your MainActivity class.
In your existing code, You have added a map fragment to the layout but calling for a mapview from activity. That's why it is not working.
To get API key, go HERE and Generate a key.
Copy the key and paste it in manifest under application tag:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY"/>
Now the google map will show up.

first you have to create the relative_layout because we can't add anything in map fragment so we add image button and map fragment in relative_layout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/activity_maps"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer_drawer" />
</android.support.v4.widget.DrawerLayout>
here your drawer_layout
`<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/activity_maps"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer_drawer" />
</android.support.v4.widget.DrawerLayout>`
in MapActivity add implements method navigationView and then add this code below the getSupportedFragment
` DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);`
then write the onclick method on Imagebutton method which is ` public void
drawerclick(View view) {
drawer.openDrawer(Gravity.START);
enter code here
navigationView.setNavigationItemSelectedListener(this);
}`

Related

Add toolbar and drawer menu to Android Google Maps Activity

I have created a standard Google Maps Activity in my app in Android Studio and the map works as expected.
My Activity is called 'NativeMap' and then the activity starts I see a ToolBar/AppBar at the top of the activity with the text "NativeMap".
The layout file for the activity is standard and calls a maps fragment:
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NativeMap">
</androidx.fragment.app.FragmentContainerView>
As such, there is no toolbar defined in the XML for me to reference or customize...
The onCreate for this activity seems very simple as well, so again I can only assume that there is a lot of stuff included in the background.
// public class NativeMap extends FragmentActivity implements OnMapReadyCallback {
public class NativeMap extends AppCompatActivity implements OnMapReadyCallback {
private static final String TAG = NativeMap.class.getSimpleName();
private GoogleMap mMap;
private ActivityNativeMapBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityNativeMapBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
if (mapFragment != null) mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
(Note that I have switched this class to extend AppCompatActivity to get the bar to appear at the top, when it extends FragmentActivity no top bar appears at all.)
How can I access this toolbar or replace it with a standard toolbar.
I also want to add a drawer menu to this activity, but everything looks so abstracted via this fragment inclusion that I have no idea how to.
I've been Googling for about a week now and can't find anything that even seems to point me in the right direction with this.
I figured it out.
androidx.fragment.app.FragmentContainerView can only contain fragments, so the base container in the layout needed to be changed.
I changed it to a <androidx.drawerlayout.widget.DrawerLayout to allow the main menu drawer to be included. (called "_drawer" in the tree view below)
Inside the DrawerLayout I added a androidx.constraintlayout.widget.ConstraintLayout to contain the main layout components, including the map fragment and the toolbar. (The contraint layout is called "mainLayout" in the treeview below)
The toolbar was then added inside a com.google.android.material.appbar.AppBarLayout.
The map fragment was then added in a <fragment> tag below the AppBarLayout.
This works as expected.
The layout XML is now as follows:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/_drawer"
android:animateLayoutChanges="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/mainLayout"
android:animateLayoutChanges="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/top_logo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:contentDescription="#string/app_logo"
app:srcCompat="#drawable/ic_app_top_logo" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appBarLayout"
tools:context=".NativeMap" />
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:id="#+id/_nav_view"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#EEEEEE">
<include
android:id="#+id/drawerMenu"
layout="#layout/drawer_main" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>

Displaying ActionBar in PreferenceScreen looks messed up

I'm trying to display an ActionBar in my PreferenceScreen. In my MainActivity it works totally fine, but in my PreferenceScreen it lokks messed up, because the title of the actinBar is in the middle of the screen and the layout is placed over the actionBar (see screenshot).
SettingsActivity.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings_drawer, new SettingsFragment())
.commit();
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mTitle = getTitle();
mDrawerLayout = findViewById(R.id.settings_drawer);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(R.string.app_name);
supportInvalidateOptionsMenu();
}
};
mDrawerLayout.addDrawerListener(mDrawerToggle);
setNavigationViewListener();
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
public static class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
settings_activity.xml:
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/settings_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".SettingsActivity">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:visibility="visible" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
</androidx.drawerlayout.widget.DrawerLayout>
Don't replace your fragment directly with the DrawerLayout, instead use a FrameLayout inside the DrawerLayout that you can replace your fragment with.
Something along the following lines should work.
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/settings_drawer_base"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".SettingsActivity">
<!-- This LinearLayout represents the contents of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:visibility="visible" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="#+id/settings_drawer"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
</androidx.drawerlayout.widget.DrawerLayout>
P.S -
You'll need to reference your DrawerLayout with the new id I gave it.
mDrawerLayout = findViewById(R.id.settings_drawer_base);

Android Google Maps Fragment

I have created a new Android project with a Google Map activity defined as
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
The Layout is this
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.alewe.myapplication.MapsActivity" />
The problem is :
i have created another activity and im trying to add the google map fragment inside it.
Using the
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.firstLayout,this,this.getTag());
doesnt work because i cant cast FragmentActivity to Fragment.
How can i add the google maps fragment just created inside another activity ?
Thanks
Change your layout (activity_maps) like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.alewe.myapplication.MapsActivity"
android:id="#+id/main_view_map">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="10dp"
app:fabSize="mini"
app:srcCompat="#drawable/ic_refresh_black_24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp" />
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
And don't use
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.firstLayout,this,this.getTag());
this code.

NullPointerException SupportMapFragment.getMapAsync [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
Hi I am trying to create a app where I am will be displaying map in a fragment using SupportMapFragment but I am getting NullPointerException. Here is the full error code
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
at obs.sharemytable.fragment.Search.onCreateView(Search.java:49)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6918)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Search.java:
public class Search extends Fragment{
GoogleMap mGoogleMap;
public static Search newInstance() {
Search fragment = new Search();
return fragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.search_layout, null, false);
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map_container);
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mGoogleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
});
return view;
}
}
search_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<include
android:id="#+id/searchToolbar"
layout="#layout/toolbar" />
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.google.android.gms.maps.MapView>
</LinearLayout>
Can someone figure what mistake I have done here.
Try getting the FragmentManager from the activity and using it to get your map:
FragmentManager mFragmentManager = getActivity().getSupportFragmentManager();
final SupportMapFragment mapFragment = (SupportMapFragment) mFragmentManager
.findFragmentById(R.id.map_container);
SupportMapFragment mapFragment = ((SupportMapFragment) getActivity().fragmentManager.findFragmentById(R.id.location_map))
Thanks #MojioMS now its working for me. Here is the changes I made to work.
Change this search_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<include
android:id="#+id/searchToolbar"
layout="#layout/toolbar" />
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.google.android.gms.maps.MapView>
</LinearLayout>
to
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<include
android:id="#+id/searchToolbar"
layout="#layout/toolbar" />
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment">
</fragment>
</LinearLayout>
When you use delay after initializing SupportMapFragment then there will not null pointer exception.

Working with Map Fragment Activity

I'm trying to create a basic activity with a google map fragment. Right now I have this:
public class MainScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
// add the map fragment to the activity
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) { return; }
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, new FragmentGoogle()).commit();
}
}
}
public class FragmentGoogle extends android.support.v4.app.Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.map_google, container, false);
}
}
Which is producing this:
My question is: how can I interact with the fragment itself? Let's supposse I want to zoom in Sidney. Should I put the code in the MainScreen.class or in the Fragment.class? Which methods should I use? It's my first time working with fragments.
you don't need to create your own FragmentGoogle. You can use com.google.android.gms.maps.SupportMapFragment and controll it from you activity code.
in the layout:
<fragment 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"
android:id="#+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
and then in the Activity code:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney, Australia, and move the camera.
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
The code is taken from
this tutorial which is enough to start and use most of the features of the Google Maps Android API, just follow the steps :)
Lets say you have an empty activity in a new project.
do this :
creating premade android map activity
And then you ll get something like this :
your project structure
go to google maps api and do what they tell you to do in the (TODO)
Create an account and a project on google maps platform https://console.cloud.google.com/google/maps-apis/
Activate android development by clicking a button you ll have to look for it
and then go to key section to create new api key
go back to android studio and click the yellow xml file in the picture
past your key there
So by now the Map actvity should give you a real map and everything should work , you should see the map if you get to the map activity
but now what you want is to add that map inside your MainActivity .(i named it ActivityMain because MapActivity and MainActivity look the same )
Now all you need to do is copy past the fragment of the xml file activity_maps.xml without changing anything except for the id .
this is the activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map1"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
I tried adding a button here but i couldn't. So what i did is copy the fragment itself and put it inside the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".ActivityMain">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
</LinearLayout>
I think these are all steps to create the picture you have in your question.
I hope this was useful not painful to read .

Categories