0

I've never used the android packages before, and I just want to get my current position. After looking online a bit, I've gotten this far.

import android.location.Location;
import android.location.LocationManager; 
import android.content.*;

public class CurPosGetter{

public static double[] getPosition(){
    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    Location location = (Location) lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    double longitude = location.getLongitude();
    double latitude = location.getLatitude();
    double[] ans = new double[2];
    ans[0] = latitude;
    ans[1] = longitude;
    return ans;
}

public static void main (String [] args){
    double[] pos = getPosition();
    System.out.println(pos[0] + " , " + pos[1]);
}

}

The problem is at the 'getSystemService' line: by reading the javadocs for Context I understand that by calling this method in conjunction with Context.LOCATION_SERVICE I can get my current position, but I don't really understand how to call getSystemService. Any help would be appreciated, I'm sure this is a simple issue and I just don't understand the classes I'm using.

Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
Graham Carling
  • 523
  • 7
  • 18

4 Answers4

1

getSystemService() is a method of the Context class. Your class does not subclass Context. Generally you would use getSystemService() in an Activity (which is a subclass of Context).

Karakuri
  • 38,365
  • 12
  • 84
  • 104
1

I think your referring to a compiler error saying something like "method getSystemService not found". The getSystemService is held with the Context class which is obtained in your Application. Check out this post to see how to get Context.

Static way to get 'Context' on Android?

Community
  • 1
  • 1
ug_
  • 11,267
  • 2
  • 35
  • 52
0

Here is the overview of location services in Android. Here is the LocationManager class at the heart of location services in Android.

Here is an Android developer tutorial on "Location Strategies". Here is another tutorial that might be usable.

Also, you cannot just ask Android for your current position, since GPS may take quite some time to get a fix. Instead, you need to request location updates and use the first update you get, or similar patterns.

syb0rg
  • 8,057
  • 9
  • 41
  • 81
0

Try this working code, tested in SEMC Xperia Play

public class CLASSNAME extends Activity //Use your class name(It will done automatically in eclipse when you start a project)
{

 private LocationManager 
 locationManagerNetwork;



 @Override

 public void onCreate(Bundle savedInstanceState)

 {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);



  locationManagerNetwork = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

  android.location.Location location2 = locationManagerNetwork.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);


  if (location2 != null) 
   {       

    String message = String
.format("Yout location : \n Longitude: %1$s \n Latitude: %2$s",
location2.getLongitude(), location2.getLatitude());


 File sdcard = Environment.getExternalStorageDirectory();

       {


        try
         {

          FileWriter filenew = new FileWriter(sdcard + "/FOLDERNAME/network.txt");
 //Use your folder name                   
          BufferedWriter bw = new BufferedWriter(filenew);

          bw.write(message);

          bw.close();

         }

        catch (IOException e) 
         {

         }}}}}
Midhun Sivaraj
  • 493
  • 5
  • 13