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

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

问题描述:

问题与此,此代码也没有工作..如何在导航抽屉活动中添加Google地图?

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener 


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")); 
     } 
    }); 
+0

显示完整的活动类和布局xml文件。 – tahsinRupam

+0

https://justpaste.it/149vo这里是我的MainActivity文件 –

+0

这里是我的MainActivity XML文件https://justpaste.it/149vr –

你两个进程混合起来。你必须改变XML中的或活动类代码:

1)无论是在与MapView的content_main.xml更换地图片段(如下):

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

现在的谷歌地图会显示出来。

+0

此代码有效,但启动应用程序时,只显示左下角的谷歌徽标,没有地图显示。我该怎么办? –

+0

@Chandan库马尔,地图不显示,因为你还没有添加API密钥。我已经添加了程序及其如何API密钥添加到我的回答,请检查。 – tahsinRupam

+0

我加入API密钥,但仍地图没有显示,如果你正在 –

我今天找到了解决方案。

  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); 
    

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添加器具方法navigationView然后添加以下getSupportedFragment

` DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
      NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);` 

此代码然后写入oncli在Imagebutton方法上的ck方法是`public void

drawerclick(View view) { 


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

      navigationView.setNavigationItemSelectedListener(this); 

     }`