实现的代码如下:
public class ShowPosition extends Activity implements LocationListener{ TextView lblLatitude;
TextView txtLatitude;
TextView lblLongitude;
TextView txtLongitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showposition);

lblLatitude = (TextView)findViewById(R.id.lblLatitude);
txtLatitude = (TextView)findViewById(R.id.txtLatitude);
lblLongitude = (TextView)findViewById(R.id.lblLongitude);
txtLongitude = (TextView)findViewById(R.id.txtLongitude);

lblLatitude.setText(R.string.latitude);
lblLongitude.setText(R.string.longitude);

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

updateWithLocation(location);

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
} public void updateWithLocation(Location location){
String Latitude;
String Longitude;
if(location != null){
Latitude = "" + location.getLatitude();
Longitude = "" + location.getLongitude();
}
else{
Latitude = "定位失败";
Longitude = "定位失败";
}
txtLatitude.setText(Latitude);
txtLongitude.setText(Longitude);
} @Override
public void onLocationChanged(Location location) {
System.out.println("changed");
updateWithLocation(location);
} @Override
public void onProviderDisabled(String provider) {
updateWithLocation(null);
} @Override
public void onProviderEnabled(String provider) {

} @Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

}
在2.2的模拟器上实现的时候执行没问题,执行结果会显示在emulator中设置的坐标,但是安装到手机上之后则一直显示检测失败,请问有哪位高手能够解决我的问题?感激不尽啊~

解决方案 »

  1.   

    模拟器上是一个在模拟的环境下的GPS定位,你手机有没有支持定位的硬件环境?这有可能导致你错误的原因
      

  2.   

    手机的确有定位的功能,在设置里面有GPS定位设置,而且打开GPS之后屏幕上方会有一个雷达的标志在闪烁。
    而且我这段代码在2.2的模拟器上运行没问题,在2.3的模拟器上运行也无法定位,连onLocationChanged函数都不会触发。
      

  3.   

    onLocationChanged函数没有触发,说明location没有发生变化给location个初始值,人为去改变它的值,触发下试试
      

  4.   

    我在emulator中改变了location的坐标,在2.2的模拟器中能触发onLocationChanged这个函数,也能获取坐标,但是2.3中就不能触发onLocationChanged函数。
      

  5.   

    不是啊,我转么跑到楼下测试过啊,手机上那个GPS信号的标志也一直在闪。
      

  6.   

    1 getLastKnownLocation()可能不能马上返回值,需要多取几次。
    2 如果GPS_PROVIDER(利用卫星)不行,试试network_provider(利用基站)