如何获得初始位置。LocationManager? [英] How to get initial location with LocationManager?

查看:262
本文介绍了如何获得初始位置。LocationManager?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到当前位置与 GoogleApiClient 到现在为止,但我只注意到它的简单得多与 LocationManager <做到这一点/ code>使用 LocationListener的,因为它甚至可以检测到何时GPS服务被打开或关闭用户。

但我后让用户第一的位置,当问题的 LocationManager 初始化。

LocationManager 有4个听众,但他们都不给你的第一个位置。它有一个 onLocationChanged 监听器,但是当你将它只能激活。

这是我如何使用它:

  //初始化LocationManager(需要跟踪,如果打开GPS或不
locationManager =(LocationManager)getApplicationContext()getSystemService(LOCATION_SERVICE)。
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,这一点);


在OnCreate结束......


/ *
LocationListener的(听,如果GPS的服务已开启/关闭)
 * /

@覆盖
公共无效onProviderEnabled(字符串提供商){
}

@覆盖
公共无效onLocationChanged(位置定位){
}

@覆盖
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
}

@覆盖
公共无效onProviderDisabled(字符串提供商){
}
 

解决方案

使用下面的方法来获取位置目标:

 公共场所的getLocation(){
    尝试 {
        locationManager =(LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);

        //获取GPS状态
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        Log.v(TAG,isGPSEnabled =+ isGPSEnabled);

        //获取网络状态
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        Log.v(TAG,isNetworkEnabled =+ isNetworkEnabled);

        this.canGetLocation = TRUE;
        如果(isNetworkEnabled){
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);
            Log.d(TAG,网络);
            如果(locationManager!= NULL){
                位置= locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                如果(位置!= NULL){
                    纬度= location.getLatitude();
                    经度= location.getLongitude();
                }
            }
        }
        //如果GPS功能的获得纬度/经度使用GPS服务
        如果(isGPSEnabled&安培;&安培;位置== NULL){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);
            Log.d(TAG,全球定位系统已启用);
            如果(locationManager!= NULL){
                位置= locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                如果(位置!= NULL){
                    纬度= location.getLatitude();
                    经度= location.getLongitude();
                }
            }
        }
    }赶上(例外五){
        Log.e(TAG,位置未找到);
    }
    返回的位置;
}
 

有关该方法的详细信息 getLastKnownLocation ,请<一href="http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String)"相对=nofollow>请参考文档。

I've been getting current location with GoogleApiClient up until now but I've just noticed that it's much simpler to do it with LocationManager using LocationListener since it can even detect when GPS service was turned on or off by the user.

But I have a problem when getting user's first location after the LocationManager was initialized.

LocationManager has 4 listeners but none of them give you your first location. It does have a onLocationChanged listener but it only activates when you move.

This is how I'm using it:

// Init LocationManager (needed to track if GPS is turned on or not
locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);


end of oncreate......


/*
LocationListener (Listening if GPS service is turned on/off)
 */

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onLocationChanged(Location location) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public void onProviderDisabled(String provider) {
}

解决方案

Use the following method to get the Location object:

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        Log.v(TAG, "isGPSEnabled =" + isGPSEnabled);

        // getting network status
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        Log.v(TAG, "isNetworkEnabled =" + isNetworkEnabled);

        this.canGetLocation = true;
        if (isNetworkEnabled) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
            Log.d(TAG, "Network");
            if (locationManager != null) {
                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                if (location != null) {
                    latitude = location.getLatitude();
                    longitude = location.getLongitude();
                }
            }
        }
        // if GPS Enabled get lat/long using GPS Services
        if (isGPSEnabled && location == null) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
            Log.d(TAG, "GPS Enabled");
            if (locationManager != null) {
                location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                if (location != null) {
                    latitude = location.getLatitude();
                    longitude = location.getLongitude();
                }
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Location Not Found");
    }
    return location;
}

For more information on the method getLastKnownLocation, please refer to the docs.

这篇关于如何获得初始位置。LocationManager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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