Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
051 Java Android Program to demonstrate Google Maps in Android
To use Google Maps you need to create a valid Google Maps API key. The key is free, you can use it with any of your applications that call the Maps API, and it supports an unlimited number of users.
You get this key via Google APIS Console. You have to provide your application signature key and the application package name.This is based on the key with which you sign your Android application during deployment.During development with Eclipse, Eclipse automatically creates and uses a debug key.
The Eclipse debug key for signing your application can be found in the C:/users/username/.android/debug.keystore file.
Now we need to create a SHA-1 for your debug key , to do so we would need to use the keytool command from your jdk installation , now to do open cmd and go to C:\ProgramFiles\Java\jdk_version\bin and type in the following command
keytool.exe -v -list -alias androiddebugkey -keystore "C:\users\username\.android\debug.
Now copy the SHA-1 output as we would need it later.
You have to register in the Google APIs Console that you want to use Google Maps for Android. You can reach this console via the following link: https://code.google.com/apis/console/ . Select here the Services entry.
Main Activity
package com.example.map; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.MapFragment; import com.google.android.gms.maps.model.BitmapDescriptorFactory; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; public class MainActivity extends Activity { private static final LatLng JLNStadium = new LatLng(28.590401000000000000, 77.233255999999980000); private GoogleMap map; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) .getMap(); Marker jln = map.addMarker(new MarkerOptions().position(JLNStadium) .title("JLN")); map.moveCamera(CameraUpdateFactory.newLatLngZoom(JLNStadium, 15)); map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
MainActivity.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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.MapFragment" /> </RelativeLayout>
AndroidManifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.map" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="17" /> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> <permission android:name="com.example.map.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.map.permission.MAPS_RECEIVE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <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" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.map.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="yourkeyvalue" /> </application> </manifest>