如何在导航抽屉活动中添加谷歌地图?

时间:2017-03-08 17:20:54

标签: java android navigation maps

问题,此代码也无效..

{{component-two someData=someData }}

3 个答案:

答案 0 :(得分:2)

我今天找到了解决方案。

  1. 实施OnMapReadyCallbackMainActivity

    public class MainActivity extends AppCompatActivity implements  
         NavigationView.OnNavigationItemSelectedListener, 
         OnMapReadyCallback
    {
        // ...
    }
    
  2. 实施onMapReady()。保持空白。

    @Override
    public void onMapReady(GoogleMap googleMap)
    {
    }
    
  3. onCreate()方法中:

    SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    supportMapFragment.getMapAsync(this);
    

答案 1 :(得分:1)

你正在混淆两个过程。您必须更改xml或活动类代码:

1)将content_main.xml中的地图片段替换为MapView(如下所示):

 <com.google.android.gms.maps.MapView
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

2)或者替换它:

mapView = (MapView)findViewById(R.id.map);

mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(GoogleMap googleMap) {
        googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(37.7750, 122.4183))
                .title("San Francisco")
                .snippet("Population: 776733"));
    }
});

使用:

 ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map)).getMapAsync(new OnMapReadyCallback() {

  @Override
  public void onMapReady(GoogleMap googleMap) {

       googleMap.addMarker(new MarkerOptions()
           .position(new LatLng(37.7750, 122.4183))
           .title("San Francisco")
           .snippet("Population: 776733"));
   }

}

在您的MainActivity类中。

在现有代码中,您已向布局添加了一个地图片段,但是要从活动中调用mapview。这就是它无法正常工作的原因。

要获取API密钥,请转到HERE生成密钥。

复制密钥并将其粘贴到应用程序标记下的清单中:

<meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="YOUR_API_KEY"/>

现在谷歌地图将会显示。

答案 2 :(得分:0)

first you have to create the relative_layout because we can't add anything in map fragment so we add image button and map fragment in relative_layout.




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

        <include
            layout="@layout/activity_maps"
            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_drawer" />

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

here your drawer_layout

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

        <include
            layout="@layout/activity_maps"
            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_drawer" />

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

在MapActivity中添加implements方法navigationView,然后在getSupportedFragment

下添加此代码
` DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
           NavigationView  navigationView = (NavigationView) findViewById(R.id.nav_view);`

然后在Imagebutton方法上编写onclick方法,即public void

drawerclick(View view) {


            drawer.openDrawer(Gravity.START);
    enter code here

            navigationView.setNavigationItemSelectedListener(this);

        }`