Get the current location on map in android… here is a solution to the problem.
Get the current location on map in android
I’m using this article: What is the simplest and most robust way to get the user’s current location on Android? Gets the current location of the device. My code is:
locationResult = new LocationResult() {
@Override
public void gotLocation(Location location) {
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
};
myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
mapController = mapView.getController();
point = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
mapController.animateTo(point);
Toast.makeText(getApplicationContext(), ("Latitude = "+latitude+" Longitude = "+longitude), Toast.LENGTH_SHORT).show();
mapController.setZoom(17);
mapView.invalidate();
Latitude and longitude are declared as global double type variables. However, after running the app, I get a blue screen. In my opinion, the problem is that my code can’t get the values for latitude and longitude. Can someone help me? Thank you.
Solution
Your question doesn’t make me feel exactly what’s wrong with your code, but if what you really need to do is get the current location of the device, I can give you a hint.
Tip: Try using a location listener that gives you latitude and longitude values if your device’s location changes.
Code:
Your activity
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
Your audience class
@Override
public void onLocationChanged(Location loc){
you are getting values
loc.getLatitude();
loc.getLongitude();
}
list license
<uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>