getLastknownLocation()在 nexus 返回null值

原问题来自于CSDN问答频道,更多解决方案见:http://ask.csdn.net/questions/1730

问题描述:

我使用下面的代码来开发一个基于位置定位的项目,这个项目我是用的是google api 8。

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    currloc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    TextView t = (TextView)findViewById(R.id.textView1);
    try{
        t.setText("Your current location is - "+currloc.getLatitude()+","+currloc.getLongitude());
    }catch (Exception e) {
        // TODO: handle exception
        t.setText("cant find current location ");
    }


这段代码在galaxy tab上能正常运行,htc上也可以。
但是当我使用nexus的时候,location得到的是null。对于galaxy nexus来说,我是不是得改变api版本,或者说还有其他一些特殊需求吗?

解决方案:

仿照下面的代码:

Step1: into your oncreate

LocationListener locationListener = new LocalLocationListener();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

Step2: into class body

/**Listener on location change*/
private class LocalLocationListener implements LocationListener
{

    public void onLocationChanged(Location location)
    {
        String text = "My current Location is: "+location.getLatitude()+", "+location.getLongitude();
        GeoPoint geoPoint = new GeoPoint((int)(location.getLatitude()* 1E6), (int)(location.getLatitude() * 1E6));
        mapController_.animateTo(geoPoint);
        Toast.makeText(LocalMap.this, text, Toast.LENGTH_SHORT).show();
        Log.i("onLocationChanged", text);

    }

    public void onProviderDisabled(String provider)
    {
        // TODO Auto-generated method stub
        Toast.makeText(LocalMap.this, "GPS Disable", Toast.LENGTH_SHORT).show();
        Log.i("onProviderDisabled", "GPS Disable");
    }

    public void onProviderEnabled(String provider)
    {
        // TODO Auto-generated method stub
        Toast.makeText(LocalMap.this, "GPS Enable", Toast.LENGTH_SHORT).show();
        Log.i("onProviderEnabled", "GPS Enable");
    }

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

    }


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值