我正在嘗試查找緯度和經度的代碼,但需要大量時間才能給出座標,或者根本沒有給出座標,可能是什麼問題。我正在三星Galaxy選項卡上嘗試我的代碼。android gps設備獲取緯度和經度所用的時間
LocationManager mlocManager;
double lat,lng;
LocationListener mlocListener = new MyLocationListener();
我稱之爲的onCreate下面的代碼()方法
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
監聽MyLocationListener類代碼如下
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc){
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " + "Latitude = " + loc.getLatitude() + "Longitude = " + loc.getLongitude();
String latLongString = "";
if (loc != null) {
lat = loc.getLatitude();
lng = loc.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
TextView myLocationText = (TextView)findViewById(R.id.FindUsTextView);
myLocationText.setText("Your Current Position is:\n" + latLongString);
System.out.println(latLongString);
}
/* findUsProgressbarField.setVisibility(View.INVISIBLE);*/
Toast.makeText(FindUs.this, Text, Toast.LENGTH_SHORT).show();
mlocManager.removeUpdates(mlocListener);
}
@Override
public void onProviderDisabled(String provider){
Toast.makeText(getApplicationContext(),"Gps Disabled, Please Enable the GPS", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider){
Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras){
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mlocManager != null) {
mlocManager.removeUpdates(mlocListener);
mlocManager = null;
}
}
可能是問題,任何指針或有我不考慮什麼一些觀點。 期待您的回覆。 謝謝。
聽起來像是「按設計」工作。公司並不總是可用。第一次獲得他們是昂貴的(及時)。 – 2012-01-02 13:05:33