2017-03-31 90 views
0

我想在我的一个活动中显示一个非常基本的MapView,但是,我的MapView永远是空白的,永远不会加载任何地图。所述MapView仅仅是具有其他元素的活动的200px高度。我没有从向导创建一个Map活动。Android谷歌地图基本地图

我的表现有:

<meta-data 
    android:name="com.google.android.gms.version" 
    android:value="@integer/google_play_services_version" /> 

<!-- 
    The API key for Google Maps-based APIs. 
--> 
<meta-data 
    android:name="com.google.android.geo.API_KEY" 
    android:value="my real code" /> 

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

我的build.gradle具有依赖性:

compile 'com.google.android.gms:play-services-maps:10.2.1' 

我AndroidStudio有GOOGLEPLAY包。你知道我错过了什么吗?

这是我的MapView XML代码:

<LinearLayout 
    android:id="@+id/geolocationLinearLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:text="Geolocation" 
     android:textStyle="bold|italic" /> 

    <com.google.android.gms.maps.MapView 
     android:id="@+id/mapView" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" /> 
</LinearLayout> 

这是我的活动Java类映射声明和装载:

声明:

MapView mapView; 
GoogleMap map; 
static final LatLng HAMBURG = new LatLng(53.558, 9.927); 

内OnCreateView:

mapView = (MapView) view.findViewById(R.id.mapView); 
mapView.getMapAsync(new OnMapReadyCallback() { 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     map = googleMap; 
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15)); 
    } 
}); 
+0

mapView.onCreate(...)在哪里? – Blackkara

+0

我加了mapView.onCreate(mBundle);现在我看到底部左侧带有Google徽标的mapView,但是地图未加载......我需要编写其他任何代码吗? – user1060551

+0

如果地图空白,常见问题是API密钥错误 –

回答

1

注:我假设你有有效的密钥

mapView = (MapView) view.findViewById(R.id.mapView);  
mapView .onCreate(null); 
mapView.getMapAsync(...) 

而且也低于采用其它所需的方法,如,请参阅下面的文档的所有方法

@Override 
public void onResume() { 
    super.onResume(); 
    mapView .onResume(); 
}