Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Android Google Maps V2 - MapView in XML
<LinearLayout xmlns:android=""
android:layout_height="fill_parent" >
< android:id="@+id/mapview"
android:layout_height="fill_parent" />
public class SomeFragment extends Fragment {
MapView mapView;
GoogleMap map;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.some_layout, container, false);
// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(;
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
try {
} catch (GooglePlayServicesNotAvailableException e) {
// Updates the location and zoom of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
return v;
public void onResume() {
public void onDestroy() {
public void onLowMemory() {
<manifest xmlns:android=""
android:versionName="1.0" >
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name=""/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.example.permission.MAPS_RECEIVE"/>
android:theme="@style/AppTheme" >
android:label="@string/app_name" >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Copy link

aminalzanki commented Dec 16, 2014

thanks! very useful

Copy link

saravanarajan commented Feb 10, 2015

Thanks dude... its very useful and working perfectly....

Copy link

fnk0 commented Feb 11, 2015

This gist saved me so much frustration! Thank you!

Copy link

DimpyLadva commented Mar 14, 2015

Great ...thanks a lot for this help

Copy link

msaleem87 commented Mar 15, 2015

Greate ... thank you for this help.

Copy link

igofed commented Mar 20, 2015

If someone want to use this code not inside fragment - you should add mapView.onResume(), or you will have no tiles before touch on map.

Copy link

mohsinraza commented Apr 27, 2015

very nice..... Thank you

Copy link

reixa00 commented Jun 19, 2015

Nice example ;)

Copy link

positivelymade commented Jun 23, 2015


Note that getMap() is now deprecated, use getMapAsync(this)

Copy link

riyase commented Jul 2, 2015

@igofed thanks for the point

Copy link

Ricardo1980 commented Jul 6, 2015


Can I call super before the map?

public void onResume() {

Copy link

yomigits commented Jul 25, 2015

This is great. Please how can I add sliding menu to this map ? want it to be a page of the side menu.

Copy link

AdrianAlcGon commented Aug 17, 2015

Awesome, thanks! :DD
My hero! XD

Copy link

Paulpeter commented Aug 30, 2015

Thank you so much, you have made my learnng a lot easier ...bless

Copy link

al3xpisani commented Sep 28, 2015

I have to touch at screen to get the map updated. Would you help me please with this issue ... thx

onResume did the trick.... :(

public void onResume() {

Copy link

kafukafienkov commented Dec 20, 2015

Thanks mate ;)

Copy link

roger35972134 commented Jan 28, 2016

Thanks a lot :)

Copy link

Atwa commented Mar 4, 2016

Thnx Dude , you made my day ^^

Copy link

repl-krishna-g commented Mar 29, 2016

Used the exact code inside Dialog. But not showing the map

Copy link

Nordin-010 commented Apr 8, 2016

I wasted a whole day, just because I didn't add onResume() :( This is crazy, but thnx to the owner of this gist, may Allah bless you.

Copy link

sankar07 commented Aug 29, 2016

Getting inflating error
android.view.InflateException: Binary XML file line #37: Binary XML file line #37: Error inflating class

Copy link

ravirajjak commented Sep 5, 2016

Have you tried for Marshmallow? I am getting error EGL BAD DISPLAY.

Copy link

ghost commented Nov 7, 2016

Thanks man. Instead of searching the location by lat long I want to search it by Name of place or institutions,How can I do it?Please help

Copy link

prasad-mattaparthi commented Jan 7, 2017

Thankyou its very usefull...

Copy link

TitikshaDaga commented Sep 4, 2017

How have you used this fragment? My MainActivity extends FlutterActivity so how should I proceed?

Copy link

yamenr commented Mar 15, 2018

When adding MapView to my XML I get the following error:

could not find dependency ""

What to do?

Copy link

Eido95 commented Jun 10, 2018

Thank you very much for this code.

Further code about MapView can be found at Google Maps Android Samples RawMapViewDemoActivity sample, and Maps SDK for Android Map Objects MapView guide.

After combining this gist code with the above mentioned links, I found it to work.

Copy link

raanaYavari commented Jun 11, 2019

Thank you for this code.
How can I find current location with mapview?

Copy link

mohkhz2001 commented Jun 7, 2021

very useful thank you

Copy link

EngNazarHussain commented Sep 12, 2022

how to get api key from google

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment