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

Saturday, August 27, 2011

Map for Beginners

Sample map:
       Create new project,(file->new->androis project).
     Add permission to access internet in manifestXml.
    
    
    

   Next edit main.xml:
             
  ?xml version="1.0" encoding="utf-8"?>  
RelativeLayout 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/mapview"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:clickable="true"
    android:apiKey="Your Maps API Key" />


Iam Sure your u have Api key...

    1. public class HelloMapView extends MapActivity {}
                                           - extend mapActivity instead od Activity .
     2. protected boolean isRouteDisplayed()
{
   
return false;//override this into false
}


      3.initialize below in oncreate()
     mapView = (MapView) findViewById(R.id.mapview);
     mapView
.setBuiltInZoomControls(true);


NOW RUN IT....
you seem to get like this

Generally Map Load from USA and BRASIL.you can change the to your location....
for your query post me here,

gps-map



Creating a Custom Layout as a Transparent Panel


We wanted our transparent panel to hold children so we looked for the most natural Android view to extend and selected Linear Layout because we wanted our TransparentPanel class to layout its children horizontally. We could just as easily chosen to extend RelativeLayout of any other layout class.

       1.TransparentPanel extends LinearLayout


The ‘magic’ of TransparentPanel happens in the dispathDraw() method. For those of you that have already created their own custom Views, you might wonder why we override dispatchDraw() instead of onDraw(). For some reason, LinearLayout does not call it’s own onDraw() method…apparently because its developer assumes a LinearLayout would never have anything to draw. BUT we want our TransparentPanel to draw a background so we override dispatchDraw() to draw the background and then let super.dispatchDraw(canvas) render any child components.


    
          protected void dispatchDraw(Canvas canvas) {
 
	           RectF drawRect = new RectF();
	           drawRect.set(0,0, getMeasuredWidth(), getMeasuredHeight());
 
	           canvas.drawRoundRect(drawRect, 5, 5, innerPaint);
	           canvas.drawRoundRect(drawRect, 5, 5, borderPaint);
 
	           super.dispatchDraw(canvas);
                }


For those new to drawing their own graphics, let’s review a few items here. First, we populate a RectF with the coordinates of the background that we want to draw. Next we make to calls to drawRoundRect(). The 1st call passes innerPaint to draw the transparent gray background while the 2nd call passes the white border that we want to paint. Lastly we call super.dispatchDraw(canvas) to render our child components (in this case a Button).







The gray background is rendered with an alpha (transparency ) == 225. This allows just enough of the map to show through.





                                      1 innerPaint.setARGB(225, 75, 75, 75);


And borderPaint allows us to render a white border with a Stroke of width = 2.



                   borderPaint = new Paint();
                   borderPaint.setARGB(255, 255, 255, 255);
                   borderPaint.setAntiAlias(true);
                   borderPaint.setStyle(Style.STROKE);
                   borderPaint.setStrokeWidth(2);
 
 
As we did above, make sure to setAntiAlias(true) so the borders of your paint blend seamlessly with its surroundings. Set this option to false to see how your borders would have jagged edges otherwise.


Adding our custom TransparentPanel class as a declarations in the layout XML.


Now we’re ready to insert the TransparentPanel into our layout XML class and to add a button. To reference our new class, we simply provide the full classpath to our the TransparentPanel and then provide layout parameters as we would for any LinearLayout. In this case, we provide padding so our Button does not rest against the edges of our TransparentPanel’s border.




xml layout: