skill paper

  • paper

Tuesday, August 14, 2012

Showing current location map

map.java

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
 
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
 
public class LocationBasedServicesV1 extends MapActivity {
 
	private MapView myMap;
 
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
 
		initMap();
		initMyLocation();
	}
 
	/**
	 * Initialise the map and adds the zoomcontrols to the LinearLayout.
	 */
	private void initMap() {
		myMap = (MapView) findViewById(R.id.mymap);
 
		View zoomView = myMap.getZoomControls();
		LinearLayout myzoom = (LinearLayout) findViewById(R.id.myzoom);
		myzoom.addView(zoomView);
		myMap.displayZoomControls(true);
 
	}
 
	/**
	 * Initialises the MyLocationOverlay and adds it to the overlays of the map
	 */
	private void initMyLocation() {
		MyLocationOverlay myLocOverlay = new MyLocationOverlay(this, myMap);
		myLocOverlay.enableMyLocation();
		myLocOverlay.enableCompass();
		myMap.getOverlays().add(myLocOverlay);
 
	}
 
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}



main layout:



  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:enabled="true"
    android:id="@+id/mymap"
    android:apiKey=" mapskey" />
  
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/myzoom"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
  />

set user permission:
  INTERNET 
   LOCATION.
 version="1.0" encoding="utf-8"?>

	xmlns:android="http://schemas.android.com/apk/res/android"
	package="de.itemis.frey.blog.lbsv1"
	android:versionCode="100"
	android:versionName="1.0.0">
	
		android:icon="@drawable/icon"
		android:label="@string/app_name">
		
			android:name=".LocationBasedServicesV1"
			android:label="@string/app_name">
			
				 android:name="android.intent.action.MAIN" />
				 android:name="android.intent.category.LAUNCHER" />
			
android:name="com.google.android.maps" /> android:name="android.permission.INTERNET" /> android:name="android.permission.ACCESS_FINE_LOCATION" /> Screen shot:


Wednesday, August 8, 2012

Passing value between activity:

Class1.java
    Intent intt=new Intent();
intt.putExtra("keyto call",value to pass);
startAcivity(intt);

key : which used to refer value in another actvity.
Class2.java

Bundle bun=Intent.getExtra();
String gh=bun.toString("Keyto call");


Sunday, August 5, 2012

REVERSE GEO POINT:

YOU MAY FACE THE PROBLEM IN AVD: BECAUSE THIS THROW SERVICE UNAVAILABLE EXCEPTION.




package com.map.touch;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

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.Overlay;

//import COM.ARUN.ex.MyLocationListener;
//import COM.ARUN.ex;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.android.maps.MapView.LayoutParams;

public class toucj extends MapActivity
{  
    /** Called when the activity is first created. */
 
double lat;
double lon;
MapView mapView;
   MapController mc;
   GeoPoint p;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
       
        setContentView(R.layout.main);
        LocationManager lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener ll=new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,ll);
        mapView = (MapView) findViewById(R.id.mapView);
        LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
        View zoomView = mapView.getZoomControls();

        zoomLayout.addView(zoomView,
            new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        mapView.displayZoomControls(true);
        mc = mapView.getController();
        String coordinates[] = {"1.352566007", "103.78921587"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);

        p = new GeoPoint(
            (int) (lat * 1E6),
            (int) (lng * 1E6));

        mc.animateTo(p);
        mc.setZoom(17);
        MapOverlay mapOverlay = new MapOverlay();
        List listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
        mapView.invalidate();

    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
    private class MyLocationListener implements LocationListener {

@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
Geocoder geocoder = new Geocoder(toucj.this, Locale.ENGLISH);
lat=(double) arg0.getLatitude();

        lon=(double) arg0.getLongitude();

}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}
   
    }
    class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView,
        boolean shadow, long when)
        {
            super.draw(canvas, mapView, shadow);                  

            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(
                getResources(), R.drawable.icon);          
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);        
            return true;
        }
        public boolean onTouchEvent(MotionEvent event, MapView mapView)
        {  
            //---when user lifts his finger---
            if (event.getAction() == 1) {              
                GeoPoint p = mapView.getProjection().fromPixels(
                    (int) event.getX(),
                    (int) event.getY());
                    Toast.makeText(getBaseContext(),
                       p.getLatitudeE6() / 1E6 + "," +
                        p.getLongitudeE6() /1E6 ,
                        Toast.LENGTH_SHORT).show();
                Geocoder geoCoder = new Geocoder(
                        getBaseContext(), Locale.getDefault());
                    try {
                   
                        List
addresses = geoCoder.getFromLocation(

                            p.getLatitudeE6()  / 1E6,
                            p.getLongitudeE6() / 1E6, 1);
   
                        String add = "";
                        if (addresses.size() > 0)
                        {
                            for (int i=0; i                                 i++)
                               add += addresses.get(0).getAddressLine(i) + "\n";
                        }
   
                        Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
                    }
                    catch (IOException e) {              
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(), e.toString()
                        , Toast.LENGTH_SHORT).show();
                    }  
                    return true;
                }
            else
return false;
             
        }
    }
}




Class cannot initiated in activity:
  • Check for activity in manifest.
  • May be due to the declaration of  normal variable in class before oncreate function.
  • clean the project and run.
  • version miss-match(emulator).




I face similar problem for declaring variable in class like
class dim extends Activity
{
button btn;//this create me problems.
oncreate()
{
}
}


ERROR DETAIL:

08-05 16:39:36.371: ERROR/AndroidRuntime(279): FATAL EXCEPTION: main
08-05 16:39:36.371: ERROR/AndroidRuntime(279): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{COM.ARUN/COM.ARUN.ex}: java.lang.NullPointerException
08-05 16:39:36.371: ERROR/AndroidRuntime(279):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
08-05 16:39:36.371: ERROR/AndroidRuntime(279):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-05 16:39:36.371: ERROR/AndroidRuntime(279):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-05 16:39:36.371: ERROR/AndroidRuntime(279):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-05 16:39:36.371: ERROR/AndroidRuntime(279):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 16:39:36.371: ERROR/AndroidRuntime(279):     at android.os.Looper.loop(Looper.java:123)
08-05 16:39:36.371: ERROR/AndroidRuntime(279):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-05 16:39:36.371: ERROR/AndroidRuntime(279):     at java.lang.reflect.Method.invokeNative(Native Method)
08-05 16:39:36.371: ERROR/AndroidRuntime(279):     at java.lang.reflect.Method.invoke(Method.java:521)
08-05 16:39:36.371: ERROR/AndroidRuntime(279):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-05 16:39:36.371: ERROR/AndroidRuntime(279):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-05 16:39:36.371: ERROR/AndroidRuntime(279):     at dalvik.system.NativeStart.main(Native Method)
08-05 16:39:36.371: ERROR/AndroidRuntime(279): Caused by: java.lang.NullPointerException
08-05 16:39:36.371: ERROR/AndroidRuntime(279):     at android.app.Activity.findViewById(Activity.java:1637)
08-05 16:39:36.371: ERROR/AndroidRuntime(279):     at COM.ARUN.ex.(ex.java:24)
08-05 16:39:36.371: ERROR/AndroidRuntime(279):     at java.lang.Class.newInstanceImpl(Native Method)
08-05 16:39:36.371: ERROR/AndroidRuntime(279):     at java.lang.Class.newInstance(Class.java:1429)
08-05 16:39:36.371: ERROR/AndroidRuntime(279):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-05 16:39:36.371: ERROR/AndroidRuntime(279):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
08-05 16:39:36.371: ERROR/AndroidRuntime(279):     ... 11 more