Android GPS 跟踪问题

标签 android gps

嗨,我正在尝试制作一个用于 GPS 检测的应用程序。 它工作正常。但经过一段时间后,什么也没有发生,或者出现 ANR 错误。 如果我增加 LocationRequest.setInterval 值(例如 1 分钟),那么这个问题就可以解决,但由于这个原因,我无法准确地绘制折线作为获取两个位置的距离增加请帮助我。

 public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener, LocationListener {
        private Button btnShowLocation;
        private String TAG = "app";
        private GoogleMap mGoogleMap;
        private String mLastUpdateTime;
        private TextView tvText;
        private Marker mapMarker;
        private List<Location> loc = new ArrayList<>();
        float distance, changeDistance;
        private Location mCurrentLocation;
        private List<Marker> marker;

        double latitude;
        double longitude;
        private static final long INTERVAL = 3000; //3 sec
        private static final long FASTEST_INTERVAL = 2500;
        private GoogleApiClient mGoogleApiClient;
        private LocationRequest mLocationRequest;


        protected void createLocationRequest() {
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(INTERVAL);
            mLocationRequest.setSmallestDisplacement(5);
      mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
           mLocationRequest.setFastestInterval(FASTEST_INTERVAL);

        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btnShowLocation = (Button) findViewById(R.id.textview1);

            marker = new ArrayList<>();
            try {

                initilizeMap();

            } catch (Exception e) {
                e.printStackTrace();
            }
            tvText = (TextView) findViewById(R.id.text1);
            if (!isGooglePlayServicesAvailable()) {
                finish();
            }
            createLocationRequest();
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();

            updateUI();



        }

        private void initilizeMap() {
            mGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();
           // mGoogleMap.clear();
            mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            addMarker(mGoogleMap.getMyLocation());
            if (mGoogleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }

        private void addMarker(Location loc) {
            MarkerOptions options = new MarkerOptions();
            options.icon(BitmapDescriptorFactory.defaultMarker());
            LatLng currentLatLng = new LatLng(loc.getLatitude(), loc.getLongitude());
            options.position(currentLatLng);
            mapMarker = mGoogleMap.addMarker(options);
            mapMarker.setDraggable(true);
            mapMarker.showInfoWindow();

            marker.add(mapMarker);
            Log.d("size marker", marker.size() + " ");
            long atTime = mCurrentLocation.getTime();
            mLastUpdateTime = DateFormat.getTimeInstance().format(new Date(atTime));
            mapMarker.setTitle(mLastUpdateTime + " distance " + distance + "m");

            mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng,
                    15));


        }


        @Override
        public void onStart() {
            super.onStart();
            Log.d(TAG, "onStart fired ");
            mGoogleApiClient.connect();

        }

        @Override
        public void onStop() {
            super.onStop();
            Log.d(TAG, "onStop fired ");
            mGoogleApiClient.disconnect();
            Log.d(TAG, "isConnected : " + mGoogleApiClient.isConnected());
        }

        private boolean isGooglePlayServicesAvailable() {
            int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
            if (ConnectionResult.SUCCESS == status) {
                return true;
            } else {
                GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
                return false;
            }
        }

        @Override
        public void onConnected(Bundle bundle) {
            Log.d(TAG, "onConnected - isConnected: " + mGoogleApiClient.isConnected());
            startLocationUpdates();

        }

        protected void startLocationUpdates() {
            PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
                    mGoogleApiClient, mLocationRequest, this);

        }

        @Override
        public void onConnectionSuspended(int i) {

        }

        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
            Log.d(TAG, "Connection failed: " + connectionResult.toString());
        }

        @Override
        public void onLocationChanged(Location location) {
            Log.d(TAG, "Firing onLocationChanged");

            mCurrentLocation = location;
            if (loc.size() > 0)
                if (mCurrentLocation.getTime() <= loc.get(loc.size() - 1).getTime()) return;

            loc.add(mCurrentLocation);
            updateUI();


            addMarker(loc.get(0));

            Log.d(TAG, loc.size() + " " + marker.size());

            Log.d("distance", changeDistance + " ");
            for (int i = 0; i < loc.size(); i++) {

                if (mCurrentLocation != loc.get(i)) {
                    if (loc.size() == 1 || loc.size() == 0) {
                        distance = getDistance(loc.get(0).getLatitude(), loc.get(0).getLongitude(), mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
                    } else {
                        mGoogleMap.addPolyline(new PolylineOptions().geodesic(true)
                                .add(new LatLng(loc.get(i).getLatitude(), loc.get(i).getLongitude()), new LatLng(loc.get(i + 1).getLatitude(), loc.get(i + 1).getLongitude()))
                                .width(3)
                                .color(Color.BLUE));
                        Log.d("distance1", distance + " i " + i);
                        distance = getDistance(loc.get(i).getLatitude(), loc.get(i).getLongitude(), loc.get(i + 1).getLatitude(), loc.get(i + 1).getLongitude());
                        if (marker.size() > 2)
                            remove(i);
                    }

                }
            }
            addMarker(loc.get(loc.size() - 1));
            Log.d("distance new", distance + " i ");
            changeDistance = changeDistance + distance;
            Log.d("changeDistance ", changeDistance + "");

            mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
            updateUI();

        }

        private void remove(int i) {
            mapMarker = marker.get(i);
            mapMarker.remove();
            marker.remove(mapMarker);
        }

        private void updateUI() {
            Log.d(TAG, "UI update initiated");
            if (null != mCurrentLocation) {
                float distnceInKm = changeDistance / 1000;
                Log.d("distance km", distnceInKm + "");
                String lat = String.valueOf(mCurrentLocation.getLatitude());
                String lng = String.valueOf(mCurrentLocation.getLongitude());
                latitude = mCurrentLocation.getLatitude();
                longitude = mCurrentLocation.getLongitude();
                tvText.setText("At Time: " + mLastUpdateTime + "\n" +
                        "Latitude: " + lat + "\n" +
                        "Longitude: " + lng + "\n" +
                        "Accuracy: " + mCurrentLocation.getAccuracy() + "\n" +
                        "Provider: " + mCurrentLocation.getProvider() + "\n" +
                        "Distance " + distnceInKm + " km");

            } else {
                tvText.setText("location is null ");
                Log.d(TAG, "location is null ");

            }
        }

        public float getDistance(double lat1, double lon1, double lat2, double lon2) {
            android.location.Location homeLocation = new android.location.Location("");
            homeLocation.setLatitude(lat1);
            homeLocation.setLongitude(lon1);

            android.location.Location targetLocation = new android.location.Location("");
            targetLocation.setLatitude(lat2);
            targetLocation.setLongitude(lon2);

            return targetLocation.distanceTo(homeLocation);
        }

    }

