Iam very much new to the java programming , but i have developed an
application by modifying a sample code  from a website which could give the
current location coordinates and keeps on updating using the gps in the
mobile.But i want to know how to send these coordinates to a server which i
could access and receive the coodinates .Any one please help me how to do
it by java coding, the following is the code which i have used for
receiving the coordinates

package com.javacodegeeks.android.lbs;


import com.javacodegeeks.android.lbs.R;

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

public class LbsGeocodingActivity extends Activity {

    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);

        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(LbsGeocodingActivity.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(LbsGeocodingActivity.this, message,
Toast.LENGTH_LONG).show();
        }

        public void onStatusChanged(String s, int i, Bundle b) {
            Toast.makeText(LbsGeocodingActivity.this, "Provider status
changed",
                    Toast.LENGTH_LONG).show();
        }

        public void onProviderDisabled(String s) {
            Toast.makeText(LbsGeocodingActivity.this,
                    "Provider disabled by the user. GPS turned off",
                    Toast.LENGTH_LONG).show();
        }

        public void onProviderEnabled(String s) {
            Toast.makeText(LbsGeocodingActivity.this,
                    "Provider enabled by the user. GPS turned on",
                    Toast.LENGTH_LONG).show();
        }

    }

}



On Sun, Apr 1, 2012 at 9:45 AM, ZeroOne <[email protected]> wrote:

