Link to home
Start Free TrialLog in
Avatar of errang
errangFlag for Afghanistan

asked on

How do you get your current position in Android?

Hey,

       I was wondering if there was a straight forward way to get the current location from an android device.  I tried looking on the Android Developer site, but their examples only confused me... and also seemed like a very roundabout way to do something so simple, which made me think I missed something in the translation..?

Could anyone point me to a more simpler straight forward tutorial?

Thanks in advance!
Avatar of snailcat
snailcat
Flag of United States of America image

Are you looking for an app that will do this or how to code this in android?

There is an app called maverick that will give yours coordinates
Avatar of errang

ASKER

I'm trying to code this
Try this link: http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/

It has a tutorial on gps for android with done code samples.
ASKER CERTIFIED SOLUTION
Avatar of snailcat
snailcat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of errang

ASKER

Hm... tried the code at the link, and it runs without errors...

But I'm having trouble getting it to run on the emulator, the command telnet localhost 5554 isn't working... is there another way around this?
SOLUTION
Avatar of Dejan Pažin
Dejan Pažin
Flag of Austria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of errang

ASKER

It just says connection refused.  Tried this on ubintu and Mac OSX.

I ran this command in the terminal after starting the simulator.

The port number of the emulator should be displayed in the title of the emulator window. Like this: 5554 MyEmulator

What does it say in your emulator?
Avatar of errang

ASKER

>>The port number of the emulator should be displayed in the title of the emulator window. Like this: 5554 MyEmulator

Hm, never noticed that before... but mine says 5554.

The connection works now for some odd reason after restarting... but I still see a blank screen on the emulator.

This is what I have... do I have it right?
 
package cookbook.recipes;

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.widget.TextView;
import android.widget.Toast;

import java.*;


public class MapsTest extends Activity

{

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);


/* Use the LocationManager class to obtain GPS locations */

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

LocationListener mlocListener = new MyLocationListener();


mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);


}


/* Class My Location Listener */

public class MyLocationListener implements LocationListener

{

@Override

public void onLocationChanged(Location loc)

{

loc.getLatitude();

loc.getLongitude();

//String Text = “My current location is: “ + “Latitud = “ + loc.getLatitude() + “Longitud = “ + loc.getLongitude();

String Text = "My current location is:" + "latitude = " + loc.getLatitude() + "Longitude = " + loc.getLongitude();
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(Text);
Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();

}


@Override

public void onProviderDisabled(String provider)

{

Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();

}


@Override

public void onProviderEnabled(String provider)

{

Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();

}


@Override

public void onStatusChanged(String provider, int status, Bundle extras)

{


}

}/* End of Class MyLocationListener */

}/* End of UseGps Activity */

Open in new window


 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>

Open in new window


Do you have Maps API Key from Google? You have to get one in order to use the maps. Here is how you get it:

http://code.google.com/intl/sl/android/add-ons/google-apis/mapkey.html
Avatar of errang

ASKER

Yes, I got the google maps API key using keytool.

The application works fine - it tells you the current location when you change it.

There is no map view in this example, it is only an appication which tells you the current location.

So, you start the app and then change the location through the telnet and the coordinates will be displayed on the blank screen. Thats it, no map view involved.