[英]How to add google map on navigation drawer activity?
Problem with this, This code is also not working..有这个问题,这段代码也不起作用..
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"));
}
});
I found a solution today.我今天找到了解决方案。
Implement OnMapReadyCallback
to MainActivity
.实现OnMapReadyCallback
到MainActivity
。
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, OnMapReadyCallback { // ... }
Implement onMapReady()
.实现onMapReady()
。 Keep it empty.让它空着。
@Override public void onMapReady(GoogleMap googleMap) { }
In onCreate()
method:在onCreate()
方法中:
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); supportMapFragment.getMapAsync(this);
You are mixing up two processes.您正在混淆两个过程。 You have to change either in your xml or activity class code:您必须在 xml 或活动类代码中进行更改:
1) Either replace your map fragment in content_main.xml with the MapView (As Below): 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) Or replace this: 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"));
}
});
With:和:
((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"));
}
}
In your MainActivity class.在您的 MainActivity 类中。
In your existing code, You have added a map fragment to the layout but calling for a mapview from activity.在您现有的代码中,您已向布局添加了一个地图片段,但从活动中调用了地图视图。 That's why it is not working.这就是它不起作用的原因。
To get API key, go HERE and Generate a key.要获取 API 密钥,请转到此处并生成密钥。
Copy the key and paste it in manifest under application tag:复制密钥并将其粘贴到应用程序标签下的清单中:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY"/>
Now the google map will show up.现在谷歌地图会出现。
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>`
in MapActivity add implements method navigationView and then add this code below the getSupportedFragment在 MapActivity 添加实现方法 navigationView 然后在 getSupportedFragment 下面添加这段代码
` DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);`
then write the onclick method on Imagebutton method which is ` public void然后在 Imagebutton 方法上编写 onclick 方法,它是 `public void
drawerclick(View view) {
drawer.openDrawer(Gravity.START);
enter code here
navigationView.setNavigationItemSelectedListener(this);
}`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.