Monday, 9 July 2012

GET THE CURRENT LOCATION WHEN CLICK ON THE MAP IN ANDROID WITH SOURCE CODE.

GET THE CURRENT LOCATION WHEN CLICK ON THE MAP IN ANDROID WITH SOURCE CODE.


In this example ,we get the current location when you click on the map.that is we get the current latitude and longitude of the location when you click on the map.the source code is given below .i hope you this will be useful for you.your valuable suggestion are always welcome















                                                                                                     


JithumapdemoActivity.java



package a.b;

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 android.app.Activity;
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.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.Toast;

public class JithumapdemoActivity extends MapActivity {
MapView mapView;
MapController mc;
GeoPoint p;


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

       @Override
       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());
           }                          
           return false;
       }      
   }
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        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);
        mapView.setSatellite(false);
        mc=mapView.getController();
        String[] cord={"8.484639","76.955538"};
        double lat = Double.parseDouble(cord[0]);
        double lng = Double.parseDouble(cord[1]);
        p = new GeoPoint(
                (int) (lat * 1E6),
                (int) (lng * 1E6));
        mc.animateTo(p);
        mc.setZoom(17);
        mapView.invalidate();
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);      

        mapView.invalidate();
    }

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}  


main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    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:enabled="true"
        android:clickable="true"
        android:apiKey="0vNjDM-9Hpz9W0naxngZObrxztXkFRH5LfyxboA"
        />
  <LinearLayout android:id="@+id/zoom" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:layout_centerHorizontal="true" />
        
</RelativeLayout>







the output for your program is look like this .when you click on the map you will get the current location  on your screen.try it.








Tuesday, 3 July 2012

Create a Button click without using a onClickListener in android example

Create a Button click without using a onClickListener in android example





In this example we are not using a onClickListner for giving action to a button ,in this example we are just give the reference to main.xml  for the onClick
















Here is the code for the main.xml



main.xml


<?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"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <Button 
    android:id="@+id/button1"
    android:text="hai"
    android:layout_width="62dip"
    android:layout_height="62dip"
    android:layout_marginLeft="75dip"
    android:layout_marginTop="75dip"
    android:onClick="sendmsg"/>
</LinearLayout>




the method name in the main activity which is used for giving action have the same name as that of the main.xml -onclick .that is in main actvity we have methode named sendmsg().one thing you must notice that this method must be public and return a void return value




ButtonclickActivity


package a.b;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ButtonclickActivity extends Activity {
 Button b1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
        b1=(Button)findViewById(R.id.button1);
        
        
    }
    public void sendmsg(View v)
    {
    if (v==b1)
    {
    Toast.makeText(this, "haiiiiii",Toast.LENGTH_LONG).show();
    }
    }
    
}















Sunday, 1 July 2012

GET THE CURRENT GPS LOCATION AND SHOW IT IN THE GOOGLE MAPS IN ANDROID WITH SOURCE CODE

GET THE CURRENT GPS LOCATION AND SHOW IT IN THE GOOGLE  MAPS IN ANDROID WITH SOURCE CODE



      In this example the users current position is taken by using the Gps or   here the Gps value is set in the emulator .in emulator take DDMS and select the emulator control and set the latitude and the longitude value.before run your application send this value to emulator.the sample program is given below,













GpspostionActivity.java                                                                



package a.b;

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.MapView.LayoutParams;

import android.app.Activity;

import android.content.Context;

import android.location.Location;

import android.location.LocationListener;

import android.location.LocationManager;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.LinearLayout;

import android.widget.Toast;


public class GpspostionActivity extends MapActivity  {
MapView mapView; 
   MapController mc;
   GeoPoint p;

     

    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters

    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds

     

    protected LocationManager locationManager;

     

    protected Button retrieveLocationButton;

     

    @Override

    public void onCreate(Bundle savedInstanceState) {

         

       super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        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[] = {"8.487495", "76.948623"};
        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); 
        mapView.invalidate();


        retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);

         

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

         

        locationManager.requestLocationUpdates(

                LocationManager.GPS_PROVIDER,

                MINIMUM_TIME_BETWEEN_UPDATES,

                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,

                new MyLocationListener()

        );

         

    retrieveLocationButton.setOnClickListener(new OnClickListener() {

            @Override

            public void onClick(View v) {

                showCurrentLocation();

            }

    });       

         

    }   


    protected void showCurrentLocation() {


        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);


        if (location != null) {

            String message = String.format(

                    "Current Location \n Longitude: %1$s \n Latitude: %2$s",

                    location.getLongitude(), location.getLatitude()

            );

            Toast.makeText(GpspostionActivity.this, message,

                    Toast.LENGTH_LONG).show();

        }


    }  


    private class MyLocationListener implements LocationListener {


        public void onLocationChanged(Location location) {

            String message = String.format(

                    "New Location \n Longitude: %1$s \n Latitude: %2$s",

                    location.getLongitude(), location.getLatitude()

            );

            Toast.makeText(GpspostionActivity.this, message, Toast.LENGTH_LONG).show();

        }


        public void onStatusChanged(String s, int i, Bundle b) {

            Toast.makeText(GpspostionActivity.this, "Provider status changed",

                    Toast.LENGTH_LONG).show();

        }


        public void onProviderDisabled(String s) {

            Toast.makeText(GpspostionActivity.this,

                    "Provider disabled by the user. GPS turned off",

                    Toast.LENGTH_LONG).show();

        }


        public void onProviderEnabled(String s) {

            Toast.makeText(GpspostionActivity.this,

                    "Provider enabled by the user. GPS turned on",

                    Toast.LENGTH_LONG).show();

        }


    }



@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}

     

}

   main.xml




<?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"
   >
<Button
android:id="@+id/retrieve_location_button"
android:text="Retrieve Location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<com.google.android.maps.MapView 
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:enabled="true"
        android:clickable="true"
        android:apiKey="0vNjDM-9Hpz8UM7pUWYJLvFptBpUpOqvQnV0S_g"
        />
  <LinearLayout android:id="@+id/zoom" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:layout_centerHorizontal="true" />
 
</LinearLayout>

  


How to set gps value in eclipse




DDMS-- emulator control --set value--send--then run your application






output is given below

















  
Download Android Codes From SkyBlueAndroid