如何在位置不变的情况下获取当前位置

时间:2017-08-30 23:14:33

标签: android google-maps gps location

您好我是Android开发的新手。我想得到我现在的位置,但我需要等待我的听众开始工作。问题是我不改变位置,所以听众无法工作(?)!

以下是我的代码示例

我的听众:

 locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {

                textView.append(location.getLongitude() + "\n" + location.getLatitude() + "\n");
                // just monitoring
                lat = location.getLatitude();
                lng = location.getLongitude();

            }

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

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);

            }
        };

我要求更新

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300, 0, locationListener);

所以,如果我没有弄错的话,gps将根据时间(300毫秒)或基于0的距离采取新的位置,以便它能够工作?我应该等一段时间才开始服用吗?多久?超过2分钟?

我打开了wi-fi,gps当然这是我的清单

<?xml version="1.0" encoding="utf-8"?>

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Details" />
    <activity android:name=".LoginMenu" />

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity android:name=".Road" />
    <activity android:name=".LoopActivity" />
    <activity android:name=".Pedestrians"></activity>
</application>

  

非常感谢

1 个答案:

答案 0 :(得分:0)

if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
            }
        }
        LocationManager mLocationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
        if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            mProvider = GPS_PROVIDER;
        } else {
            mProvider = NETWORK_PROVIDER;
                       }
        mLocationManager.requestLocationUpdates(mProvider, REFRESH_TIME, REFRESH_DISTANCE, mLocationListener);
        Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            getAddressFromLocation(mLastLocation, getContext(), new GeocoderHandler());
        }

这就是我在我的应用程序中获得类似结果的方式,该设备可以坐在桌子上,但它会定期刷新位置。下面的方法是我解码数据的地方:

public void getAddressFromLocation(
            final Location location, final Context context, final Handler handler) {
        if (context != null) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
                    String result = null;
                    try {
                        List<Address> list = geocoder.getFromLocation(
                                location.getLatitude(), location.getLongitude(), 1);
                        if (list != null && list.size() > 0) {
                            Address address = list.get(0);
                            // sending back first address line and locality
                            //result = address.toString();
                            result = address.getAddressLine(0) + ", " + address.getLocality();
                            mAddress = address;
                            Log.e(TAG, result);
                        }
                    } catch (IOException e) {
                        Log.e(TAG, "Impossible to connect to Geocoder", e);
                    } finally {
                        Message msg = Message.obtain();
                        msg.setTarget(handler);
                        if (result != null) {
                            msg.what = 1;
                            Bundle bundle = new Bundle();
                            bundle.putString("address", result);
                            msg.setData(bundle);
                            Log.e(LocationFragment.class.getSimpleName(), "getAddressFromLocation");
                        } else
                            msg.what = 0;
                        msg.sendToTarget();
                    }
                }
            };
            thread.start();
        }
    }

使用的全局变量定义如下:

private Address mAddress;
private String mProvider = LocationManager.GPS_PROVIDER;

前段时间我遇到了类似的问题,这可能是我在互联网上收集到的所有知识和代码的高潮。它符合我的目的,我现在很满意,你也可以改进它。