hxn_217 2013-03-15 07:19 采纳率: 0%
浏览 6429
已采纳

getLastknownLocation()在 nexus 返回null值

我使用下面的代码来开发一个基于位置定位的项目,这个项目我是用的是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版本,或者说还有其他一些特殊需求吗?

  • 写回答

3条回答 默认 最新

  • yongyong_21 2013-03-15 08:33
    关注

    仿照下面的代码:

    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
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题