Friday, 6 September 2013

Google Map Demo for Location Display





















//Main Activity File

package com.demo;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class GoogleMapDemoAct extends MapActivity
{
    GeoPoint gp;
    MapView mv;
    MapController mc;
    MyLocationOverlay mo;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
        mv=(MapView) findViewById(R.id.myMapView);
        mv.setSatellite(true);
        mc=mv.getController();
        mv.setBuiltInZoomControls(true);
        mo= new MyLocationOverlay(getApplicationContext(),mv);
        mv.getOverlays().add(mo);
      
        LocationManager lm=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll=new MyLocationListener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,ll);
        mo.enableMyLocation();
    }
  
    public class MyLocationListener implements LocationListener
    {
        public void onLocationChanged(Location location)
        {      
            double lat=location.getLatitude();
            double log=location.getLongitude();
            Toast.makeText(getApplicationContext(),"My current location is: " +"\n"+ "Latitude= "+lat+"\n"+"Longitude= "+log,500).show();
          
            gp = new GeoPoint((int)(lat*1e6),(int)(log*1e6));
            mc.animateTo(gp);
            mc.setCenter(gp);
        }

        public void onProviderDisabled(String provider)
        {      
            Toast.makeText(getApplicationContext(),"GPS Disabled",Toast.LENGTH_SHORT).show();
        }

        public void onProviderEnabled(String provider)
        {      
            Toast.makeText(getApplicationContext(),"GPS Enabled",Toast.LENGTH_SHORT).show();
        }

        public void onStatusChanged(String provider, int status, Bundle extras)
        {      
          
        }
    }
  
    protected boolean isRouteDisplayed()
    {
        // TODO Auto-generated method stub
        return false;
    }
}

//Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.demo"
      android:versionCode="1"
      android:versionName="1.0">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>


    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <uses-library android:name="com.google.android.maps"/>
        <activity android:name=".GoogleMapDemoAct"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>


//main.xml File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

        <com.google.android.maps.MapView
            android:id="@+id/myMapView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:enabled="true"
            android:clickable="true"
            android:apiKey="AIzaSyCzvWIOs-UiTZ3BIfAB1Utwt10P9-1Wo6M"
            />
</LinearLayout>


//pri-requirments for this running above programm
go to "C:\Documents and Settings\student\.android"

copy the 'debug.keystore' file to the "D:\android installations"

open Command Promt and go to "C:\Program Files\Java\jdk1.6.0_21\bin" path.
and execute following


C:\Program Files\Java\jdk1.6.0_21\bin>keytool -list -v -alias androiddebugkey -
eystore "d:/android installations/debug.keystore" -storepass android -keypass a
droid

then you will get the output as

Alias name: androiddebugkey
Creation date: Aug 31, 2013
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: 4445789
Valid from: Sat Aug 31 05:09:20 IST 2013 until: Sun Aug 31 05:09:20 IST 2014
Certificate fingerprints:
         MD5:  B6:3D:96:D7:1F:D8:8D:2A:03:44:73:45:17:40:F3:09
         SHA1: 7D:DC:5E:21:F0:A6:1E:2A:D0:91:DE:AF:A8:B8:6B:CF:04:3A:5A:C4
         Signature algorithm name: SHA256withRSA
         Version: 3

Extensions:

#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: EF 99 1C 71 A9 5B A9 9C   98 67 23 3C DB 22 72 B2  ...q.[...g#<."r.
0010: A1 1B 25 97                                        ..%.
]
]

OPen Google.com and Search for "Google consol API"
select 1st Result..
and go on..
Steps are below:
create new project..
give a product key
and select other and create new client Id.
then go in Services and turn ON the Google Maps API v2
click on API Access
click on Create new Android key
enter the <API Key>;<package name
eg.. -> 7D:DC:5E:21:F0:A6:1E:2A:D0:91:DE:AF:A8:B8:6B:CF:04:3A:5A:C4;com.demoMap
click ok
this will generate the API key..
It is Unique !!
eg..
API key: AIzaSyCzvWIOs-UiTZ3GFHB1Utwt10P

No comments:

Post a Comment