代码如下:
package com.gpstest;import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;public class GpsTest extends Activity {
OnClickListener listener1 = null;
LocationListener locationlistener =null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationUpdate(locationManager);
        //按钮接受事件
        final Button gpsButton = (Button)findViewById(R.id.butt);
        gpsButton.setOnClickListener(listener1);
        listener1 = new OnClickListener(){ @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
LoadCoords(locationManager);
}
        
        };
      
    } private void LocationUpdate(LocationManager locationManager) {
// TODO Auto-generated method stub
locationlistener= new LocationListener(){ @Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if(location != null){
Log.e("SupperMap", "Location changed : Lat:"+location.getLatitude()+"Lng:"+location.getLongitude());

}else{
Log.e("faild", "没有卫星地址!!"); }
} @Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

} @Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

} @Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub

}

};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationlistener);
}
private void LoadCoords(LocationManager locationManager) {
// TODO Auto-generated method stub
TextView latText = (TextView) findViewById(R.id.Latitude);
TextView lngText = (TextView) findViewById(R.id.Langitude);
Location location = locationManager.getLastKnownLocation("gps");
if(null == location){
setTitle("定位信息失败");

}else{
latText.setText(Double.toString(location.getLatitude()));
lngText.setText(Double.toString(location.getLongitude()));
}
}
}可是却获取不到一直都是“定位信息失败”。,到底是哪里出问题,
后来我看了下,好像又要需要导入什么KML文件什么的。大家有没知道的?还是怎么解决?

解决方案 »

  1.   

    模拟器无法获取真实的 GPS 经纬度,使用真机可以。
      

  2.   

    设置-->位置和安全-->使用GPS卫星
    打上勾
      

  3.   

    使用模拟器的话,要使用eclipse向模拟器发送GPS数据。
    如果是真机,则不能在室内测试,一定要在室外才能测试,当然你要将GPS功能开启才行
      

  4.   


    对,记得模拟器通过QEMUD这个DEAMON进程来中转数据的.
      

  5.   

    可是我用模拟器发送了之后还是取得null值。。中么办
      

  6.   

    到目前,我知道的问题关键是,代码是没什么问题,就是模拟器无法得到GPS开启这样一个东西,比如我打开ddms点击send之后。还是取null。就是模拟器的状态栏也没有显示gps系统的东西。在看到的资料中有显示。