最佳答案

在这里你可以调用你的线程asynctask-

new AsyncCaller().execute();

现在在 doInBackground(Void... params) 方法中编写 GPS 操作代码

private class AsyncCaller extends AsyncTask<Void, Void, Void>
{
    ProgressDialog pdLoading = new ProgressDialog(AsyncExample.this);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        //this method will be running on UI thread
        pdLoading.setMessage("\tLoading...");
        pdLoading.show();
    }

    @Override
    protected Void doInBackground(Void... params) {

        //this method will be running on background thread so don't update UI frome here
        //do your long running http tasks here like gps operations of yours,you dont want to pass argument and u can access the parent class' variable url over here


        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        //this method will be running on UI thread

        pdLoading.dismiss();
    }

    }
}

关于Android GPS 跟踪问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33795212/

相关文章:

android - 如何在 flutter 中检查此应用程序是否存在 SharedPreferences

gps - GPS坐标转换

ios - 快速返回 GPS 位置的实用程序类?

ios - 在手机上跟踪室内锻炼时保持 iOS 应用程序运行

android - GpsSatellite.getSnr() - 取值范围是多少?

java - 如何在按钮可见时剪切部分 TextView

android - 应用级别的gradle文件中flutterversionNameflutterVersionCode的flutter问题

android - EditText的光标位置

java.util.zip.ZipException : duplicate entry: AbstractHttpContent. 类

java - 在 GPS 和网络之间切换?