简体   繁体   English

android获取当前位置失败

[英]android get current location failed

I wan't to get my current location,but in most phones it's failed. 我不希望获得当前位置,但是在大多数手机中,它都失败了。 Next is mycode: 接下来是mycode:

public Location getLocation(Context context)
{
    LocationManager locMan = (LocationManager) context
        .getSystemService(Context.LOCATION_SERVICE);
    Location location = locMan
        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location == null)
    {
        location = locMan
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    }

    return location;
}    

Try using this...might be possible that locationmanager doesn't have your last location 尝试使用此功能... locationmanager可能没有您的最后位置

/** * this function is used to instantiate location manager and bind it to GPS provider. / ** *此函数用于实例化位置管理器并将其绑定到GPS提供程序。 * location manager will request for location updates after every 5 minutes. *位置经理将每5分钟请求一次位置更新。 */ * /

private void subscribeToLocationUpdates() {
      this.lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
      this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000, 0, this);
}

public void onLocationChanged(Location location) {
    if(location != null){

        //make use of this location

    }       
}

First of all - did you set the right permission? 首先-您是否设置了正确的权限? The location permissions are implicit - so if you request fine location, you'll get coarse, too. 位置权限是隐式的-因此,如果您请求精确的位置,则也会变得很粗糙。

Did you follow this guide(s) for getting your location? 您是否遵循指南来获取您的位置?

Normally you don't set up a LocationService each time you want to get the current Location, too. 通常,您也不希望每次都想获取当前位置时就设置LocationService。 Did you try to request location updates for yourself? 您是否尝试自己请求位置更新?

There is too less code and the question is too unspecified to really help you, sorry. 对不起,代码太少了,问题还没有明确说明,无法真正帮助您。

在清单文件中添加权限ACCESS_FINE_LOCATION

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM