【问题标题】:How to get the updated location with an interval of 1 minute ANDROID如何以 1 分钟的间隔获取更新的位置 ANDROID
【发布时间】:2017-04-07 04:43:35
【问题描述】:

我想以 1 分钟的间隔获取更新的位置,以便我将标记放在地图上,同时让其他应用程序可以在我移动时看到我的当前位置。

这是我的代码,但遗憾的是即使代码已经在 onLocationChanged 方法中,它也不会更新位置。但我不确定我的代码是否正确。

@Override
public void onLocationChanged(Location location) {

    mLastLocation = location;
    if (mCurrLocationMarker != null) {
        mCurrLocationMarker.remove();
    }
    latlng = new LatLng(location.getLatitude(), location.getLongitude());
    lat = String.valueOf(location.getLatitude());
    lLong = String.valueOf(location.getLongitude());

    driverID = pref.getString("driverID", "");

    final Query userName = ref.orderByChild("username").equalTo(username);
    userName.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot snapShot : dataSnapshot.getChildren()) {
                if (!snapShot.child("latitude").equals(lat) || !snapShot.child("longitude").equals(lLong)) {
                    snapShot.getRef().child("latitude").setValue(lat);
                    snapShot.getRef().child("longitude").setValue(lLong);
                    snapShot.getRef().child("currentLocation").setValue(strOrigin);
                    Toast.makeText(MapsActivity.this, "old lat: " + snapShot.child("latitude").getValue().toString() + "\nnew lat: " + lat +
                            "\nold long: " + snapShot.child("longitude").getValue().toString() + "\nnew long: " + lLong, Toast.LENGTH_LONG).show();
                }
            }
            markerOptions = new MarkerOptions();
            markerOptions.position(latlng);
            markerOptions.title("Current Location");
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
            mCurrLocationMarker = mMap.addMarker(markerOptions);

            //move map camera
            mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            Toast.makeText(MapsActivity.this, "on cancelled child latlng: " + firebaseError.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

    Geocoder geocoder;
    List<android.location.Address> addresses;
    geocoder = new Geocoder(this, Locale.getDefault());

    StringBuilder result = new StringBuilder();

    try {
        addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);

        if (addresses.size() > 0) {
            Address address = addresses.get(0);
            result.append(address.getAddressLine(0)).append(", ");
            result.append(address.getLocality()).append(", ");
            result.append(address.getCountryName());
        }

        strOrigin = result.toString();
        Toast.makeText(MapsActivity.this, "origin" + strOrigin, Toast.LENGTH_SHORT).show();
        // onSendOrigin(r);
    } catch (IOException e) {
        e.printStackTrace();
    }
    //stop location updates
    if (mGoogleApiClient != null) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    }\
}

【问题讨论】:

标签: android location marker locationmanager


【解决方案1】:

看起来你在打电话:

        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

在 onLocationChanged 方法中。这将在第一次调用 onLocationChanged 后立即停止位置更新。

尝试删除该行,看看它是否有效。仅当您使用完位置服务,或者您的应用暂停/停止时,您才应该调用 removeLocationUpdates

【讨论】:

    猜你喜欢
    • 2011-11-27
    • 2014-08-07
    • 2021-10-22
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    相关资源
    最近更新 更多