Basic GPS info - latitude & longitude

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

Basic GPS info - latitude & longitude

Postby svebee » Sat Apr 24, 2010 11:00 pm

Everything I need is basic GPS info - I need latitude and longitude (of my current position) in double type variables (so I can manipulate with them later).

What's the shortest way? I tried everything but always some error(s) :?
svebee
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Apr 21, 2010 10:19 pm

Top

Postby hkswaroop » Tue Apr 27, 2010 6:29 am

private LocationManager lm;
private LocationListener ll;

lm=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
ll=new LocationListener()
{
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
@Override
public void onProviderEnabled(String arg0) {
}

@Override
public void onProviderDisabled(String arg0) {
}

@Override
public void onLocationChanged(Location l) {
double lat=l.getLatitude();
double lon=l.getLongitude();

gp=new GeoPoint((int)(lat*1E6), (int)(lon*1E6)); mc.animateTo(gp);

}
};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
hkswaroop
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed May 20, 2009 8:15 am
Location: Bangalore

Postby svebee » Thu Apr 29, 2010 4:48 pm

This seems not working, why? :?
(It happens when I send coordinates to the emulator)

Code: Select all
04-29 15:44:00.704: ERROR/AndroidRuntime(864): Uncaught handler: thread main exiting due to uncaught exception
04-29 15:44:00.713: ERROR/AndroidRuntime(864): java.lang.NullPointerException
04-29 15:44:00.713: ERROR/AndroidRuntime(864):     at com.transport.NalazimSe$4.onLocationChanged(NalazimSe.java:284)
04-29 15:44:00.713: ERROR/AndroidRuntime(864):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:179)
04-29 15:44:00.713: ERROR/AndroidRuntime(864):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:112)
04-29 15:44:00.713: ERROR/AndroidRuntime(864):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:128)
04-29 15:44:00.713: ERROR/AndroidRuntime(864):     at android.os.Handler.dispatchMessage(Handler.java:99)

...

It says that error is on the line
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. mojGPS.setText(String.valueOf(Data));
Parsed in 0.029 seconds, using GeSHi 1.0.8.4


*... means I removed buttons and similar stuff so the code is much more readable

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class NalazimSe extends Activity {
  2.        
  3. TextView mojGPS;
  4.  
  5. MapView mapView;
  6. MapController mc;
  7. GeoPoint p;
  8.  
  9. ...
  10.  
  11. /** Called when the activity is first created. */
  12.  @Override
  13.  public void onCreate(Bundle savedInstanceState) {
  14.   super.onCreate(savedInstanceState);
  15.   setContentView(R.layout.nalazimse);
  16.  
  17.   ...
  18.  
  19.   final LocationManager lm;
  20.   final LocationListener ll;
  21.  
  22.   lm=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
  23.   ll=new LocationListener()
  24.   {
  25.   @Override
  26.   public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
  27.   }
  28.   @Override
  29.   public void onProviderEnabled(String arg0) {
  30.   }
  31.  
  32.   @Override
  33.   public void onProviderDisabled(String arg0) {
  34.   }
  35.  
  36.   @Override
  37.   public void onLocationChanged(Location l) {
  38.   double lat=l.getLatitude();
  39.   double lon=l.getLongitude();
  40.   String latitudeString = Double.toString(lat);
  41.   String longitudeString = Double.toString(lon);
  42.  
  43.   p = new GeoPoint((int)(lat*1E6), (int)(lon*1E6));
  44.  
  45.   String Data ="";
  46.   Data = Data + latitudeString + " - " + longitudeString + " n ";
  47.   mojGPS.setText(String.valueOf(Data));
  48.  
  49.   }
  50.   };
  51.   lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
  52.  
  53.  }
  54. }
  55.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
svebee
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Apr 21, 2010 10:19 pm

Postby Neondog » Thu Apr 29, 2010 7:36 pm

Do you think that if you set Data to equal something besides double quotations that it might help (i.e. "data goes here")? Maybe you are getting errors becuase you aren't converting the double's to a string before (or while) placing them in a string?
Neondog
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Apr 28, 2010 8:58 pm

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 2 guests