这是我目前拥有的代码:
public class Map extends FragmentActivity implements LocationListener
{
private Location l;
private Location x;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LocationManager provider = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
l = provider.getLastKnownLocation(LocationManager.GPS_PROVIDER);
showm();
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date(l.getTime());
Toast.makeText(this, "Fix received on: " + format.format(date), Toast.LENGTH_SHORT).show();
}
void showm()
{
Toast.makeText(this, "Latitude: " + l.getLatitude() + ", Longitude: " + l.getLongitude(), Toast.LENGTH_SHORT).show();
}
@Override
public void onLocationChanged(Location location)
{
if(location != null)
x = location;
}
现在,如果我运行应用程序,则此代码provider.getLastKnownLocation
有效经度/纬度。