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!
errangAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

snailcatCommented:
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
errangAuthor Commented:
I'm trying to code this
snailcatCommented:
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.
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

snailcatCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
errangAuthor Commented:
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?
Dejan PažinHead of SW DevelopmentCommented:
>> the command telnet localhost 5554 isn't working

What happens when you try this? It should work if the emulator is started.
errangAuthor Commented:
It just says connection refused.  Tried this on ubintu and Mac OSX.

I ran this command in the terminal after starting the simulator.
Dejan PažinHead of SW DevelopmentCommented:

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?
errangAuthor Commented:
>>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

Dejan PažinHead of SW DevelopmentCommented:

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
errangAuthor Commented:
Yes, I got the google maps API key using keytool.
Dejan PažinHead of SW DevelopmentCommented:

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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Android

From novice to tech pro — start learning today.