Java Android |将导航抽屉添加到默认地图活动
我已经寻找这个问题的答案好几个星期了,所以如果我看起来有点沮丧,请原谅。因为我是 如果之前有人问过这个问题,请提前向我们道歉。我是Android编程新手(我主要做网络应用),这对我来说是一个全新的世界。感谢您的帮助 我想做的是将导航抽屉添加到标准的常规地图活动中。我该怎么做呢?我希望每个按钮都能够将地图重新居中到另一个地方 当我们在做的时候,我计划在我的地图上创建一个KML图层。正如您所知,KML点具有内置信息,因此当您点击/单击它们时,您可以看到这些信息。我想让KML点打开导航抽屉及其内置信息。我可以使用默认的安卓点数来实现这一点,但是KML的一些东西是值得赞赏的。如果您想要一个参考框架,可以考虑如下JavaScript示例,但侧边栏大部分时间是隐藏的: MainActivity.java:Java Android |将导航抽屉添加到默认地图活动,java,android,google-maps,navigation-drawer,Java,Android,Google Maps,Navigation Drawer,我已经寻找这个问题的答案好几个星期了,所以如果我看起来有点沮丧,请原谅。因为我是 如果之前有人问过这个问题,请提前向我们道歉。我是Android编程新手(我主要做网络应用),这对我来说是一个全新的世界。感谢您的帮助 我想做的是将导航抽屉添加到标准的常规地图活动中。我该怎么做呢?我希望每个按钮都能够将地图重新居中到另一个地方 当我们在做的时候,我计划在我的地图上创建一个KML图层。正如您所知,KML点具有内置信息,因此当您点击/单击它们时,您可以看到这些信息。我想让KML点打开导航抽屉及其内置信息
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)));
}
}
活动\u 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" />
<?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>
提前谢谢你的帮助。你好 第一件事首先,您需要将
片段
包装到另一个父布局中,该布局包括导航抽屉
,然后在java中为抽屉
设置内容。
提及
在交互部分,KML
只是一个符号,它包含一些信息,您可以为每个KML地理点创建一个标记
,并将其绑定到标记
的单击功能以打开导航抽屉
要向Google Maps添加标记,首先需要将
片段
包装到另一个父布局中,该布局包括导航抽屉
,然后在java中为抽屉
设置内容。
提及
在交互部分,KML
只是一个符号,它包含一些信息,您可以为每个KML地理点创建一个标记
,并将其绑定到标记
的单击功能以打开导航抽屉
用于将标记添加到Google地图如果您想在地图活动上导航,而不是更新您的活动\u 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" />
<?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提到的一个答案,您需要编写一些代码来处理导航抽屉。如果您希望导航抽屉覆盖您的地图活动,而不是更新您的活动\u 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" />
<?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提到的一个答案,您需要编写一些代码来处理导航抽屉。您想要什么?您想在MainActivity中添加导航抽屉您想要什么?您想在MainActivity中添加导航抽屉我如何为所有KML点创建单击功能?我对Java/Android的了解非常有限。不过我有地图要用了,你真是太好了。算了吧!我使用OnMarkerClickListener创建了onMarkerClick的布尔值,用于检查单击了哪个标记。然后它打开一个单独的片段。谢谢你的帮助!很高兴我能提供帮助:)我该如何为所有KML点创建单击功能?我对Java/Android的了解非常有限。不过我有地图要用了,你真是太好了。算了吧!我使用OnMarkerClickListener创建了onMarkerClick的布尔值,用于检查单击了哪个标记。然后它打开一个单独的片段。谢谢你的帮助!很高兴我能帮忙:)