0
I’m finalizing a project where will open a alertDialog. Once the user presses OK he will make a confirmation and his location will be informed by a Toast. I would like to know how I can return the location where the user is.
0
I’m finalizing a project where will open a alertDialog. Once the user presses OK he will make a confirmation and his location will be informed by a Toast. I would like to know how I can return the location where the user is.
1
Do it this way:
public class main extends AppCompatActivity {
LocationListener locationListener;
LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Faço o objeto da classe
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
assert locationManager != null;
//Requesto a localização, os parâmetros 500 e 0 são intervalo que irei pegar o localização e a variação em metros que irei pegar respectivamente
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, locationListener);
}
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
//Peguei a latitude e longitude
double lat = loc.getLatitude();
double log = loc.getLongitude();
//Agora transformo em endereço:
String cityName = null;
Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses;
try {
addresses = gcd.getFromLocation(loc.getLatitude(),
loc.getLongitude(), 1);
if (addresses.size() > 0) {
//Aqui você chama seu alert, o endereço se pega da maneira abaixo:
//Endereço = addresses.get(0).getLocality());
System.out.println(addresses.get(0).getLocality());
cityName = addresses.get(0).getLocality();
}
}catch (IOException e) {
e.printStackTrace();
}
//Depois de pegar a localização eu para o onLocationChanged
locationManager.removeUpdates(this);
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
}
Chewier than this only if I make your alertDialog kkkkkkkkkkkkkkkkkk, remember to make a method that checks if the GPS is active before trying to pick up the location, as an exception may occur if it is turned off.
Remembering that you should add in the Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Browser other questions tagged java android google-location-api
You are not signed in. Login or sign up in order to post.
kkkk Thank you very much saved dms, the Alert already ta ready kk
– Bruno SIlva
@Brunosilva then marks as accepted there to strengthen the friendship kkkk
– Woton Sampaio
of course kkk, vlw partner
– Bruno SIlva
Here is one more question, and if I want to call the Alert and depending on what he asks then yes I return the location?
– Bruno SIlva
No ok on Alert you call:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, locationListener);
– Woton Sampaio
See you again, brigade
– Bruno SIlva
You’re welcome, partner :)
– Woton Sampaio