【发布时间】:2020-06-12 18:57:49
【问题描述】:
我一直在尝试让 Google 地图在 Android Studios 中运行。
使用 Google 地图模板创建新项目时,它可以正常工作。但是,当我在现有项目中实现地图时,它显示的只是一个灰色屏幕,左下角有徽标。
现有项目使用一个片段导航系统,其中包含一个承载所有其他片段类的活动。但这不应该是问题的原因,因为我以与模板中相同的方式实现地图,但它也不起作用。
我检查了 Logcat 的输出,没有出现密钥验证错误。如果我更改密钥,则会出现错误。
这是包含地图的片段类的代码:
public class MapFragment extends Fragment implements OnMapReadyCallback {
private MapViewModel dashboardViewModel;
private GoogleMap mMap;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
dashboardViewModel =
ViewModelProviders.of(this).get(MapViewModel.class);
View root = inflater.inflate(R.layout.fragment_map, container, false);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return root;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng testLocation = new LatLng(50, -2);
mMap.moveCamera(CameraUpdateFactory.newLatLng(testLocation));
}
}
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.backintyne">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
提前致谢
【问题讨论】:
标签: java android android-studio google-maps-android-api-2