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

时间:2016-05-23 07:35:30

标签: android google-maps android-fragments

我是Android开发的新手。我已经使用导航抽屉作为MainActivity,MAP的Maps活动,并使用了我在其他问题中找到的GPSTracker 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

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中

<?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

<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

<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>

2 个答案:

答案 0 :(得分:0)

使用此方法在Google MAP中显示MyLocation: -

mMap.setMyLocationEnabled(真);

如果您不想使用自定义标记。

答案 1 :(得分:0)

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);
    }