根据用户当前位置删除旧折线

时间:2019-07-09 12:49:44

标签: java android google-maps maps directions

每当我尝试更新用户当前位置时,都需要删除一条旧的折线,但是我可以根据用户当前位置移动标记

我已尝试从用户先前位置计算出的每200米触发我的应用程序发出的新HTTP请求。我没有得到任何回应,该应用程序冻结了。没有新的http请求,我需要更新当前的折线

我的地图活动文件所有回调将从此处触发。我使用了MVVM设计模式

respo的所有响应将触发此功能

            private void initViewModel() {
                viewModel = ViewModelProviders.of(this).get(MapsViewModel.class);
                viewModel.init();
                viewModel.askLocationUpdates(this);
                viewModel.getApplicationHelper().observe(this, this);
                viewModel.getCurrentLocation().observe(this, new Observer<Location>() {
                    @Override
                    public void onChanged(Location location) {

                        if (!isDirectionsMode) {


                            updateUserCurrentLocation(mMap, location);
                        } else {

                            mMap.clear();

        if(PolyUtil.isLocationOnPath(new LatLng(location.getLatitude(),location.getLongitude()),polylines.get(0).getPoints(),true,10));
                            {

                                Log.d(TAG, "has path ");
                            }


                        }

                    }
                });


                viewModel.checkServices();
                viewModel.getDirectionsResults().observe(this, new Observer<PolylineOptions>() {
                    @Override
                    public void onChanged(PolylineOptions polylineOptions) {
                        Log.d(TAG, "drawed");
                        oldpolylineoptions = polylineOptions;
                        drawPathDirections(polylineOptions);
                    }
                });


            }

            private void drawPathDirections(PolylineOptions latLngList) {
                if (isDirectionsMode) {
                    for (Polyline line: polylines) {
                        line.remove();
                    }
                    polylines.clear();
                }

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                               int[] grantResults)


                        return;
                    }
                }
                mMap.setMyLocationEnabled(false);
                int height = 100;
                int width = 100;
                BitmapDrawable bitmapdrawOrigin = (BitmapDrawable) getResources().getDrawable(R.drawable.mylocation);
                Bitmap bitmapdrawOriginBitmap = bitmapdrawOrigin.getBitmap();
                Bitmap smallMarkerOrigin = Bitmap.createScaledBitmap(bitmapdrawOriginBitmap, width, height, false);


                BitmapDrawable bitmapdrawDest = (BitmapDrawable) getResources().getDrawable(R.drawable.destination);
                Bitmap bitmapdrawDestBitmap = bitmapdrawDest.getBitmap();
                Bitmap smallMarkerDest = Bitmap.createScaledBitmap(bitmapdrawDestBitmap, width, height, false);

                mMap.addMarker(new MarkerOptions().position(new LatLng(originPlace.getLatitude(), originPlace.getLongitude())).icon(BitmapDescriptorFactory.fromBitmap(smallMarkerDest)).snippet("Your location"));
                mMap.addMarker(new MarkerOptions().position(new LatLng(DestinationPlace.getLatitude(), DestinationPlace.getLongitude())).icon(BitmapDescriptorFactory.fromBitmap(smallMarkerOrigin)).snippet("Destination place"));

                polylines.add(mMap.addPolyline(latLngList));


            }

每当用户设置目的地时,此方法都会从活动中触发,我将要求从视图模型中映射活动repo。在respo中,我所有的HTTP请求都将存在

            @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (requestCode == SEARCH_VIEW_RESULT) {
                    if (resultCode == RESULT_OK) {
                        if (isDirectionsMode) {
                            for (Polyline line : polylines) {
                                line.remove();
                            }

                        }


                        mMap.clear();

                        isDirectionsMode = true;
                        Place place = Autocomplete.getPlaceFromIntent(data);
                        final Location temp = new Location(LocationManager.GPS_PROVIDER);
                        temp.setLatitude(place.getLatLng().latitude);
                        temp.setLongitude(place.getLatLng().longitude);
                        DestinationPlace = temp;
                        previousLocation = originPlace;
                        viewModel.askDirectionFromCurrentLocation(temp);


                    } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
                        // TODO: Handle the error.
                        Status status = Autocomplete.getStatusFromIntent(data);
                        Log.i(TAG, status.getStatusMessage());
                    } else if (resultCode == RESULT_CANCELED) {
                        // The user canceled the operation.
                    }
                }


            }

该方法将在应用程序根据更新地图的值成功找到用户当前位置时触发


            private void updateUserCurrentLocation(GoogleMap mMap, Location location) {
                if (mapReady) {
                    mMap.clear();


                    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), DEFAULT_ZOOM));


             originPlace = location;enter code here
                   mMap.setMyLocationEnabled(true);

                }
            }

0 个答案:

没有答案