做了个获取GPS的小demo,循环取GPS的数据,发现每次取经纬度和时间都是一样 不知为何,请教各位高手了。。

解决方案 »

  1.   

    确信你的GPS位置更新了吗?可以在模拟器里模拟位置改变的,否则,都是最后一次的定位信息
      

  2.   

    是用模拟器测试还是真机测试?模拟器无法测实际经纬度的,只能靠手动输入进去的固定值。真机的话,检查一下代码,看看是不是事件监听没有做。同时LOG出来看一下。
      

  3.   

    打开gps了吗?有时显示是打开,但未真正的起作用。
      

  4.   

    locationManager.requestLocationUpdates(provider, 1000, 0, new LocationListener() {

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

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {
    updateToNewLocation(null);
    }

    @Override
    public void onLocationChanged(Location location) {
    updateToNewLocation(location);//更新GPS
    }
    });
        }
        
    private void updateToNewLocation(Location location) {
    TextView tv;
    String text;
         tv = (TextView) findViewById(R.id.textv);
         if (location != null) {
         lat = location.getLatitude();
         lon= location.getLongitude();
         text="纬度:"+lat+"\n经度:"+lon;
         }
         else {
         text="无法获取地理信息";
         }
         tv.setText(text);
         }
      

  5.   


    public class GpsManager implements GpsStatus.Listener, LocationListener { private static Context context;
    private static GpsListener call;
    private List<GpsSatellite> numSatelliteList = new ArrayList<GpsSatellite>(); // 卫星信号
    private static GpsManager mGpsListener;
    public void RegisteListener(Context _context, GpsListener _call){

    context = _context; call = _call;

    getLocation();
    } LocationManager locationManager; private void getLocation() {
    // 获取位置管理服务
    String serviceName = Context.LOCATION_SERVICE;
    locationManager = (LocationManager) context
    .getSystemService(serviceName);

    // 获取GPS信息
    String provider = LocationManager.GPS_PROVIDER; Location location = locationManager.getLastKnownLocation(provider);// 通过GPS获取位置 if (location == null)
    location = locationManager
    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); // updateToNewLocation(location);
    // 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000)或最小位移变化超过N米
    locationManager.requestLocationUpdates(provider, 1000, 0, this);
    locationManager.addGpsStatusListener(this); // 注册状态信息回调
    } private void updateToNewLocation(Location location) {
    // 获取系统时间
    Time t = new Time();
    t.setToNow(); // 取得系统时间
    int year = t.year;
    int month = t.month + 1;
    int date = t.monthDay;
    int hour = t.hour; // 24小时制
    int minute = t.minute;
    int second = t.second; if (location != null) {
    double latitude = location.getLatitude();// 经度
    double longitude = location.getLongitude();// 纬度
    double altitude = location.getAltitude(); // 海拔 call.SetSatellite("搜索卫星个数:" + numSatelliteList.size());
    call.SetLocation(new GPSPoint(longitude, latitude, 0, 0, 0, 0, GPSPoint.TYPE_CART));
    call.SetLatestUpdateTime("/n时间:" + year + "年" + month + "月" + date + "日"
    + hour + ":" + minute + ":" + second);
    } else { call.SetSatellite("无法获取地理信息");
    }
    } public void onLocationChanged(Location location) {
    // 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
    if (location != null) {
    updateToNewLocation(location); }
    } public void onProviderDisabled(String provider) {
    // Provider被disable时触发此函数,比如GPS被关闭
    updateToNewLocation(null);
    } public void onProviderEnabled(String provider) {
    // Provider被enable时触发此函数,比如GPS被打开
    } public void onStatusChanged(String provider, int status, Bundle extras) {
    // Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
    } @Override
    public void onGpsStatusChanged(int event) {
    GpsStatus status = locationManager.getGpsStatus(null); // 取当前状态
    updateGpsStatus(event, status);
    } private void updateGpsStatus(int event, GpsStatus status) {
    if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
    int maxSatellites = status.getMaxSatellites();
    Iterator<GpsSatellite> it = status.getSatellites().iterator();
    numSatelliteList.clear();
    int count = 0;
    while (it.hasNext() && count <= maxSatellites) {
    GpsSatellite s = it.next();
    numSatelliteList.add(s);
    count++;
    }
    }
    } public interface GpsListener { // public void RegisteCallback();
    public void SetLocation(GPSPoint point); public void SetSatellite(String numberInfo);

    public void SetLatestUpdateTime(String time); }
    }