In this sample application we wills see
how to display a MapView in an android application. Of course we are
going to use Google Map API library for that.
So let us start:
1.Create
a new project File
->New -> Project ->Android ->Android Application Project.
While creating the new project, name the application and project name
as MapViewApplication
,
package name as you like, set the Build SDK to Google
APIs (Google Inc.) (API8) and
the Build Target to API8:Android
2.2(Froyo) -If
you do not have these AVDs or Platforms please use SDK Manager or AVD
Manager to download and install them.
2.
Let the Activity name be MainActivity(MainActivity.java)
and layout name be activity_main(activity_main.xml).
3.
Now let us design the layout for our Activity .Note that while
including the MapView tag in xml it is necessary to mention the full
library path(com.google.android.maps.MapView)
since it is not android defined and belongs to google. Also note that
the 'android:apikey'
attibute
should have a valid key which should be obtained by registering a
Google Map API Key Refer (How to obtain Google Map API Key for Android)
to know how to obtain a google map API key
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.google.android.maps.MapView android:id="@+id/mapview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey="Your API Key here" android:clickable="true" /> </RelativeLayout>4.Now let us use this layout in the MainActivity . This activity will extend MapActivity and must implement isRouteDisplayed() method .Press Ctrl+Shift+O for missing imports after typing the code.
MapActivity
public class MainActivity extends MapActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MapView mapview = (MapView) findViewById(R.id.mapview); mapview.setBuiltInZoomControls(true); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } }5.Make the changes in AndroidManifest.xml file by including internet permission <uses-permission android:name="android.permission.INTERNET" /> and indicate the use of google map library <uses-library android:name="com.google.android.maps" />
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.aj.mapviewapplication" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <uses-library android:name="com.google.android.maps" /> <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>6.Run your Android Application by rightclicking project Run as → android project. If everything goes well you will be shown a map of North America which you can zoom in and drag around.
Output:
The
output of this example would be similar to the one as follows: