Wednesday 29 May 2013

GooglemapIntegrationV3

1) WebMapActivity.java
====================
package com.example.googleappandroidv3;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.Criteria;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebMapActivity extends Activity implements LocationListener {
 
  private static final String MAP_URL = "http://gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/simple-android-map.html";
  private WebView webView;
  private Location mostRecentLocation;

  @Override
  /** Called when the activity is first created. */
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getLocation();
    setupWebView();
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

  }
  /** Sets up the WebView object and loads the URL of the page **/
  private void setupWebView(){
    final String centerURL = "javascript:centerAt(" +
    mostRecentLocation.getLatitude() + "," +
    mostRecentLocation.getLongitude()+ ")";
    webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    //Wait for the page to load then send the location information
    webView.setWebViewClient(new WebViewClient(){ 
      @Override 
      public void onPageFinished(WebView view, String url) 
      {
        webView.loadUrl(centerURL);
      }

    });
    webView.loadUrl(MAP_URL); 




  }




  /** The Location Manager manages location providers. This code searches
        for the best provider of data (GPS, WiFi/cell phone tower lookup,
        some other mechanism) and finds the last known location.
   **/
  private void getLocation() {     
    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = locationManager.getBestProvider(criteria,true);

    //In order to make sure the device is getting location, request updates.       
   // locationManager.requestLocationUpdates(provider, 1, 0, this);
    mostRecentLocation = locationManager.getLastKnownLocation(provider);
  }

  /** Sets the mostRecentLocation object to the current location of the device **/
  @Override
  public void onLocationChanged(Location location) {
    mostRecentLocation = location;
  }

  /** The following methods are only necessary because WebMapActivity implements LocationListener **/
  @Override
  public void onProviderDisabled(String provider) {
  }

  @Override
  public void onProviderEnabled(String provider) {
  }

  @Override
  public void onStatusChanged(String provider, int status, Bundle extras) {
  }

}
2) WebMapActivityJSInterface.java
========================
package com.example.googleappandroidv3;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.Criteria;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.Criteria;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebMapActivityJSInterface extends Activity implements LocationListener {
  private static final String MAP_URL = "http://gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/simple-android-map.html";
  private WebView webView;
  private Location mostRecentLocation;

  @Override
  /** Called when the activity is first created. */
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getLocation();
    setupWebView();
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

  }
  /** Sets up the WebView object and loads the URL of the page **/
  private void setupWebView(){

    webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl(MAP_URL); 

    /** Allows JavaScript calls to access application resources **/
    webView.addJavascriptInterface(new JavaScriptInterface(), "android");

  }

  /** Sets up the interface for getting access to Latitude and Longitude data from device **/
  private class JavaScriptInterface {
    public double getLatitude(){
      return mostRecentLocation.getLatitude();
    }
    public double getLongitude(){
      return mostRecentLocation.getLongitude();
    }

  }

  /** The Location Manager manages location providers. This code searches
        for the best provider of data (GPS, WiFi/cell phone tower lookup,
        some other mechanism) and finds the last known location.
   **/
  private void getLocation() {     
    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = locationManager.getBestProvider(criteria,true);

    //In order to make sure the device is getting location, request updates.       
    locationManager.requestLocationUpdates(provider, 1, 0, this);
    mostRecentLocation = locationManager.getLastKnownLocation(provider);
  }

  /** Sets the mostRecentLocation object to the current location of the device **/
  @Override
  public void onLocationChanged(Location location) {
    mostRecentLocation = location;
  }

  /** The following methods are only necessary because WebMapActivity implements LocationListener **/
  @Override
  public void onProviderDisabled(String provider) {
  }

  @Override
  public void onProviderEnabled(String provider) {
  }

  @Override
  public void onStatusChanged(String provider, int status, Bundle extras) {
  }

}

3)manifest.xml
================

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.googleappandroidv3"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="WebMapActivity"
            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>
res
===
layout
main.xml
============ 
==========
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  <WebView android:id="@+id/webview"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"/>
</LinearLayout>
=============================
  Steps for generate api key
=============================
googlemap api key is
==================================================
  07ohfV6aNzCx7llNFaJJFZ_-9BUhZB2OqjHBlfg

Put it in main.xml
==================
  <com.google.android.maps.MapView
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:apiKey="07ohfV6aNzCx7llNFaJJFZ_-9BUhZB2OqjHBlfg"
                 />  

terminal code for find api key
============================================
user2@user2-desktop:~$ cd /home/user2/.android
user2@user2-desktop:~/.android$ keytool -list -alias androiddebugkey -keystore debug.keystore
Enter keystore password:  
androiddebugkey, 1 Jan, 2011, PrivateKeyEntry, 
Certificate fingerprint (MD5): 30:D1:6C:1A:5E:51:17:AC:90:F8:FB:88:88:E7:D7:3F
user2@user2-desktop:~/.android$ ^C
user2@user2-desktop:~/.android$ ^C
user2@user2-desktop:~/.android$ ^C
user2@user2-desktop:~/.android$ ^C
user2@user2-desktop:~/.android$ 




How to find google map api key for v1
=====================================
first search the file debug.keystore
    
  to search debug.keystore file go to window -->preferences-->


 Android(click)
  Build(click)
     default debuge Keysore:  /home/user2/.android/debug.keystore
        -------------------------------------

Here is your key store file  path 
copy this path
open terminal
   cd /home/user2/.android
  ==enter==
 
$ keytool -list -alias androiddebugkey -keystore debugkey.keystore

===enter==
enter password : android
===enter===
api key will be generated
***********************************************************
 
for windows use command
c:\Program Files\java\jdk1.7.0_25\jre\bin>keytool -list -alias androiddebugkey -keystore C:\Users\Manisha\.android\debug.keystore
 
 put it in
open a site in google named obtaining maps api key 
  click on link  http://code.google. com/android/maps-api-signup.html
   tick check box i have read & agree

   My certificate's MD5 fingerprint: (go to terminal & copy this key & put it here

The SDK tools create the debug keystore/key with predetermined names/passwords:(from android developer--> dev guide--> publishing --signing your application)
===================================================================================================================================================================
    * Keystore name: "debug.keystore"
    * Keystore password: "android"
    * Key alias: "androiddebugkey"
    * Key password: "android"
    * CN: "CN=Android Debug,O=Android,C=US"


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
         Get Google map API v2
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Links:
======
https://developers.google.com/maps/documentation/android/start#the_google_maps_api_key


Open terminal

manisha@manisha-desktop:~$ cd /home/manisha/.android/
manisha@manisha-desktop:~/.android$ keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
Alias name: androiddebugkey
Creation date: 14 May, 2012
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=Android Debug, O=Android, C=US
Issuer: CN=Android Debug, O=Android, C=US
Serial number: 4fb0a11b
Valid from: Mon May 14 11:37:23 IST 2012 until: Wed May 07 11:37:23 IST 2042
Certificate fingerprints:
  MD5:  E9:95:1F:46:16:9B:B1:4F:06:3D:D5:03:44:B8:88:5C
  SHA1: 98:80:82:5E:58:4F:62:11:09:CD:B4:11:88:21:6B:80:09:FE:DF:2E
  Signature algorithm name: SHA1withRSA
  Version: 3



sign in with google
===================
https://code.google.com/apis/console/#project:585866497286:access
=================================================================

=> create project by right clicking on left side spinner and create new project
=> Click on services 
=>  Google Maps Android API v2  
 Google Maps API v2  
 Google Play Android Developer API (click this services three buttons)
 Agree with terms * condition check box clik 

=> click api access fro left nevigation bar

=>Create new android key
 
put SHA1 & project package
 98:80:82:5E:58:4F:62:11:09:CD:B4:11:88:21:6B:80:09:FE:DF:2E;com.example.mapsdemo

add your api key to your project


=> In AndroidManifest.xml, add the following element as a child of the <application> element, by inserting it just before the closing tag </application>:

 <meta-data
     android:name="com.google.android.maps.v2.API_KEY"
 android:value="your_api_key"/>

 substituting your API key for your_api_key. This element sets the key com.google.android.maps.v2.API_KEY to the value your_api_key and makes the API key visible to   any MapFragment in your application.

Smaple code
==============
=> Sample code is bundled with the Google Play services SDK. First you must add Google Play services as an Android library project as follows:

    Select File > Import > Android > Existing Android Code Into Workspace and click Next.
    Select Browse..., enter <android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib, and click Finish.

Once you have installed Google Play services, you can view the sample code, or run the sample app locally. The sample app is an Eclipse project that you can run on your device as follows:

   ->  Select File > New > Other > Android sample project > Maps > Finish.
   ->  Select File > New > Other > Android Existing project from workspace > Browse > <android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib, and click Finish.
   -> Click Righ click on project. Properties >Android> in Library click on Add library> Apply>OK
   ->  Select Project > Properties, select Java Build Path, and navigate to Libraries.
   ->  Select Add External Jars, include the following jar files, and click OK:
        <android-sdk-folder>/extras/android/compatibility/v4/android-support-v4.jar
    Add your own Google Maps Android API key.
    Select Run > Run to test the sample app.
  -> Create folder libs and put both jar on it(android-support-v4.jar,google-play-services.jar)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
         Get Google map API v3
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Links:
======
http://gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/WebMap/


 

No comments:

Post a Comment