Java 如何在导航抽屉活动中添加谷歌地图?
问题是,此代码也不起作用Java 如何在导航抽屉活动中添加谷歌地图?,java,android,navigation,maps,Java,Android,Navigation,Maps,问题是,此代码也不起作用 public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener mapView = (MapView)findViewById(R.id.map); mapView.onCreate(savedInstanceState); mapView.getMapAsync(new O
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"));
}
});
你把两个过程搞混了。您必须更改xml或活动类代码: 1) 用MapView替换content_main.xml中的地图片段(如下所示): 与: 在你的活动课上 在现有代码中,您已经向布局中添加了一个地图片段,但从活动中调用了一个地图视图。这就是它不起作用的原因 要获取API密钥,请转到并生成一个密钥 复制密钥并将其粘贴到应用程序标记下的清单中:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY"/>
现在谷歌地图将显示。我今天找到了一个解决方案
OnMapReadyCallback
至main活动
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener,
OnMapReadyCallback
{
// ...
}
。让它空着
@Override
public void onMapReady(GoogleMap googleMap)
{
}
onCreate()
方法中:
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
supportMapFragment.getMapAsync(this);
drawerclick(View view) {
drawer.openDrawer(Gravity.START);
enter code here
navigationView.setNavigationItemSelectedListener(this);
}`
显示完整的活动类和布局xml文件。这是我的MainActivity文件这是我的MainActivity XML文件内容主XML文件我添加了一个答案,你们可以看一下。这段代码可以工作,但启动应用程序时,它只是在左下角显示google徽标,没有地图显示。我该怎么办?@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开发者控制台()中确保启用了“Google Maps Android API v2”。确保以下Android密钥存在:API密钥:AIZASYASUYI6DNMIE6IDMXWP9ACLNMG12R_6v4您的API密钥:AIzaSyAsuyiU6dNMiE6IDmXWP9aClnMG12R_6v4是否与console的API密钥匹配?如果是,请卸载该应用程序,清理生成并重新运行该应用程序。如果这也不起作用,请生成一个新的API密钥并重试。
@Override
public void onMapReady(GoogleMap googleMap)
{
}
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>`
` DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);`
drawerclick(View view) {
drawer.openDrawer(Gravity.START);
enter code here
navigationView.setNavigationItemSelectedListener(this);
}`