Importing Google Play Services into Eclipse
After downloading play services we need to import it to Eclipse which will be used as a library for our maps project.Importantly both Maps project and library project should be in same folder.
1. In Eclipse goto File ⇒ Import ⇒ Android ⇒ Existing Android Code Into Workspace
2. Click on Browse and select Google Play Services project from your android sdk folder. You can locate play services library project from
android-sdk-windows\extras\google\google_play_services\libproject\google-play-services_lib
3. Importantly while importing check Copy projects into workspace option as shown in the below image.
4. Above procedure need to follow in normal eclipse but in AndroidStudio it is little different procedure.
a) create libraries folder inside your project and copy all the google-play-service library project ( android-sdk-windows\extras\google\google_play_services\libproject\google-play-services_lib) inside that folder.
b) Now build.gradle file was not available for that library project, so then create that file with the following data.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
apply plugin: 'android-library'
Getting the Google Maps API key
1. Same as in maps v1 we need to generate SHA-1 fingerprint using java keytool. Open your terminal and execute the following command to generate SHA-1 fingerprint.
On Windows
keytool -list -v -keystore C:\Users\Pavan\.android\debug.keystore
-storepass android -keypass android
------------------------------------------------------------------------
Follow these links for more information :
http://blog-emildesign.rhcloud.com/?p=435
https://blog-emildesign.rhcloud.com/?p=403
http://developer.xamarin.com/guides/android/platform_features/maps_and_location/maps/part_2_-_maps_api/
-------------------------------------------------------------------------
|
On Linux or Mac OS
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
|
In the output you can see SHA 1 finger print like this :
4. Now select API Access on left side and on the right side click on Create new Android key…
5. It will popup a window asking the SHA1 and package name. Enter your SHA 1 and your android project package name separated by semicolon ; and click on create.
I have given like below
BE:03:E1:44:39:7B:E8:17:02:9F:7F:B7:98:82:EA:DF:84:D0:FB:6A;info.androidhive.googlemapsv2
|
And note down the API key which required later in our project.
For example In my case i got this:
Key for Android apps (with certificates)
API key:
AIzaSyASdlKHxMfx5hnwo6y9GvnVWpk8auY1J_o
Android apps:
C4:E4:12:27:43:C5:A3:63:14:53:9F:FE:73:EB:BF:AC:BC:1A:E1:A7;com.example.cartravels
Activated on: Jun 21, 2015 9:38 AM
Activated by: pavan.s*****@gmail.com – you
4. Creating new Project
After completing required configuration, It’s time to start our project.
After completing required configuration, It’s time to start our project.
1. In Eclipse create a new project by going to File ⇒ New ⇒ Android Application Project and fill required details. I kept my project name as Google Maps V2 and package name asinfo.androidhive.info
2. Now we need to use Google Play Services project as a library to use project. So right click on project and select properties. In the properties window on left side select Android. On the right you can see aAdd button under library section. Click it and select google play services project which we imported previously.
3. Add the Map Key in the manifest file. Open AndroidManifest.xml file and add the following code before tag. Replace the android:value with your map key which you got from google console.
< meta-data
android:name = "com.google.android.maps.v2.API_KEY"
android:value = "AIzaSyBZMlkOv4sj-M5JO9p6wksdax4TEjDVLgo" />
|
4. Google maps needs following permissions and features.
ACCESS_NETWORK_STATE – To check network state whether data can be downloaded or not
INTERNET – To check internet connection status
WRITE_EXTERNAL_STORAGE – To write to external storage as google maps store map data in external storage
ACCESS_COARSE_LOCATION – To determine user’s location using WiFi and mobile cell data
ACCESS_FINE_LOCATION – To determine user’s location using GPS
OpenGL ES V2 – Required for Google Maps V2
Finally my AndroidManifest.xml file looks like this (Replace the package name with your project package)
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.cartravels" android:versionCode="1" android:versionName="1.0" >
<permission android:name="com.example.cartravels.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
<uses-permission android:name="com.example.cartravels.permission.MAPS_RECEIVE" />
<uses-sdk android:minSdkVersion="8"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 --> <uses-feature android:glEsVersion="0x00020000" android:required="true" />
<application android:allowBackup="true" android:icon="@drawable/car_logo" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<activity android:name="com.example.cartravels.SplashScreen" android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.cartravels.HomeScreen" android:theme="@android:style/Theme.Black.NoTitleBar" />
<activity android:name="com.example.cartravels.MapsScreen" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyASdlKHxMfx5hnwo6y9GvnVWpk8auY1J_o" />
</application>
</manifest>
6. Add the following code in your Main Activity java (MapsScreen.java) class.
package com.example.cartravels;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
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 MapsScreen extends Activity{
LinearLayout redCarLayout , blueCarLayout , blackCarLayout;
Button blackCarBtn , blueCarBtn , redCarBtn;
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;
@Override protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps_screen);
getAllIds();
initialiseMaps();
}
private void getAllIds() {
redCarLayout=(LinearLayout)findViewById(R.id.redCarLayout);
blueCarLayout=(LinearLayout)findViewById(R.id.blueCarLayout);
blackCarLayout=(LinearLayout)findViewById(R.id.blackCarLayout);
blackCarBtn=(Button)findViewById(R.id.blackCarBtn);
blueCarBtn=(Button)findViewById(R.id.blueCarBtn);
redCarBtn=(Button)findViewById(R.id.redCarBtn);
}
private void initialiseMaps(){
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15. map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera. map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
Comments
Post a Comment