我已經在我的設備上實現了一個簡單的應用程序,獲取並顯示您的GPS經度和緯度。然而,即使我在我的建築中移動,座標也不會改變。下面是我的實現:GPS座標不會改變移動 - 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>
「...在我的建築物中」GPS在建築物中工作不太好,這取決於建築物的材料。代碼似乎沒問題,所以在外面嘗試。 – weston
好吧,我發現了一些東西;你的「標準」是空的。 http://developer.android.com/reference/android/location/Criteria.html#Criteria()'新對象對精度,功耗或響應時間沒有要求' – weston
如果您在危險區域或太陽下工作刻錄容易,你也可以運行在模擬器和[模擬GPS](http://stackoverflow.com/questions/2279647/how-emulate-gps-location-in-the-android-emulator):) – weston