Связывание навигационного ящика с картой, показывающей, что текущее местоположение не работает - PullRequest
0 голосов
/ 04 февраля 2019

Это MainActivity

        package com.example.safna.tracker5;

        import android.graphics.Color;
        import android.os.Bundle;
        import android.support.design.widget.FloatingActionButton;
        import android.support.design.widget.Snackbar;
        import android.util.Log;
        import android.view.View;
        import android.support.design.widget.NavigationView;
        import android.support.v4.view.GravityCompat;
        import android.support.v4.widget.DrawerLayout;
        import android.support.v7.app.ActionBarDrawerToggle;
        import android.support.v7.app.AppCompatActivity;
        import android.support.v7.widget.Toolbar;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.widget.TableLayout;
        import android.widget.TableRow;
        import android.widget.TextView;
        import android.widget.Toast;



        import org.json.JSONArray;
        import org.json.JSONException;
        import org.json.JSONObject;

        import java.io.BufferedReader;
        import java.io.InputStream;
        import java.io.InputStreamReader;

        public class MainActivity extends AppCompatActivity
                implements NavigationView.OnNavigationItemSelectedListener {
            public static TextView data;
            @Override
            protected void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                data=(TextView) findViewById(R.id.desc);
                fetchData process=new fetchData();

                process.execute();



            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);


                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
                drawer.addDrawerListener(toggle);
                toggle.syncState();

                NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
                navigationView.setNavigationItemSelectedListener(this);
            }

            @Override
            public void onBackPressed() {
                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                if (drawer.isDrawerOpen(GravityCompat.START)) {
                    drawer.closeDrawer(GravityCompat.START);
                } else {
                    super.onBackPressed();
                }
            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
            }

            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                // Handle action bar item clicks here. The action bar will
                // automatically handle clicks on the Home/Up button, so long
                // as you specify a parent activity in AndroidManifest.xml.
                int id = item.getItemId();

                //noinspection SimplifiableIfStatement
                if (id == R.id.action_settings) {
                    return true;
                }

                return super.onOptionsItemSelected(item);
            }

            @SuppressWarnings("StatementWithEmptyBody")
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                // Handle navigation view item clicks here.
                int id = item.getItemId();

                if (id == R.id.nav_camera) {
                    // Handle the camera action
                } else if (id == R.id.nav_gallery) {

                } else if (id == R.id.nav_slideshow) {

                } else if (id == R.id.nav_manage) {

                } else if (id == R.id.nav_share) {

                } else if (id == R.id.nav_send) {

                }

                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                drawer.closeDrawer(GravityCompat.START);
                return true;
            }
        }

Активность карт

        package com.example.safna.tracker5;

        import android.graphics.Color;
        import android.os.Bundle;
        import android.support.design.widget.FloatingActionButton;
        import android.support.design.widget.Snackbar;
        import android.util.Log;
        import android.view.View;
        import android.support.design.widget.NavigationView;
        import android.support.v4.view.GravityCompat;
        import android.support.v4.widget.DrawerLayout;
        import android.support.v7.app.ActionBarDrawerToggle;
        import android.support.v7.app.AppCompatActivity;
        import android.support.v7.widget.Toolbar;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.widget.TableLayout;
        import android.widget.TableRow;
        import android.widget.TextView;
        import android.widget.Toast;



        import org.json.JSONArray;
        import org.json.JSONException;
        import org.json.JSONObject;

        import java.io.BufferedReader;
        import java.io.InputStream;
        import java.io.InputStreamReader;

        public class MainActivity extends AppCompatActivity
                implements NavigationView.OnNavigationItemSelectedListener {
            public static TextView data;
            @Override
            protected void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                data=(TextView) findViewById(R.id.desc);
                fetchData process=new fetchData();

                process.execute();



            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);


                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
                drawer.addDrawerListener(toggle);
                toggle.syncState();

                NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
                navigationView.setNavigationItemSelectedListener(this);
            }

            @Override
            public void onBackPressed() {
                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                if (drawer.isDrawerOpen(GravityCompat.START)) {
                    drawer.closeDrawer(GravityCompat.START);
                } else {
                    super.onBackPressed();
                }
            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
            }

            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                // Handle action bar item clicks here. The action bar will
                // automatically handle clicks on the Home/Up button, so long
                // as you specify a parent activity in AndroidManifest.xml.
                int id = item.getItemId();

                //noinspection SimplifiableIfStatement
                if (id == R.id.action_settings) {
                    return true;
                }

                return super.onOptionsItemSelected(item);
            }

            @SuppressWarnings("StatementWithEmptyBody")
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                // Handle navigation view item clicks here.
                int id = item.getItemId();

                if (id == R.id.nav_camera) {
                    // Handle the camera action
                } else if (id == R.id.nav_gallery) {

                } else if (id == R.id.nav_slideshow) {

                } else if (id == R.id.nav_manage) {

                } else if (id == R.id.nav_share) {

                } else if (id == R.id.nav_send) {

                }

                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                drawer.closeDrawer(GravityCompat.START);
                return true;
            }
        }

