2017-03-08 76 views
-2

問題與此,此代碼也沒有工作..如何在導航抽屜活動中添加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 –

回答

0

你兩個進程混合起來。你必須改變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密鑰,但仍地圖沒有顯示,如果你正在 –

0

我今天找到了解決方案。

  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); 
    
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添加器具方法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); 

     }`