public
class
MainActivity
extends
Activity
implements
OnClickListener {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
loadGps();
getLocation();
}
public
void
loadGps() {
Log.w(
"tag"
,
"loadGPS"
);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria =
new
Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(
false
);
criteria.setBearingRequired(
false
);
criteria.setSpeedRequired(
false
);
criteria.setCostAllowed(
true
);
provider = locationManager.getBestProvider(criteria,
true
);
listener =
new
GpsLocationListener();
locationManager.requestLocationUpdates(provider,
10000
,
10
, listener);
}
public
Location getLocation() {
Location location = locationManager.getLastKnownLocation(provider);
if
(location ==
null
) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.w(
"tag"
,
"get Location from NETWORK provider"
);
place=
""
;
}
else
if
(location !=
null
){
double
latitude = location.getLatitude();
double
longitude = location.getLongitude();
location.getLatitude();
location.getLongitude();
String Text =
"My current location is:"
+
"Latitud ="
+location.getLatitude()
+
"Longitud ="
+location.getLongitude();
List<Address> addresses=
null
;
Geocoder gc =
new
Geocoder(MainActivity.
this
, Locale.getDefault());
try
{
addresses = gc.getFromLocation(latitude, longitude,
5
);
StringBuffer sb =
new
StringBuffer();
if
(addresses.size() >
0
) {
for
(Address addr : addresses) {
for
(
int
i=
0
;i < addr.getMaxAddressLineIndex();i++)
sb.append(addr.getAddressLine(i)).append(
"<< \n\n"
);
}
Address address = addresses.get(
0
);
sb.append(address.getAddressLine(
0
)).append(
" "
);
place = sb.toString();
Toast.makeText(MainActivity.
this
, place,
2000
).show();
}
else
{
place=
""
;
}
}
catch
(Exception e){
e.printStackTrace();
place=
""
;
}
}
return
location;
}