【问题标题】:How to add google map on navigation drawer activity?如何在导航抽屉活动中添加谷歌地图?
【发布时间】:2017-03-08 17:20:54
【问题描述】:

这个问题,这个代码也不起作用..

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

【问题讨论】:

标签: java android navigation maps


【解决方案1】:

我今天找到了解决方案。

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

【讨论】:

    【解决方案2】:

    您正在混淆两个进程。您必须更改您的 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 类中。

    在您现有的代码中,您已将地图片段添加到布局中,但需要从活动中调用地图视图。这就是它不起作用的原因。

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

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

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

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

    【讨论】:

    • 此代码有效,但在启动应用程序时,它只是在左下角显示谷歌徽标,没有地图显示。我该怎么办?
    • @Chandan Kumar,地图未显示,因为您尚未添加 api 密钥。我已经添加了如何在我的答案中添加 api 密钥的过程,请检查。
    • 如果您收到“Google Maps Android API:授权失败”,请检查您的错误日志。
    • 错误日志 - 授权失败。请参阅 03-09 12:10:59.317 3165-3245/com.finalproject.mapapp E/Google Maps Android API:在 Google Developer Console (console.developers.google.com) 中确保启用“Google Maps Android API v2”。确保存在以下 Android 密钥:API 密钥:AIzaSyAsuyiU6dNMiE6IDmXWP9aClnMG12R_6v4
    • 您的 API Key: AIzaSyAsuyiU6dNMiE6IDmXWP9aClnMG12R_6v4 是否与控制台的 API 密钥匹配?如果是,请卸载该应用程序,清理构建并重新运行该应用程序。如果这也不起作用,请生成新的 API 密钥并重试。
    【解决方案3】:
    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);`
    

    然后在Imagebutton方法上写onclick方法`public void

    drawerclick(View view) {
    
    
                drawer.openDrawer(Gravity.START);
        enter code here
    
                navigationView.setNavigationItemSelectedListener(this);
    
            }`
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 2021-07-05
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多