Android |将导航抽屉添加到默认的地图活动

时间:2017-08-10 04:46:07

标签: java android google-maps navigation-drawer

我已经好几周都在寻找答案,所以请原谅我是否有点沮丧。这是因为我。

提前,对不起,如果之前已经问过这个问题。我是Android编程的新手(我主要做网络应用程序),这对我来说是一个全新的世界。感谢所有帮助!

我想要做的是将导航抽屉添加到磨坊地图活动的标准运行中。我该怎么做呢?我希望每个按钮都能将地图重新​​定位到另一个地方。

当我们参与其中时,我计划在地图中创建一个KML图层。如您所知,KML点内置了信息,因此当您点击/点击它们时,您可以看到信息。我想让KML点打开导航抽屉及其内置信息。我很好地使用默认的Android点,但是K​​ML的东西是值得赞赏的。如果您想要一个参考框架,请考虑类似这个JavaScript示例,但大多数情况下隐藏侧边栏: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" />

提前感谢您的帮助。美好的一天吧!

2 个答案:

答案 0 :(得分:0)

首先,您需要将Fragment包含在另一个包含Navigation drawer的父布局中,然后在java中为drawer设置内容。 请参阅this

来到交互部分,KML只是一个符号,它包含一些信息,您可以做的是为每个KML地理点创建一个marker并将其绑定到{{的点击函数1}}打开marker

Check here用于将标记添加到Google地图

答案 1 :(得分:0)

如果您希望导航抽屉覆盖地图活动而不是更新 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>

它将创建您的导航抽屉作为rajan提及的一个答案,您需要编写一些代码来处理导航抽屉。