i am working on a project that allows the user to add/update/delete records from a MySQL database. The app successfully uploads and retrieves all information from the database. I am trying to get the lat and long to be displayed itself as soon as i send the values from emulator control instead of the user having to manually put the values in themselves via a textfield which was how i originally programmed it to be. After they are displayed the values should be inserted into the database along with the other textfields values. I hope that makes sense.. Now when i send the values from the emulator control they are not being displayed in the textView and im not sure why? Any help would be highly appreciated.
UPDATE: This is where the lat and long views are being initialised and should be getting the values from the emulator control?
//Defining views
//private EditText editTextLatitude;
//private EditText editTextLongitude;
private EditText editTextTimeInserted;
private EditText editTextUserID;
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
private Button buttonAdd;
private Button buttonView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing views
latituteField = (TextView) findViewById(R.id.TextView02);
longitudeField = (TextView) findViewById(R.id.TextView04);
//editTextLatitude = (EditText) findViewById(R.id.editTextLat);
//editTextLongitude = (EditText) findViewById(R.id.editTextLon);
editTextTimeInserted = (EditText) findViewById(R.id.editTextTimeInserted);
editTextUserID = (EditText) findViewById(R.id.editTextUserID);
buttonAdd = (Button) findViewById(R.id.buttonAdd);
buttonView = (Button) findViewById(R.id.buttonView);
//Setting listeners to button
buttonAdd.setOnClickListener(this);
buttonView.setOnClickListener(this);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the location provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
}
public void onLocationChanged(final Location location) {
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
}
答案 0 :(得分:1)
Add in manifest.xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(Bundle connectionHint) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
}
}