activity_map.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
MapActivity.java
public class MapActivity extends Activity {
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
//modify below according to your requirement
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
map.setMyLocationEnabled(true);
..
..
..
Add following in your manifest file:
<permission
android:name="com.yourpackage.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.yourpackage.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<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" />
<application
........ >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="your_api_key" />
Of course, you have to other manifest parameter like activity name and other.
Then you have to reference google play service library into your project.
Follow with this link to install google play service sdk Install the Google Play Services SDK
And then
Your Project--->Right Click-->Properites-->Android-->Add
Select google-play-services sdk that you import into your workspace using that i mention in above method
Finally, select your android sdk target to
Google API Version 8
or other Google API version according to your requirement.
Further if you have any confusion, you can follow with this android official link:Google Maps Android API v2
UPDATED CODE:
To make it work in lower version:
activity_map.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
MyMapActivity.java
FragmentManager fragmentManager = getSupportFragmentManager();
SupportMapFragment mapFragment = (SupportMapFragment)
fragmentManager.findFragmentById(R.id.map);
map = mapFragment.getMap();
Thanks !!