> Just to establish the baseline so that we know what we can assume...
> Have you ever programmed anything else before? Do you know Java
> programming at all, or is this your first Java programming project
> ever? I'm proficient with Java but new to Android so I found this
> thread when struggling with getting the location. This is a pretty
> good page explaining the details on what you need to do:
>
> http://developer.android.com/guide/topics/location/obtaining-user-location.html
> Read that and experiment.
>
>   -ZeroOne
>
>
> On Apr 1, 4:29 pm, Bhuvan Chandra <[email protected]> wrote:
> > Hey i did run this code ...but iam getting some errors, what result am i
> > expecting here, i couldnt run the code , any help will be appreciated.
> >
> > On Sat, Mar 31, 2012 at 1:26 AM, Save My Life! <
> [email protected]>wrote:
> >
> >
> >
> >
> >
> >
> >
> > > Good help Ankit.
> >
> > > Dear Bhuvan,
> >
> > > use following code for GPS Lat long (Location ) Traking [use Google API
> > > 2.x in eclipse for creating AVD]
> >
> > >  import java.io.BufferedReader;
> >
> > > import java.io.BufferedWriter;
> >
> > > import java.io.File;
> >
> > > import java.io.FileWriter;
> >
> > > import java.io.IOException;
> >
> > > import java.io.InputStreamReader;
> >
> > > import java.util.List;
> >
> > > import java.util.Locale;
> >
> > > import org.apache.http.HttpEntity;
> >
> > > import org.apache.http.HttpResponse;
> >
> > > import org.apache.http.client.HttpClient;
> >
> > > import org.apache.http.client.methods.HttpGet;
> >
> > > import org.apache.http.impl.client.DefaultHttpClient;
> >
> > > import org.json.JSONArray;
> >
> > > import org.json.JSONObject;
> >
> > > import android.app.Activity;
> >
> > > import android.app.PendingIntent;
> >
> > > import android.content.ContentResolver;
> >
> > > import android.content.Context;
> >
> > > import android.content.Intent;
> >
> > > import android.content.pm.ActivityInfo;
> >
> > > import android.database.Cursor;
> >
> > > 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.location.LocationProvider;
> >
> > > import android.net.Uri;
> >
> > > import android.os.Bundle;
> >
> > > import android.provider.ContactsContract;
> >
> > > import android.telephony.SmsManager;
> >
> > > import android.util.Log;
> >
> > > import android.view.View;
> >
> > > import android.view.View.OnClickListener;
> >
> > > import android.widget.Button;
> >
> > > import android.widget.ProgressBar;
> >
> > > import android.widget.TextView;
> >
> > > import android.widget.Toast;
> >
> > > 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;
> >
> > > public class clsGPSLocatorActivity extends MapActivity implements
> > > OnClickListener {
> >
> > > /** Called when the activity is first created. */
> >
> > > private MapView mapView;
> >
> > > private MapController mapController;
> >
> > > public String GBLatitude="";
> >
> > > public String GBLongitude="";
> >
> > > LocationListener locationlistener;
> >
> > > LocationManager locationManager;
> >
> > > String Current_Address="";
> >
> > > TextView txtStatus;
> >
> > > TextView txtLocation;
> >
> > > TextView txtLoad;
> >
> > > Button btnExitme;
> >
> > > Button btnSendSMS;
> >
> > >  ProgressBar mapprogbar;
> >
> > > public static final int PICK_CONTACT = 10;
> >
> > > public int StopFlag=0;
> >
> > > String address="";
> >
> > > @Override
> >
> > > public void onCreate(Bundle savedInstanceState) {
> >
> > > super.onCreate(savedInstanceState);
> >
> > > try
> >
> > > {
> >
> > > Find_Current_LatLong();
> >
> > > }
> >
> > > catch(Exception e)
> >
> > > {
> >
> > > Log.v("Find Latlong error :",e.toString());
> >
> > > }
> >
> > > }
> >
> > >  @Override
> >
> > > protected boolean isRouteDisplayed() {
> >
> > > // TODO Auto-generated method stub
> >
> > > return false;
> >
> > > }
> >
> > >  private void Find_Current_LatLong() {
> >
> > > try
> >
> > > {
> >
> > > txtStatus.setText("Finding Current Latitude & Longitude..");
> >
> > > appendLog("\n ClsMyLocation : Find_Current_LatLong(); Called \n ");
> >
> > > appendLog("Wait....Locating Accident Place!");
> >
> > > //Find Latitude & Longitude
> >
> > > locationManager =
> > > (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
> >
> > > locationlistener = new LocationListener() {
> >
> > > public void onLocationChanged(Location location) {
> >
> > > mapprogbar.setVisibility(View.VISIBLE);
> >
> > > txtLoad.setVisibility(View.VISIBLE);
> >
> > > updateLocation(location);
> >
> > > }
> >
> > > public void onStatusChanged(
> >
> > > String provider, int status, Bundle extras) {
> >
> > > switch (status) {
> >
> > > case LocationProvider.AVAILABLE:
> >
> > > appendLog("GPS available again\n");
> >
> > > txtStatus.setText("GPS available again\n");
> >
> > > break;
> >
> > > case LocationProvider.OUT_OF_SERVICE:
> >
> > > appendLog("GPS out of service\n");
> >
> > > txtStatus.setText("GPS out of service\n");
> >
> > > break;
> >
> > > case LocationProvider.TEMPORARILY_UNAVAILABLE:
> >
> > > appendLog("GPS temporarily unavailable\n");
> >
> > > txtStatus.setText("GPS temporarily unavailable\n");
> >
> > > break;
> >
> > > }
> >
> > > }
> >
> > > public void onProviderEnabled(String provider)
> >
> > > {
> >
> > > appendLog("GPS Enabled");
> >
> > > txtStatus.setText("GPS Enabled\n");
> >
> > > }
> >
> > > public void onProviderDisabled(String provider)
> >
> > > {
> >
> > > appendLog("GPS Disabled");
> >
> > > txtStatus.setText("GPS Disabled\n");
> >
> > > }
> >
> > > };
> >
> > > locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
> 5000,
> > > 10, locationlistener);
> >
> > > }
> >
> > > catch(Exception e)
> >
> > > {
> >
> > > Toast.makeText(this,"Find_Current_LatLong: "+e.toString(),
> > > Toast.LENGTH_LONG).show();
> >
> > > appendLog("\n ClsMyLocation : Error in Find_Current_LatLong"
> +e.toString()
> > > );
> >
> > > }
> >
> > > }
> >
> > > //Update Location
> >
> > > private void updateLocation(Location location)
> >
> > > {
> >
> > > try
> >
> > > { if (location != null) {
> >
> > > GeoPoint point = new GeoPoint(
> >
> > > (int) (location.getLatitude() * 1E6),
> >
> > > (int) (location.getLongitude() * 1E6));
> >
> > > GBLatitude = Double.toString(location.getLatitude());
> >
> > > GBLongitude = Double.toString(location.getLongitude());
> >
> > > this.mapController.setCenter(new GeoPoint((int)
> (location.getLatitude() *
> > > 1E6),(int) (location.getLongitude() * 1E6)));
> >
> > > // add marker
> >
> > > MapOverlay mapOverlay = new MapOverlay(this);
> >
> > > mapOverlay.setPointToDraw(point);
> >
> > > List<Overlay> listOfOverlays = mapView.getOverlays();
> >
> > > listOfOverlays.clear();
> >
> > > listOfOverlays.add(mapOverlay);
> >
> > > txtStatus.setText(Double.toString(location.getLatitude()) +"," +
> > > Double.toString(location.getLongitude()));
> >
> > > address = ConvertPointToLocation(point);
> >
> > > //address += "\n"+ Double.toString(location.getLatitude()) +"," +
> > > Double.toString(location.getLongitude());
> >
> > > txtStatus.setText(address);
> >
> > >  mapController.animateTo(point);
> >
> > > //mapController.setZoom(16);
> >
> > > mapView.invalidate();
> >
> > > }
> >
> > > }
> >
> > > catch(Exception e)
> >
> > > {
> >
> > > Toast.makeText(this,"Location Update Not Succeed!"+e.toString(),
> > > Toast.LENGTH_LONG).show();
> >
> > > }
> >
> > > }
> >
> > > public String ConvertPointToLocation(GeoPoint point) {
> >
> > > String address = "";
> >
> > > Geocoder geoCoder = new Geocoder(
> >
> > > getBaseContext(), Locale.getDefault());
> >
> > > try {
> >
> > > List<Address> addresses = geoCoder.getFromLocation(
> >
> > > point.getLatitudeE6() / 1E6,
> >
> > > point.getLongitudeE6() / 1E6, 1);
> >
> > > if (addresses.size() > 0) {
> >
> > > for (int index = 0;
> >
> > > index < addresses.get(0).getMaxAddressLineIndex(); index++)
> >
> > > address += addresses.get(0).getAddressLine(index) + " ";
> >
> > > }
> >
> > > }
> >
> > > catch (Exception e) {
> >
> > > appendLog("clsGPSLocator:Error in convertpoint to location:
> > > "+e.toString());
> >
> > > }
> >
> > > try
> >
> > > {
> >
> > > if(address == "")
> >
> > > { appendLog("ClsOperationalIntent : Getting Address Using Json
> Parsing");
> >
> > > String Latitude=Double.toString(point.getLatitudeE6() / 1E6);
> >
> > > String Longitude=Double.toString(point.getLongitudeE6() / 1E6);
> >
> > > address ="JS:";
> >
> > > address += ReadAddressFromWebService(Latitude,Longitude);
> >
> > > }
> >
> > > }
> >
> > > catch(Exception e)
> >
> > > {
> >
> > > appendLog("\nClsOperationalIntent : Json Parser Fail To get
> Address\n");
> >
> > > }
> >
> > > return address;
> >
> > > }
> >
> > > public String ReadAddressFromWebService(String latitude, String
> longitude)
> > > {
> >
> > > String Address="";
> >
> > > StringBuffer sb=new StringBuffer();
> >
> > > appendLog("ClsOperationalIntent : Getting Address Using Json Parsing");
> >
> > > sb.append("http://maps.googleapis.com/maps/api/geocode/json?latlng="+
> > > latitude +","+longitude +"&sensor=false");
> >
> > > String url=sb.toString();
> >
> > > HttpClient httpClient=new DefaultHttpClient();
> >
> > > appendLog("HTTP client created");
> >
> > > String responseData="";
> >
> > > try {
> >
> > > HttpResponse response=httpClient.execute(new HttpGet(url));
> >
> > > response.addHeader("Accept-Language", "en-US");
> >
> > > HttpEntity entity=response.getEntity();
> >
> > > appendLog("HTTP Response arrived");
> >
> > > BufferedReader bf=new BufferedReader(new
> > > InputStreamReader((entity.getContent()),"UTF-8"));
> >
> > > String line="";
> >
> > > appendLog("Start buffre reading");
> >
> > > while((line=bf.readLine())!=null){
> >
> > > responseData=responseData+line;
> >
> > > }
> >
> > > JSONObject jsonObj = new JSONObject(responseData);
> >
> > > JSONArray resultArry = jsonObj.getJSONArray("results");
> >
> > > Address =
> > > resultArry.getJSONObject(0).getString("formatted_address").toString();
> >
> > > } catch (Exception e) {
> >
> > > // TODO Auto-generated catch block
> >
> > > appendLog("Parsing Problem: "+e.toString());
> >
> > > }
> >
> > > return Address;
> >
> > > }
> >
> > > class MapOverlay extends Overlay
> >
> > > {
> >
> > > private GeoPoint pointToDraw;
> >
> > > Activity activity;
> >
> > > public void setPointToDraw(GeoPoint point) {
> >
> > > pointToDraw = point;
> >
> > > }
> >
> > > public GeoPoint getPointToDraw() {
> >
> > > return pointToDraw;
> >
> > > }
> >
> > > public MapOverlay(Activity mActivity){
> >
> > > // create a class level activity object in your ImageDownloader class.
> >
> > > activity = mActivity;
> >
> > > }
> >
> > > @Override
> >
> > > public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
> long
> > > when) {
> >
> > > super.draw(canvas, mapView, shadow);
> >
> > > try
> >
> > > {
> >
> > > // convert point to pixels
> >
> > > Point screenPts = new Point();
> >
> > > mapView.getProjection().toPixels(pointToDraw, screenPts);
> >
> > > // add marker
> >
> > > Bitmap bmp =
> > >
> BitmapFactory.decodeResource(activity.getResources(),R.drawable.locationpin);
> >
> > > canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null);
> >
> > > }
> >
> > > catch(Exception e)
> >
> > > {
> >
> > > appendLog("GPSLocatorActivity: Draw Error :"+ e.toString());
> >
> > > }
> >
> > > return true;
> >
> > > }
> >
> > > }
> >
> > > On Sat, Mar 31, 2012 at 7:46 AM, lbendlin <[email protected]> wrote:
> >
> > >> "science background" - good one.  Keep it up.
> >
> > >> On Thursday, March 29, 2012 1:43:17 AM UTC-4, bhuvan wrote:
> >
> > >>> Actually i come from a science background and iam not an android
> > >>> developer , i was trying to develop an application for the sake of my
> > >>> project - Vehicle tracking using a mobile, I would
> >
> > ...
> >
> > read more ยป
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to