Skip to content

Instantly share code, notes, and snippets.

@joshdholtz
Last active December 22, 2022 09:41
Show Gist options
  • Save joshdholtz/4522551 to your computer and use it in GitHub Desktop.
Save joshdholtz/4522551 to your computer and use it in GitHub Desktop.
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>
@DimpyLadva
Copy link

Great ...thanks a lot for this help

@msaleem87
Copy link

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

very nice..... Thank you

@reixa00
Copy link

reixa00 commented Jun 19, 2015

Nice example ;)

@positivelymade
Copy link

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

Thanks!

Can I call super before the map?

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

@yomigits
Copy link

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

Awesome, thanks! :DD
My hero! XD

@Paulpeter
Copy link

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

@al3xpisani
Copy link

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

Thanks mate ;)

@roger35972134
Copy link

Thanks a lot :)

@Atwa
Copy link

Atwa commented Mar 4, 2016

Thnx Dude , you made my day ^^

@repl-krishna-g
Copy link

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

@bouchtaoui-dev
Copy link

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

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

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

Thankyou its very usefull...

@TitikshaDaga
Copy link

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?

@gryphon2411
Copy link

gryphon2411 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

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

@mohkhz2001
Copy link

very useful thank you

@EngNazarHussain
Copy link

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