我正在尝试查找纬度和经度的代码,但需要大量时间才能给出坐标,或者根本没有给出坐标,可能是什么问题。我正在三星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