how to get gps location

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

how to get gps location

Postby Kenan89 » Mon May 17, 2010 7:43 pm

Hello

I am trying to make a simple application, that turns on the GPS device and sends my current location/coordinates to my email address.

I have been looking at this code, which works fine:

http://www.firstdroid.com/2010/04/29/an ... mment-2247

the only problem is, that i dont want a listener that updates the coordinates every time they change, all the time. Is there a way to set a delay on this?
What i want is to make the program send the coordinates every hour in example.

I have looked at the getLastKnownPosition method, but i dont know how often it updates the coordinates. The application is supposed to send Current coordinates, so it's not good if they are out of date.

I have also tried making a thread, that creates a new object of a class that get's the current posiotion (using the listener), making the thread send the email, then remove the reference to the class so it can get garbage collected, and sleep for 60 minutes. Couldnt get it to work though.

my code:

main class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class HelloAndroid extends Activity {
  2.        
  3.         public String latitude = null;
  4.         public String longtitude = null;
  5.        
  6.     /** Called when the activity is first created. */
  7.     @Override
  8.     public void onCreate(Bundle savedInstanceState) {
  9.        
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.main);
  12.          
  13.         ThreadClass tc = new ThreadClass();
  14.         new Thread(tc).start();
  15.    
  16.     }
  17.  
  18. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


class that gets the current position:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.app.Activity;
  2. import android.content.Context;
  3. import android.location.Location;
  4. import android.location.LocationListener;
  5. import android.location.LocationManager;
  6. import android.os.Bundle;
  7. import android.widget.Toast;
  8.  
  9. public class GetLocation extends Activity {
  10.        
  11.         public String latitude;
  12.         public String longitude;
  13.        
  14.        
  15.         public void onCreate(Bundle savedInstanceState) {
  16.                
  17.                 super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.main);      
  19.        
  20.         LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);     
  21.         LocationListener mlocListener = new MyLocationListener();
  22.         mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
  23.        
  24.         }
  25.        
  26.         public String getLatitude() {
  27.                 return latitude;
  28.         }
  29.        
  30.         public String getLongtitude() {
  31.                 return longitude;
  32.         }
  33.        
  34.        
  35.          public class MyLocationListener implements LocationListener
  36.             {  
  37.  
  38.                         @Override
  39.                
  40.                         public void onLocationChanged(Location loc) {
  41.                                
  42.                                 latitude = "" +loc.convert(loc.getLatitude(), 0);
  43.  
  44.                                 longitude = "" + loc.convert(loc.getLongitude(), 0);
  45.                                
  46.                                 String Text = "Latitude: "+ loc.convert(loc.getLatitude(), 0) + "\nLongtitude: " + loc.convert(loc.getLongitude(), 0) + "\nBearing: "+ loc.getBearing();
  47.                                
  48.                                 Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_LONG).show();
  49.                                
  50.                                 // TODO Auto-generated method stub
  51.                                
  52.                         }
  53.  
  54.                         @Override
  55.                         public void onProviderDisabled(String provider) {
  56.                                 // TODO Auto-generated method stub
  57.                                
  58.                         }
  59.  
  60.                         @Override
  61.                         public void onProviderEnabled(String provider) {
  62.                                 // TODO Auto-generated method stub
  63.                                
  64.                         }
  65.  
  66.                         @Override
  67.                         public void onStatusChanged(String provider, int status, Bundle extras) {
  68.                                 // TODO Auto-generated method stub
  69.                                
  70.                         }
  71.                
  72.             }
  73.        
  74.        
  75. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


the thread class which i was talkiing about:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class ThreadClass implements Runnable {
  2.        
  3.         public String latitude;
  4.         public String longitude;
  5.         public GetLocation gl;
  6.        
  7.         public ThreadClass() {
  8.                 this.gl = new GetLocation();   
  9.         }
  10.  
  11.         @Override
  12.         public void run() {
  13.                
  14.                 while(true) {
  15.                 try {
  16.                         Thread.sleep(60000);
  17.                         latitude = gl.getLatitude();
  18.                         longitude = gl.getLongtitude();
  19.                         SendMail.getInstance().sendEmail(latitude, longitude);
  20.                        
  21.                 } catch (InterruptedException e) {
  22.                         // TODO Auto-generated catch block
  23.                         e.printStackTrace();
  24.                 } catch (Exception e) {
  25.                         // TODO Auto-generated catch block
  26.                         e.printStackTrace();
  27.                 }
  28.                
  29.                 // TODO Auto-generated method stub
  30.                
  31.         }
  32.        
  33.         }
  34. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


any suggestions on how to get this to work?
BTW:
When i create the GetLocation class from the thread, it doesnt activate the GPS and doesnt update the longitude and latitude variables. So i only recieve null in the email.
PS:
I am new to android development.
Kenan89
Once Poster
Once Poster
 
Posts: 1
Joined: Mon May 17, 2010 7:37 pm

Top

Re: how to get gps location

Postby Funkeeh » Wed May 26, 2010 6:04 pm

You can decide when the requestLocationUpdates should get new data input from the locationListener:

locationManager lm;
lm.requestLocationUpdates(bestProvider, time, distanceTimer, loc);

Dunno if this help you.. hehe :p
Funkeeh
Freshman
Freshman
 
Posts: 3
Joined: Tue May 25, 2010 9:57 pm
Location: Aalborg, Denmark

Re: how to get gps location

Postby wahaneebelly » Fri May 28, 2010 5:40 am

Woah woah...this is WAY too complicated.. Well without the gps turned on the phone will try and use nearby cell tower and nearby wifi points to guess where you are. .you can get a "My Map" for google maps which will do this. I'm not sure how to link those, but if you go to google maps, then the my maps tab. From there, choose "browse the directory".
[url=http://www.zoombits.co.uk/memory-cards/micro-sd]16gb micro sd cards[/url]
wahaneebelly
Freshman
Freshman
 
Posts: 4
Joined: Sat May 08, 2010 7:37 am

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 3 guests