J'ai mis en place une application simple sur mon appareil qui obtient et affiche votre latitude et longitude GPS. Cependant, les coordonnées ne changent pas même lorsque je me déplace dans mon immeuble. Voici mon implémentation:GPS coordonnées ne changent pas lors du déplacement - Android
foodActivity.java
public class foodActivity extends Activity {
LocationListener mlocListener;
Location location;
String provider;
private double latitude = 0;
private double longitude = 0;
private Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_food);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.header_food);
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = mlocManager.getBestProvider(criteria, false);
if (provider != null && !provider.equals("")) {
location = mlocManager.getLastKnownLocation(provider);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(provider, 0, 0, mlocListener);
if (location != null) {
mHandler = new Handler();
mHandler.postDelayed(updateTask, 0);
} else {
Toast.makeText(getBaseContext(), "Location can;t be retrieved", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getBaseContext(), "No Provider found", Toast.LENGTH_SHORT).show();
}
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
TextView lats = (TextView) findViewById(R.id.tv_latitude);
TextView longs = (TextView) findViewById(R.id.tv_longitude);
latitude = loc.getLatitude();
longitude = loc.getLongitude();
lats.setText("Latitude :" + latitude);
longs.setText("Longitude :" + longitude);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "GPS Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "GPS Disabled", Toast.LENGTH_SHORT).show();
}
}
public Runnable updateTask = new Runnable(){
public void run(){
mlocListener.onLocationChanged(location);
mHandler.postDelayed(updateTask,50000);
}
};
}
activity_food.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.hkuapp.foodActivity">
<TextView
android:text="Hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_latitude"
android:layout_below="@+id/tv_longitude"
android:layout_alignParentLeft="true"
android:layout_marginTop="32dp" />
<TextView
android:text="Yo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_longitude"/>
</RelativeLayout>
"... dans mon immeuble" Le GPS ne fonctionne pas très bien dans les bâtiments, en fonction de la construction des matériaux. Le code semble correct, alors essayez dehors. – weston
Eh bien, j'ai repéré quelque chose; Vos 'Critères' sont vides. http://developer.android.com/reference/android/location/Criteria.html#Criteria() 'Le nouvel objet n'aura aucune exigence en matière de précision, de puissance ou de temps de réponse. – weston
Si vous travaillez dans une zone dangereuse ou au soleil graver facile, vous pouvez également exécuter en émulateur et [simuler le GPS] (http://stackoverflow.com/questions/2279647/how-to-emulate-gps-location-in-the-android-emulator) :) – weston