DroidForums.net is the original Verizon Android Forum! Registered Users do not see these ads. Please Register - It's Free!
Results 1 to 4 of 4

Thread: How to get location data (with code samples!)

  1. Junior Droid
    MrGlass's Avatar
    Member #
    12425
    Join Date
    Dec 2009
    Posts
    12
    Phone
    Enter Current Phone Here
    #1

    How to get location data (with code samples!)

    OK, I searched long and hard for a guide to this and couldn't find one anywhere, so here goes:

    in brief, there are essentially 3 important parts to getting location data. You need to

    1. Request location permissions in the manifest
    2. Grab the location service
    3. Start a location listener

    the location listener seems to be the part most people miss. It watches for new location data and performs an action when the device location is updated.

    Below is a very basic sample that will display the devices latitude and longitude. If you run this in an emulator, make sure you simulate the gps data. There is plenty of documentation of doing that, but I found it easiest to use the location control (found in the emulator control panel in eclipse DDMS view) to pass a lat/long value.

    If you want help with this, feel free to reply below or contact me on twitter (@misterglass)

    Code:
    //hello.java
    package mrglass.androidapp1;
    
    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;
    
    public class hello extends Activity {
    
        private TextView mytext;
        private LocationManager locmgr = null;
        
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            mytext = (TextView) findViewById(R.id.mytext);
            
            //grab the location manager service
            locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
      
            mytext.setText("waiting for location");
        }
        
        //Start a location listener
        LocationListener onLocationChange=new LocationListener() {
            public void onLocationChanged(Location loc) {
                //sets and displays the lat/long when a location is provided
                String latlong = "Lat: " + loc.getLatitude() + " Long: " + loc.getLongitude();   
                mytext.setText(latlong);
            }
             
            public void onProviderDisabled(String provider) {
            // required for interface, not used
            }
             
            public void onProviderEnabled(String provider) {
            // required for interface, not used
            }
             
            public void onStatusChanged(String provider, int status,
            Bundle extras) {
            // required for interface, not used
            }
        };
        
        //pauses listener while app is inactive
        @Override
        public void onPause() {
            super.onPause();
            locmgr.removeUpdates(onLocationChange);
        }
        
        //reactivates listener when app is resumed
        @Override
        public void onResume() {
            super.onResume();
            locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,10000.0f,onLocationChange);
        }
    }
    
    
    //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:id="@+id/mytext"  
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    
        
    </LinearLayout>
    
    
    //manifest
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="mrglass.androidapp1"
          android:versionCode="2"
          android:versionName="0.2">
          
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".hello"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
        <uses-sdk android:minSdkVersion="6" />
    </manifest>
  2. Senior Droid
    silverx10's Avatar
    Member #
    7876
    Join Date
    Nov 2009
    Location
    Virginia Beach, VA
    Posts
    199
    Liked
    1 times
    Phone
    Motorola DROID
    #2
    Wow.

    Now, I know you said you couldn't find any snippets out there for this, but... did you do this from what you've learned, or did you scavenge the net for snippets of code to splice into each other to make this?

    If the latter, then kudos. Very much so.
    "The only verdict is vengence; a vendetta, held as a votive, not in vain, for the value and voracity of such shall one day vindicate the vigilant and the virtuous."
  3. Junior Droid
    MrGlass's Avatar
    Member #
    12425
    Join Date
    Dec 2009
    Posts
    12
    Phone
    Enter Current Phone Here
    #3
    After reading for hours yesterday, I understood enough to know what to grab from a fairly simple code sample I found (Internet/Weather at master from commonsguy's cw-android - GitHub).

    Also, I had never used java before, so that probly factored into the learning curve.
  4. Senior Droid
    silverx10's Avatar
    Member #
    7876
    Join Date
    Nov 2009
    Location
    Virginia Beach, VA
    Posts
    199
    Liked
    1 times
    Phone
    Motorola DROID
    #4
    Outstanding.

    I think that I'm finally starting to get the hang of the code, myself. 'specially them thar overrides and such. Problem now is getting to know the Android API library to an intimate enough level that I know which to import, and which to overlook.
    "The only verdict is vengence; a vendetta, held as a votive, not in vain, for the value and voracity of such shall one day vindicate the vigilant and the virtuous."

Links

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Similar Threads

  1. QR Code Tip
    By mivok in forum Android General Discussions
    Replies: 5
    Last Post: 10-15-2012, 09:12 AM
  2. bar code scanning grocery list
    By Droid4me in forum Android Applications
    Replies: 29
    Last Post: 04-08-2012, 09:01 AM
  3. Android 2.0 Source Code Available
    By JohnDroid in forum Android News
    Replies: 5
    Last Post: 11-30-2010, 07:32 AM
  4. Security Code
    By sjleader in forum HTC Droid Eris
    Replies: 2
    Last Post: 12-12-2009, 03:03 PM
  5. SMS Area Code Problem
    By neal121 in forum Android Tech Support
    Replies: 6
    Last Post: 11-27-2009, 10:54 AM

Search tags for this page

android get location

,

android get location example

,
android getlocation
,
android location example
,
android location sample code
,
android locationlistener example
,
android onlocationchanged example
,

locationlistener example

,
onlocationchanged android example
,
onlocationchanged example
Click on a term to search our site for related topics.

Tags for this Thread

Find us on Google+