Finding Own Current Location

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

Finding Own Current Location

Postby mayanks » Mon Jun 23, 2008 12:45 pm

Hello PlusMinus,

I am a new to android

I am trying to find my current location using the following piece of code:
********************************************************************************
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  2.  
  3.                 Location location = locationManager.getCurrentLocation("gps");
  4.  
  5.                
  6.  
  7.                 final String  latitudeString = String.valueOf( location.getLatitude());
  8.  
  9.                 final String longitudeString = String.valueOf( location.getLongitude());
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


********************************************************************************


But at line "Location location = locationManager.getCurrentLocation("gps"); " , I am getting a null pointer exception. In debug perspective of eclipse, I am getting following exception

********************************************************************************
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. DEBUG/dalvikvm(517): Exception Ljava/lang/NullPointerException; from LocationManagerService.java:690 not caught locally
  2.  
  3. ERROR/JavaBinder(517): *** Uncaught remote exception!  (Exceptions are not yet supported across processes.)
  4.  
  5. ERROR/JavaBinder(517): java.lang.NullPointerException
  6.  
  7. ERROR/JavaBinder(517):     at android.server.LocationManagerService.getCurrentLocation(LocationManagerService.java:690)
  8.  
  9. ERROR/JavaBinder(517):     at android.location.ILocationManager$Stub.onTransact(ILocationManager.java:149)
  10.  
  11. ERROR/JavaBinder(517):     at android.os.Binder.execTransact(Binder.java:274)
  12.  
  13. ERROR/JavaBinder(517):     at android.dalvik.NativeStart.run(Native Method)
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

********************************************************************************



For gps, I have also provided user permissions in Mainifest.xml file as follows:
********************************************************************************
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. package="com.abc.move">
  2.  
  3.  <uses-permission android:name="android.permission.ACCESS_LOCATION"></uses-permission>
  4.  
  5. <uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission>
  6.  
  7. <uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS"></uses-permission>
  8.  
  9. <uses-permission android:name="android.permission.ACCESS_CELL_ID"></uses-permission>
  10.  
  11.      <application android:icon="@drawable/icon">
Parsed in 0.002 seconds, using GeSHi 1.0.8.4

********************************************************************************


Is there something wrong in my code, or am i missing some configuration for this?
Is it mandatory to create mock LocationProviders for it.
If yes, relative to which path we need to put "/data/misc/location/<provider_name>/abc.kml"


Thanks in advance,
Mayank
mayanks
Freshman
Freshman
 
Posts: 3
Joined: Mon Jun 23, 2008 12:29 pm

Top

Postby shiteiru » Mon Jun 23, 2008 5:55 pm

try out this piece of code........should work....at least it does for me


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. import android.location.Location;
  5.  
  6. import android.location.LocationManager;
  7.  
  8. import java.text.DecimalFormat;
  9.  
  10.  
  11.  
  12. protected LocationManager myLocationManager = null;
  13.  
  14. protected Location myLocation = null;
  15.  
  16.  
  17.  
  18. this.myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  19.  
  20. this.myLocation = this.myLocationManager.getCurrentLocation("gps");
  21.  
  22.  
  23.  
  24. DecimalFormat df = new DecimalFormat("#00.000000");
  25.  
  26.  
  27.  
  28. String lat_str = df.format(this.myLocation.getLatitude());
  29.  
  30. String lng_str = df.format(this.myLocation.getLongitude());
  31.  
  32.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
shiteiru
Junior Developer
Junior Developer
 
Posts: 23
Joined: Tue May 20, 2008 7:16 pm

Postby mayanks » Tue Jun 24, 2008 6:09 am

Hello Shiteiru,

Thank you for your help.
The code worked fine.

Regards,
Mayank
mayanks
Freshman
Freshman
 
Posts: 3
Joined: Mon Jun 23, 2008 12:29 pm

What's the diffenence?

Postby liaowg » Thu Oct 09, 2008 6:19 am

shiteiru wrote:try out this piece of code........should work....at least it does for me


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import android.location.Location;
  3. import android.location.LocationManager;
  4. import java.text.DecimalFormat;
  5.  
  6. protected LocationManager myLocationManager = null;
  7. protected Location myLocation = null;
  8.  
  9. this.myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  10. this.myLocation = this.myLocationManager.getCurrentLocation("gps");
  11.  
  12. DecimalFormat df = new DecimalFormat("#00.000000");
  13.  
  14. String lat_str = df.format(this.myLocation.getLatitude());
  15. String lng_str = df.format(this.myLocation.getLongitude());
  16.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


What's the difference between this and the not-working code?
liaowg
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Jun 03, 2008 7:26 am

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 3 guests