Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android Google Maps V2 - MapView in XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.gms.maps.MapView android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
public class SomeFragment extends Fragment {
MapView mapView;
GoogleMap map;
@Override
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(R.id.mapview);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
try {
MapsInitializer.initialize(this.getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
// Updates the location and zoom of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
map.animateCamera(cameraUpdate);
return v;
}
@Override
public void onResume() {
mapView.onResume();
super.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<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.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<permission
android:name="com.example.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.permission.MAPS_RECEIVE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="your_key"/>
<activity
android:name=".HomeActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
@fahadishaque35
Copy link

fahadishaque35 commented Dec 2, 2014

Keep in mind mapView.onResume() draws the map else user would only see a grid box.

@aminalzanki
Copy link

aminalzanki commented Dec 16, 2014

thanks! very useful

@saravanarajan
Copy link

saravanarajan commented Feb 10, 2015

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

@fnk0
Copy link

fnk0 commented Feb 11, 2015

This gist saved me so much frustration! Thank you!

@DimpyLadva
Copy link

DimpyLadva commented Mar 14, 2015

Great ...thanks a lot for this help

@msaleem87
Copy link

msaleem87 commented Mar 15, 2015

Greate ... thank you for this help.

@igofed
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.

@mohsinraza
Copy link

mohsinraza commented Apr 27, 2015

very nice..... Thank you

@axierjhtjz
Copy link

axierjhtjz commented Jun 19, 2015

Nice example ;)

@positivelymade
Copy link

positivelymade commented Jun 23, 2015

Thanks.

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

@riyase
Copy link

riyase commented Jul 2, 2015

@igofed thanks for the point

@Ricardo1980
Copy link

Ricardo1980 commented Jul 6, 2015

Thanks!

Can I call super before the map?

public void onResume() {
super.onResume();
mapView.onResume();
}

@yomigits
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.

@AdrianAlcGon
Copy link

AdrianAlcGon commented Aug 17, 2015

Awesome, thanks! :DD
My hero! XD

@Paulpeter
Copy link

Paulpeter commented Aug 30, 2015

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

@al3xpisani
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.... :(

@Override
public void onResume() {
    mapView.onResume();
    super.onResume();
}

@kafukafienkov
Copy link

kafukafienkov commented Dec 20, 2015

Thanks mate ;)

@roger35972134
Copy link

roger35972134 commented Jan 28, 2016

Thanks a lot :)

@Atwa
Copy link

Atwa commented Mar 4, 2016

Thnx Dude , you made my day ^^

@repl-krishna-g
Copy link

repl-krishna-g commented Mar 29, 2016

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

@Nordin-010
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.

@sankar07
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 com.google.android.gms.maps.MapView

@ravirajjak
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

@prasad-mattaparthi
Copy link

prasad-mattaparthi commented Jan 7, 2017

Thankyou its very usefull...

@TitikshaDaga
Copy link

TitikshaDaga commented Sep 4, 2017

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

@yamenr
Copy link

yamenr commented Mar 15, 2018

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

could not find dependency "com.google.android.gms:play-services-maps:11.8.0"

What to do?

@Eido95
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.

@raanaYavari
Copy link

raanaYavari commented Jun 11, 2019

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

@mohkhz2001
Copy link

mohkhz2001 commented Jun 7, 2021

very useful thank you

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