简体   繁体   English

安卓 | 将导航抽屉添加到默认地图活动

[英]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.我是 Android 编程的新手(我主要做网络应用程序),这对我来说是一个全新的世界。 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.在此过程中,我计划在我的地图中创建一个 KML 图层。 As you know, KML points have information built into them so that when you tap/click on them you can see the info.如您所知,KML 点具有内置的信息,因此当您点击/单击它们时,您可以看到信息。 I want to have the KML point open the Navigation Drawer with its built-in-information.我想让 KML 点使用其内置信息打开导航抽屉。 I'm fine using the default android points for this, but something with KML is appreciated.我可以为此使用默认的 android 点,但可以使用 KML 的东西。 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如果您想要一个参考框架,请考虑类似以下 JavaScript 示例,但大部分时间都隐藏侧边栏:https ://developers.google.com/maps/documentation/javascript/kml

MainActivity.java: 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 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.首先,您需要将Fragment包装在另一个包含Navigation drawer的父布局中,然后在 java 中为drawer设置东西。 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 .说到交互部分, KML只是一个符号,它包含一些信息,您可以做的是为每个 KML 地理点创建一个marker ,并将其绑定到marker的单击功能以打开Navigation drawer

Check here for adding Markers to Google Maps 查看此处以将标记添加到 Google 地图

If you want navigation drawer over your map activity than update your activity_main.xml如果您希望在地图活动上使用导航抽屉,而不是更新您的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.它将创建您的导航抽屉作为 rajan 提到的一个答案,您需要编写一些代码来处理导航抽屉。

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

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