Im new to android and i am trying to make a program that outputs the longitude and latitude integers to the screen inside text fields.
Ive have two edit text fields:
<EditText
android:id="@+id/longitudeField"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/latitudeField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
I know have a java file that runs a class called EmergencyLocation. In this file i set the longitude and latitude and try to output these to the screen via the edit text fields but every time i do this nothing comes up.
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Context;
import android.location.LocationManager;
import android.location.Criteria;
public class EmergencyLocation extends Activity implements LocationListener
{
private TextView latitudeField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
latitudeField = (TextView) findViewById(R.id.latitudeField);
//logitudeField = (TextView) findViewById(R.id.????);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
locationManager.requestLocationUpdates(provider, 0, 0, this);
Location location = locationManager.getLastKnownLocation(provider);
onLocationChanged(location);
}
@Override
public void onDestroy()
{
super.onDestroy();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location)
{
if(location != null)
{
System.out.println("Provider " + provider + " has been selected.");
int lat = (int) (location.getLatitude());
//int lng = (int) (location.getLongitude());
latitudeField.setText(String.valueOf(lat));
//longitudeField.setText(String.valueOf(lng));
}
else
{
latitudeField.setText(String.valueOf("Provider not avaliable"));
//the same with long stuff
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
}
as i said before im new to android and i think it is something very simple im missing.
thanks in advance
getLatitude actually returns a double. Did you try that ?
I've tested your app. It is working and location is updating. May be you forget to specify LOCATION- permisions in AndroidManifest.xml?
Also, here:
provider = locationManager.getBestProvider(criteria, false);
provider may be null. In this case you will receive an exception while trying to do
locationManager.requestLocationUpdates(provider, 0, 0, this);
So, you should check provider for "null" before calling requestLocationUpdates() .
Also, is there any problem with longitude?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.