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
}
}