简体   繁体   English

使用导航抽屉映射不显示当前位置

[英]Map not showing current location with Navigation Drawer

I'm new to Android Development. 我是Android开发的新手。 I've used Navigation drawer as MainActivity, Maps Activity for MAP and have used GPSTracker java file which I found in other question. 我已经使用导航抽屉作为MainActivity,MAP的Maps活动,并使用了我在其他问题中找到的GPSTracker java文件。

When I run app with Maps activity, it shows user current location but now with Navigation drawer, it run without any error but do not show current location. 当我使用地图活动运行应用时,它会显示用户当前位置,但现在使用导航抽屉,它运行时没有任何错误,但不显示当前位置。

Below is my code: 以下是我的代码:

MainActivity.java MainActivity.java

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
Button btnShowLocation;

// GPSTracker class
GPSTracker gps;
private GoogleMap mMap;
double latitude;
double longitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);


}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    gps = new GPSTracker(MapsActivity.this);

    // check if GPS enabled
    if (gps.canGetLocation()){

        latitude = gps.getLatitude();
        longitude = gps.getLongitude();



        // \n is for new line
        // Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
    }else{
        // can't get location
        // GPS or Network is not enabled
        // Ask user to enable GPS/network in settings
        gps.showSettingsAlert();
    }
    // Add a marker in Sydney and move the camera
    Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();

    LatLng sydney = new LatLng(latitude, longitude);
    mMap.addMarker(new MarkerOptions().position(sydney).title("You are here").icon(BitmapDescriptorFactory
            .fromResource(R.drawable.defaultico)));

    //mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    float zoomLevel = (float) 14.0; //This goes up to 21
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, zoomLevel));
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
}

MapsActivity.java MapsActivity.java

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
Button btnShowLocation;

// GPSTracker class
GPSTracker gps;
private GoogleMap mMap;
double latitude;
double longitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);


}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    gps = new GPSTracker(MapsActivity.this);

    // check if GPS enabled
    if (gps.canGetLocation()){

        latitude = gps.getLatitude();
        longitude = gps.getLongitude();



        // \n is for new line
        // Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
    }else{
        // can't get location
        // GPS or Network is not enabled
        // Ask user to enable GPS/network in settings
        gps.showSettingsAlert();
    }
    // Add a marker in Sydney and move the camera
    Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();

    LatLng sydney = new LatLng(latitude, longitude);
    mMap.addMarker(new MarkerOptions().position(sydney).title("You are here").icon(BitmapDescriptorFactory
            .fromResource(R.drawable.defaultico)));

    //mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    float zoomLevel = (float) 14.0; //This goes up to 21
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, zoomLevel));
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);


}


}

Activity_main.xml activity_main.xml中

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

<include layout="@layout/app_bar_main" android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView android:id="@+id/nav_view"
    android:layout_width="wrap_content" android:layout_height="match_parent"
    android:layout_gravity="start" android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" />

activity_maps.xml activity_maps.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/btnShowLocation"
    android:layout_gravity="center_horizontal" />



<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:id="@+id/map" tools:context=".MapsActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment"

    />

activity_navigation.xml activity_navigation.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include layout="@layout/activity_maps"/>
<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"

    app:headerLayout="@layout/drawer_header"
    app:menu="@menu/drawer"/>


</android.support.v4.widget.DrawerLayout>

Use this method to show MyLocation in Google MAP:- 使用此方法在Google MAP中显示MyLocation: -

mMap.setMyLocationEnabled(true); mMap.setMyLocationEnabled(真);

if you do not want to use custom marker. 如果您不想使用自定义标记。

Try This : Just call this method and import what needs to be imported.

public void setMyLocation(){
    LocationListener locationListener = new LocationListener() {
                public void onLocationChanged(Location location) {
                    // Called when a new location is found by the network location provider.
                    Geocoder gCoder = new Geocoder(this);
                    ArrayList<Address> addresses = null;
                    try {
                        addresses = (ArrayList<Address>) gCoder.getFromLocation(location.getLatitude(),location.getLongitude(), 1);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (addresses != null && addresses.size() > 0) {
                        Toast.makeText(this, "locaion : " + addresses.get(0).getAddressLine(0), Toast.LENGTH_LONG).show();


                    }
                }

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

                public void onProviderEnabled(String provider) {
                }

                public void onProviderDisabled(String provider) {
                }
            };

    // Register the listener with the Location Manager to receive location updates
            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return null;
            }
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM