getLastKnownLocations始终返回null [英] getLastKnownLocations always returns null

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

问题描述

我无法对我的Andr​​oid设备(三星Galaxy Note N7000),因为它总是空,塔上getLastKnownLocations被检测为网络,它是好的,任何人有任何想法如何让我的位置?

  D = getResources()getDrawable(R.drawable.songs_gray)。    //在位置精确放置
    LM =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);
    标准暴击=新标准();    塔= lm.getBestProvider(暴击,FALSE);    Toast.makeText(LocationActivity.this,塔:+塔,Toast.LENGTH_SHORT).show();    地点= lm.getLastKnownLocation(塔);    Toast.makeText(LocationActivity.this的位置:+位置,Toast.LENGTH_SHORT).show();    如果(位置!= NULL){
        纬度=(INT)(location.getLatitude()* 1E6);
        隆基=(INT)(location.getLongitude()* 1E6);
        GeoPoint的ourLocation =新的GeoPoint(LAT,隆基);
        OverlayItem overlayItem =新OverlayItem(ourLocation,怎么了,第二届字符串);
        CustomPinpoint定制=新CustomPinpoint(D,LocationActivity.this);
        custom.insertPinPoint(overlayItem);
        overlayList.add(自定义);
    }其他{
        Toast.makeText(LocationActivity.this,无法获得供应商,Toast.LENGTH_SHORT).show();
    }

清单

 <使用许可权的android:NAME =android.permission.ACCESS_FINE_LOCATION/>
<使用许可权的android:NAME =android.permission.ACCESS_MOCK_LOCATION/>


解决方案

如果位置已被另一个应用程序拨打电话前,确定的最后一个已知位置才可用。启动地图或其他一些应用程序,让它的位置锁定。然后运行你的应用程序。

如果你真的需要的在您的应用程序的位置,你需要回落到一些其他的方法,因为你不能依赖于那里是最后一个已知位置。

作为一个方面说明, ACCESS_MOCK_LOCATION 这里不需要。从文档,


  

允许应用程序测试创建模拟位置提供


I am unable to getLastKnownLocations on my Android device (Samsung Galaxy Note N7000) as it is always null, towers is detected as network so it is fine, anybody got any idea how to get my location?

    d = getResources().getDrawable(R.drawable.songs_gray);

    //Placing pinpoint at location
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria crit = new Criteria();

    towers = lm.getBestProvider(crit, false);

    Toast.makeText(LocationActivity.this, "Towers: " + towers, Toast.LENGTH_SHORT).show();

    Location location = lm.getLastKnownLocation(towers);

    Toast.makeText(LocationActivity.this, "Location: " + location, Toast.LENGTH_SHORT).show();

    if(location !=null){
        lat = (int) (location.getLatitude()*1E6);
        longi = (int) (location.getLongitude()*1E6);
        GeoPoint ourLocation = new GeoPoint(lat, longi);
        OverlayItem overlayItem = new OverlayItem(ourLocation, "What's up", "2nd String");
        CustomPinpoint custom = new CustomPinpoint(d, LocationActivity.this);
        custom.insertPinPoint(overlayItem);
        overlayList.add(custom);    
    }else{
        Toast.makeText(LocationActivity.this, "Couldn't get provider", Toast.LENGTH_SHORT).show();
    }

Manifest

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

解决方案

the last known location is only available if the location has been determined by another app prior to your call. start maps or some other app and let it get a location lock. then run your app.

if you really need location in your app, you need to fall back to some other method, because you cannot depend on there being a last known location.

as a side note, ACCESS_MOCK_LOCATION isn't required here. from the docs,

Allows an application to create mock location providers for testing

这篇关于getLastKnownLocations始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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