0

I have been doing android programing for the last 3 months but I'm having a hard time on this problem.

My task: I want my device to be able to get its current location (latitude and longitude) and pass it to another class where I have a bitmap. The bitmap is a Map I drew myself. So does anyone know a simple way to pass latitude and longitude coordinates to another class?

Note: once I get the coordinates and pass them to the other class I know how to draw on my bitmap. I'm working with eclipse and java btw. And I wish to do this without using phonegap.

EDIT : I have used the following code (where i found it in a tutorial) and when i use it i can view the location. The problem is i do not know how he gets the location exactly. So can anybody pls explain to me how can i use this code to pass the location to another class?

Many thanks in advance!

package org.mh00217.SilineSnap;


public class LocationActivity extends MapActivity implements LocationListener { //<1>

      private static final String TAG = "LocationActivity";



      LocationManager locationManager; //<2>
      Geocoder geocoder; //<3>
      TextView locationText;
      MapView map;  
      MapController mapController; //<4>

    @Override
      public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.location);

        locationText = (TextView)this.findViewById(R.id.lblLocationInfo);
        map = (MapView)this.findViewById(R.id.mapview);
        map.setBuiltInZoomControls(true);

        mapController = map.getController(); //<4>
        mapController.setZoom(16);

        locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE); //<2>

        geocoder = new Geocoder(this); //<3>

        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); //<5>
        if (location != null) {
          Log.d(TAG, location.toString());
          this.onLocationChanged(location); //<6>

        }
      }

      @Override
      protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); //<7>
      }

      @Override
      protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this); //<8>
      }

      @Override
      public void onLocationChanged(Location location) { //<9>

        Log.d(TAG, "onLocationChanged with location " + location.toString());
        String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f", location.getLatitude(), 
                      location.getLongitude(), location.getAltitude(), location.getBearing());
        this.locationText.setText(text);

        try {
          List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10>
          for (Address address : addresses) {
            this.locationText.append("\n" + address.getAddressLine(0));
          }

          int latitude = (int)(location.getLatitude() * 1000000);
          int longitude = (int)(location.getLongitude() * 1000000);

          GeoPoint point = new GeoPoint(latitude,longitude);
          mapController.animateTo(point); //<11>

        } catch (IOException e) {
          Log.e("LocateMe", "Could not get Geocoder data", e);
        }
      }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }


    }
Bananeweizen
  • 21,797
  • 8
  • 68
  • 88
  • possible duplicate of [How do I get the current GPS location programmatically in Android?](http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android) – Philipp Reichart Apr 07 '13 at 11:18

1 Answers1

0

From the GPS receiver, you get a Location-object. It implements the Parcelable-interface.

If you want to pass this object to another Activity, you can add it to a Bundle:

Intent i = new Intent(...);
i.putExtra("Location", location); // The object.
startActivity(i);
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
  • thanks @Lukas Knuth. can you please answer related to the code i provided just now? Thanks in advance! – mariosErodotou Apr 07 '13 at 12:30
  • @mariosErodotou I don't understand what you want. Phrase it up in one sentence. – Lukas Knuth Apr 07 '13 at 13:16
  • i was wondering whether you could explain to me in related to the code that i provided above how i could posibly do what you said. – mariosErodotou Apr 07 '13 at 14:04
  • Well, add it where you start the other Activity, as shown above. In the "other" activity, you can get the value with `getIntent().getParcelableExtra("Location");` – Lukas Knuth Apr 07 '13 at 17:44