Sunday, April 1, 2012

Re: [android-developers] Re: Sending GPS coordinates to a server

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! <alertsavemylife@gmail.com> 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 <lutz@bendlin.us> 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 be very thankful to u if u could explain to me in simple terms how to do it, I couldnt understand what u have mentioned earlier

On Wed, Mar 28, 2012 at 10:39 PM, Ankit Kasliwal <kasliwalankit2000@gmail.com> wrote:
Hello,
            First you create Handler file or Web service which store data over server and then
call that file through your Android application on Location onchange function to pass all the values to file.


On Thu, Mar 29, 2012 at 11:04 AM, Bhuvan Chandra <bhuvanchandra18@gmail.com> wrote:
By sending the coordinates to a server I mean once u recieve the position coordinates of the location they have to be transmitted to a server(By server i mean our college server , The server which is used in our college and which iam working on) so that the coordinates can be accessed from the server and overlayed on a map from tracking the vehicle. Can anybody give me an idea about how to code a program so that i could recieve the coordinates of the location and directly transmit them to the server i mentioned above

On Mon, Mar 26, 2012 at 12:41 AM, Ali Chousein <ali.chousein@gmail.com> wrote:
> recieve the Location coordiantes
http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android
(The guidance provided by CommonsWare is simply excellent)

> send them directly to a server
This depends what type of interface you have at the server side.
Without knowing the server side interface, no one can tell you how to
communicate with it from an Android platform. First you need to
clarify your server side architecture, a question which is not
directly related to Android SDK. If the server you should send data is
already there, contact the responsible people to find out its
interface. If you should develop the server also, I guess you need to
research the best practices for server side architectures to decide
which one fits best your scenario.

-------------------------------------------------
Ali Chousein
http://socialnav.blogspot.com | http://twitter.com/socialnav1
http://weatherbuddy.blogspot.com | http://twitter.com/weather_buddy
http://www.codeproject.com/KB/android/PayGol-Android.aspx
http://geo-filtered-assistant.blogspot.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en



--

Thanks and Regards,

Ankit Kasliwal
kasliwalankit2000@gmail.com
+91-9300-940-136

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment