程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-getLastknownLocation()在 nexus 返回null值

android-getLastknownLocation()在 nexus 返回null值

編輯:編程綜合問答
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版本,或者說還有其他一些特殊需求嗎?

最佳回答:


仿照下面的代碼:

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

    }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved