getLastknownLocation()返回在Nexus空值 [英] getLastknownLocation() returns null value on nexus

查看:96
本文介绍了getLastknownLocation()返回在Nexus空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发基于位置的项目中,我用下面的code

i am developing location based project where i am using the following code

我使用谷歌API 8项目

i am using google api 8 for the project

 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 ");
    }

这code正常工作我的Galaxy Tab上甚至在HTC

this code works fine on my galaxy tab even on htc

但是当我使用的Nexus它返回的位置空值。
我需要改变我的API级别或者是有Galaxy Nexus的任何具体要求
预先感谢您:)

but when i use nexus it returns null value for location. do i need to change my api level or is there any specific requirement for galaxy nexus thank you in advance :)

推荐答案

请遵循线code ..

Please follow the line of code..

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

    }

这篇关于getLastknownLocation()返回在Nexus空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