[英]Google Maps V2 Android with API KEY showing blank
我正在嘗試將地圖視圖添加到我的應用程序,但地圖顯示一個空白屏幕,我搜索了這個問題,我添加了我需要的所有權限和API密鑰,但仍然是空白。 這是我的地圖代碼
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:apiKey="-----------------------------------"
android:layout_height="200dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"/>
這是onCreateView中的java代碼(我正在使用片段)
mapview = (MapView) view.findViewById(R.id.map);
mapview.onCreate(savedInstanceState);
mapview.getMapAsync(this);
和
@Override
public void onMapReady(GoogleMap googleMap) {
GoogleMap map = googleMap;
map.getUiSettings().setMyLocationButtonEnabled(false);
if (ActivityCompat.checkSelfPermission(this.getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this.getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
map.setMyLocationEnabled(true);
GeoPoint location = user.getLocation();
LatLng pos;
if(location == null) pos = new LatLng(41.8919300, 12.5113300);
else pos = new LatLng(location.getLatitude(), location.getLongitude());
map.addMarker(new MarkerOptions().position(pos)
.title("Ti trovi qui"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(pos,10f));
mapview.onResume();
}
這是我的表現
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.diy.yourself">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<permission
android:name="com.diy.yourself.permissions.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.diy.yourself.permissions.MAPS_RECEIVE"/>
<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/Theme.AppCompat.DayNight.NoActionBar">
<activity
android:name="com.diy.yourself.SignInActivity"
android:label="Sign In"></activity>
<activity android:name="com.diy.yourself.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.diy.yourself.SignUpActivity" />
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="-----------------------------------------"/>
<uses-library android:name="com.google.android.maps"/>
<activity android:name="com.diy.yourself.ConditionsActivity" />
</application>
</manifest>
Google Maps for Android API已啟用,我使用的密鑰是Firebase自動生成的密鑰,適用於其他API,調試程序不會顯示任何錯誤。
有人可以幫忙嗎? 提前謝謝👍🏻
您必須將此元數據添加到清單中
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="----------------------------"/>
無需將apiKey添加到xml。 所以從MapView
刪除這一行
android:apiKey="-----------------------------------"
並在您的片段中添加此行
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
在XML中使用此代碼
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
這個代碼用於定義
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.