Skip to content

Instantly share code, notes, and snippets.

@joshdholtz
Last active December 22, 2022 09:41
Show Gist options
  • Select an option

  • Save joshdholtz/4522551 to your computer and use it in GitHub Desktop.

Select an option

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

Copy link
Copy Markdown

thanks! very useful

@saravanarajan

Copy link
Copy Markdown

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

@fnk0

fnk0 commented Feb 11, 2015

Copy link
Copy Markdown

This gist saved me so much frustration! Thank you!

@DimpyLadva

Copy link
Copy Markdown

Great ...thanks a lot for this help

@msaleem87

Copy link
Copy Markdown

Greate ... thank you for this help.

@igofed

igofed commented Mar 20, 2015

Copy link
Copy Markdown

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
Copy Markdown

very nice..... Thank you

@reixa00

reixa00 commented Jun 19, 2015

Copy link
Copy Markdown

Nice example ;)

@positivelymade

Copy link
Copy Markdown

Thanks.

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

@riyase

riyase commented Jul 2, 2015

Copy link
Copy Markdown

@igofed thanks for the point

@Ricardo1980

Copy link
Copy Markdown

Thanks!

Can I call super before the map?

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

@yomigits

Copy link
Copy Markdown

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
Copy Markdown

Awesome, thanks! :DD
My hero! XD

@Paulpeter

Copy link
Copy Markdown

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

@al3xpisani

Copy link
Copy Markdown

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
Copy Markdown

Thanks mate ;)

@roger35972134

Copy link
Copy Markdown

Thanks a lot :)

@Atwa

Atwa commented Mar 4, 2016

Copy link
Copy Markdown

Thnx Dude , you made my day ^^

@repl-krishna-g

Copy link
Copy Markdown

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

@bouchtaoui-dev

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

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

ghost commented Nov 7, 2016

Copy link
Copy Markdown

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
Copy Markdown

Thankyou its very usefull...

@TitikshaDaga

Copy link
Copy Markdown

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

@yamenr

yamenr commented Mar 15, 2018

Copy link
Copy Markdown

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

gryphon2411 commented Jun 10, 2018

Copy link
Copy Markdown

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
Copy Markdown

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

@mohkhz2001

Copy link
Copy Markdown

very useful thank you

@EngNazarHussain

Copy link
Copy Markdown

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