Download Code
For beginners, It is headache to integrate Google map on their android app. You can find many post regarding Google map and of course at the one point you will confused and try to pull your hair ;)
Here, is the full step by step tutorial to integrate Google maps in your android app. Take a look.
1. Integrate "google_play_services_lib" to your project if you are using Eclipse or You can select "Google Maps Activity" on create project in Android Studio.
2. After creating project, You need to fetch SHA key for your system. It is unique and will use to generate Google Hash Key. Most of the people do mistake in this point but trust me, It is very simple. I have give steps with screenshots.
3. For SHA1:
- If you are using Eclipse you can get it from below Path:
Windows > Preferences > Android > Build
- OR With Command Prompt:
"keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android "
4. Now you have your SHA key, You need to use it for generating new Google Hash Key.
- Go to Google API Console, where you can manage your projects which uses/will use Google services. (https://code.google.com/apis/console)
- Create New project named "Google Map Demo". After successfully project creation, Go to : APIs & Outh > APIs You will see screen as like below screenshot.
- Enable "Google Maps Android API":
- Now, Go to Credentials and Click on "Create New Key" button:
- Click "Android Key", add SHA1;PACKAGE_NAME in below dialog and hit Create:
Like, 12:34:56:78:90:12:34:56:78:90:A3:16:65:0B:F0:6B:F4:EC:2B:83;demo.map.com.googlemapdemo
This Key will use in your project. Lets, hit it and make project with this key.
Project (Google Map Demo)
Here is the file wise code which you can use:
- AndroidMenifest.xml (Paste Key which you generated above to "GOOGLE_MAP_KEY")
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="demo.map.com.googlemapdemo" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<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" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="GOOGLE_MAP_KEY" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_maps.xml
<fragment 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:id="@+id/map" tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
- MapsActivity.Java
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("0,0"));
}
}
Done, Now You can run project and get the output as shown first image of Post.
amazing
ReplyDelete