Get Current Location Gmaps
i tried a lot of 'get current location' for android. But i think most of them are outdated, or i simply dont get it. I DONT want to set a marker.addmarker(params..) i would like to
Solution 1:
Follow this http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ . Then just place the latitude and longitude you get(using gps.getLatitude(), gps.getLongitude()) in your google map and it will show your current location.
Solution 2:
Try this code, it is worked in my application:
privateLocationManager locationManager;
privateString provider;
double lat,lon;
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locate);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 1, this);
if ( !locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
gpsalert();
}
// Criteria to select location providerLocation location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
Toast.makeText(getApplicationContext(), "Provider is available", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Provider is not available", Toast.LENGTH_SHORT).show();
}
}
@OverridepublicvoidonLocationChanged(Location location) {
// TODO Auto-generated method stubString addr,city,country;
lat=location.getLatitude();
lon=location.getLongitude();
//Toast.makeText(getApplicationContext(), "lat"+lat+"long"+lon, Toast.LENGTH_LONG).show();Geocoder geocoder;
List<Address> addresses;
geocoder = newGeocoder(this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(lat, lon, 1);
addr = addresses.get(0).getAddressLine(0);
city = addresses.get(0).getAddressLine(1);
country = addresses.get(0).getAddressLine(2);
t1.setText(addr+"\n"+city+"\n"+country);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@OverridepublicvoidonProviderDisabled(String provider) {
// TODO Auto-generated method stub//Toast.makeText(this, "Disabled provider " + provider, Toast.LENGTH_SHORT).show();//t1.setText("");
}
@OverridepublicvoidonProviderEnabled(String provider) {
// TODO Auto-generated method stub//Toast.makeText(this, "Enabled provider " + provider, Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@OverrideprotectedvoidonResume() {
super.onResume();
//locationManager.requestLocationUpdates(provider, 400, 1, this);
}
// Remove LocationListener updates@OverrideprotectedvoidonPause() {
super.onPause();
//locationManager.removeUpdates(this);
}
publicvoidgpsalert()
{
final AlertDialog.Builder builder = newAlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", newDialogInterface.OnClickListener() {
publicvoidonClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(newIntent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", newDialogInterface.OnClickListener() {
publicvoidonClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
Post a Comment for "Get Current Location Gmaps"