[英]android google map issues
我目前正在構建一個android應用。 我了解基本知識,並且做了很多應用程序,但是現在我的項目更大了。 我的應用程序包括許多帶有NavigationDrawer的屏幕,這些屏幕迫使我在要顯示在應用程序上的每個類上使用Fragment類的擴展。 這是我的問題-不久前,我創建了一個簡單的一頁Google Maps屏幕的應用程序,它工作得很好。 我想在當前正在使用的應用程序上做同樣的事情,但是當我轉到頁面時,它給我的就是左下角屏幕上的google日志,其余為灰色,如視圖(未顯示地圖)。在包括本網站在內的很多地方搜索了解決方案,我得到的答案是,這可能與google提供給我的API密鑰有關。我對此深信不疑,我敢肯定它不是來自密鑰,因為我將其應用到了我的一頁Google地圖應用程序上,並且運行完美。我還檢查了6次,獲得了所有所需的權限。因此,我的結論是代碼中有些內容阻止了地圖的顯示。
這是我的代碼:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class guide extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.test, container,
false);
return v;
}
class HoldFragmentPlace extends FragmentActivity{
private GoogleMap mMap;
public HoldFragmentPlace(){
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.test, container,
false);
return v;
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
}
我需要該Fragment擴展才能使NavigationDrawer正常工作,所以我創建了一個內部類並在其中應用了地圖的代碼。
這是清單中的內容:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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" />
這是我的地圖XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_marginTop="-470dp" />
</LinearLayout>
我還嘗試了Java代碼的其他一些變體,其中一些不包含內部類,結果仍然相同。我遺漏了一些非常基礎的東西,或者我誤解了有關Android的一些概念。您的空閑時間!
我懷疑缺少某些部分:
(1)聲明使用MAPS_RECEIVE
權限:
<permission android:name="com.example.yourpackage.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<!-- after declare use this -->
<uses-permission android:name="com.example.yourpackage.permission.MAPS_RECEIVE"/>
(2)缺少聲明的gms版本變量:
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
(3)API_KEY的生成可能存在一些問題:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="your_key" />
請首先與上述3位嫌疑犯進行檢查:如果仍然存在錯誤,則可以從此處重新確認啟用Google地圖的步驟
仔細檢查與API_KEY相關的內容
檢查APK簽名。 您必須在測試中分配調試API密鑰,並在釋放APK時分配Release API密鑰。 插入正確的API密鑰,清理並重建項目。
我去過那里一年了,我可以保證這是問題所在。 仔細檢查您的密鑰,以及它是否是調試或發布密鑰。
要檢查的另一件事是您的應用程序的軟件包名稱...讓我知道您是否已這樣做,並且仍然可以看到灰色視圖,而不是
更新:
當我意識到您的問題僅在簽名階段時,我試圖包含所有內容。
您只需要獲取調試密鑰庫的SHA-1指紋並將其放在“ API密鑰”對話框中即可。 並相信我,沒有什么我可以寫的比Google文檔更清楚,更有意義的了。
請按照此鏈接中的步驟獲取調試證書和SHA-1指紋 。
更新2:
為了獲得密鑰庫的SHA-1,您可以檢查此線程 。
您可以使用SupportMapFragment在片段中顯示地圖。
xml應該更改為
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/mapView"
android:layout_marginTop="-470dp"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
和你的班級如下
public class guide extends Fragment {
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.test, container, false);
((SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.mapView)).getMapAsync (new OnMapReadyCallback(){
@Override
public void onMapReady(final GoogleMap map) {
this.mMap = map;
mMap.setMyLocationEnabled(true);
setUpMap();
}
});
return v;
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
注意:如果找不到SupportMapFragment的getMapAsync方法,請更新您的Google Play服務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.