это мой файл активности;

        <?xml version="1.0" encoding="utf-8"?>
        <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:context=".MainActivity"
            tools:showIn="@layout/app_bar_main">

            <TextView
                android:id="@+id/textview1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:layout_editor_absoluteX="19dp"
                tools:layout_editor_absoluteY="165dp">

                <Switch
                    android:id="@+id/switch2"
                    android:layout_width="191dp"
                    android:layout_height="42dp"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginStart="102dp"
                    android:layout_marginTop="469dp"
                    android:showText="true"
                    android:splitTrack="false"
                    android:switchMinWidth="@android:dimen/notification_large_icon_height"
                    android:text="Mode"
                    android:textOff="Offline"
                    android:textOn="Online"
                    tools:checked="false" />
                <include
                    layout="@layout/activity_maps"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

            </RelativeLayout>
            <android.support.v4.widget.NestedScrollView
                android:id="@+id/bottom_sheet"
                android:layout_width="match_parent"
                android:layout_height="350dp"
                android:background="#FFFAFA"
                app:layout_behavior="@string/bottom_sheet_behavior"
                app:behavior_hideable="false"
                app:behavior_peekHeight="55dp">


                <TableLayout android:id="@+id/table"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:focusableInTouchMode="true"
                    android:focusable="true">
                <TextView android:id="@+id/req1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="View Requests"
                    android:padding="16dp"
                    android:textSize="20sp"
                    android:textColor="#2E8B57"
                    android:textStyle="bold"
                    />
                <TextView
                    android:id="@+id/desc"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:padding="16dp"
                    android:textSize="16sp"
                    android:text="Service Request \n \n"/>

                </TableLayout>
            </android.support.v4.widget.NestedScrollView>


        </android.support.design.widget.CoordinatorLayout>

Текущее местоположение работает в отдельном приложении,Но это не работает, если интегрировать его с блоком навигации.

Я добавил нижний лист в contentmain.xml , который показывает некоторые данные и карту Google.Но это не показывало текущее местоположение.без ошибок

Ответы [ 2 ]

0 голосов
/ 05 февраля 2019
        i have add a function called loadMap which show the current location and call 
        this function in the MainActivity file instead of MapsActivity
            private void loadMap() {
                     SupportMapFragment mapFragment = (SupportMapFragment) 
                      getSupportFragmentManager()
                            .findFragmentById(R.id.map);

                    mapFragment.getMapAsync(this);
                    ActivityCompat.requestPermissions(this, new String[] 


                    {Manifest.permission.ACCESS_FINE_LOCATION},
                    PackageManager.PERMISSION_GRANTED);
                    ActivityCompat.requestPermissions(this, new String[] 
                   {Manifest.permission.ACCESS_COARSE_LOCATION}
                   ,PackageManager.PERMISSION_GRANTED);

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

                     Log.e("onLocationChanged",
                    "onLocationChanged===========================");
                            try {
                                latLng = new LatLng(location.getLatitude(), 
                    location.getLongitude());
                                mMap.addMarker(new 
                   MarkerOptions().position(latLng).title("My 
                    Location"));

                  mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                            }
                            catch (SecurityException e)
                            {
                                e.printStackTrace();
                            }
                        }

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

                        }

                        @Override
                        public void onProviderEnabled(String provider) {

                        }

                        @Override
                        public void onProviderDisabled(String provider) {

                        }
                    };
                    locationManager= 
                       (LocationManager)getSystemService(LOCATION_SERVICE);
                    try
                    {

                     locationManager.requestLocationUpdates


                    (LocationManager.GPS_PROVIDER,
                     MIN_TIME,MIN_DIST,locationListener);
                    }
                    catch (SecurityException e)
                    {
                        e.printStackTrace();
                    }
                  }      
0 голосов
/ 04 февраля 2019

Похоже, проблема в вашем макете.Вы можете использовать LinearLayout, чтобы справиться с этой проблемой.

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                tools:layout_editor_absoluteX="19dp"
                tools:layout_editor_absoluteY="165dp">

                <Switch
                    android:id="@+id/switch2"
                    android:layout_width="191dp"
                    android:layout_height="42dp"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginStart="102dp"
                    android:layout_marginTop="469dp"
                    android:showText="true"
                    android:splitTrack="false"
                    android:switchMinWidth="@android:dimen/notification_large_icon_height"
                    android:text="Mode"
                    android:textOff="Offline"
                    android:textOn="Online"
                    tools:checked="false" />
                <include
                    layout="@layout/activity_maps"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

            </RelativeLayout>
            <android.support.v4.widget.NestedScrollView
                android:id="@+id/bottom_sheet"
                android:layout_width="match_parent"
                android:layout_height="350dp"
                android:background="#FFFAFA"
                app:layout_behavior="@string/bottom_sheet_behavior"
                app:behavior_hideable="false"
                app:behavior_peekHeight="55dp">


                <TableLayout android:id="@+id/table"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:focusableInTouchMode="true"
                    android:focusable="true">
                <TextView android:id="@+id/req1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="View Requests"
                    android:padding="16dp"
                    android:textSize="20sp"
                    android:textColor="#2E8B57"
                    android:textStyle="bold"
                    />
                <TextView
                    android:id="@+id/desc"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:padding="16dp"
                    android:textSize="16sp"
                    android:text="Service Request \n \n"/>

                </TableLayout>
            </android.support.v4.widget.NestedScrollView>
</LinearLayout>

Важная часть: свойство

<RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"

weight="1" будет заполнять страницу после прорисовки других представлений.Этот должен работать для вас.

...