简体   繁体   中英

Android | Add Navigation Drawer to default Maps Activity

I've been searching for the answer to this for weeks now, so please excuse if I seem a little frustrated. It's because I am.

In advance, sorry if this question has been asked before. I'm new to Android programming (I mostly do web-apps) and this is a whole new world to me. All help is appreciated!

What I want to do is add a Navigation Drawer to a standard run of the mill maps activity. How would I go about doing this? I want each button to be able to recenter the map to another place as well.

While we're at it, I plan on creating a KML layer in my map. As you know, KML points have information built into them so that when you tap/click on them you can see the info. I want to have the KML point open the Navigation Drawer with its built-in-information. I'm fine using the default android points for this, but something with KML is appreciated. If you want a frame of reference, think something like this JavaScript example but with the sidebar hidden most of the time:https://developers.google.com/maps/documentation/javascript/kml

MainActivity.java:

package us.tourismproject.jared.tourismprojectandroid;

import android.content.res.Resources;
import android.nfc.Tag;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MapStyleOptions;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

private static final String TAG = MainActivity.class.getSimpleName();

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    try {
        // Customise the styling of the base map using a JSON object defined
        // in a raw resource file.
        boolean success = googleMap.setMapStyle(
                MapStyleOptions.loadRawResourceStyle(
                        this, R.raw.style_json));

        if (!success) {
            Log.e(TAG, "Style parsing failed.");
        }
    } catch (Resources.NotFoundException e) {
        Log.e(TAG, "Can't find style. Error: ", e);
    }
    // Position the map's camera near Sydney, Australia.
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(-34, 151)));
}
}

activity_main.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="us.tourismproject.jared.tourismprojectandroid.MainActivity" />

Thanks for any help in advance. Good day now!

First things first, you need to wrap your Fragment inside another parent layout which includes Navigation drawer and then set up things for drawer in java. Refer this

Coming to interaction part, KML is just a notation, which holds some information, what you can do is create a marker for each KML geo points and tie it to a click function of marker to open Navigation drawer .

Check here for adding Markers to Google Maps

If you want navigation drawer over your map activity than update your activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:fitsSystemWindows="true" tools:openDrawer="start">

        <fragment 
          android:id="@+id/map"
           android:name="com.google.android.gms.maps.SupportMapFragment"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
  tools:context="us.tourismproject.jared.tourismprojectandroid.MainActivity" />

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

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

                <!-- Draw your view whatever you want to draw -->

            </LinearLayout>


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

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

It will create your navigation drawer as one answer mention by rajan, you need to write some code for handling navigation drawer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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