无法显示 mapview V2
Can't get mapview V2 to show
我正在努力在我正在开发的应用程序中获取地图视图。
我已经在 google 控制台中成功生成了一个 API 密钥,并将权限添加到清单并检查 logcat 是否有一些失败但没有。
布局如下:
<LinearLayout
android:id="@+id/main_box_container2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/box_container2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:minWidth="450dp"
android:orientation="vertical" >
<TextView
android:id="@+id/geo_map_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/distribuzione_title"
android:textSize="28sp"
android:textStyle="bold" />
<com.google.android.gms.maps.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.google.android.gms.maps.MapView>
</LinearLayout>
</LinearLayout>
这是清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.previnet.mobile.dashboard.credem"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="23" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<permission
android:name="it.previnet.mobile.dashboard.credem.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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="it.previnet.mobile.dashboard.credem.permission.MAPS_RECEIVE" />
<application
android:allowBackup="true"
android:icon="@drawable/logo_credemvita"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".DashboardActivity"
android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDfFnshaZgWuun842Z3wzGN4gqp2dj98ew" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
这里是代码:
protected void initMap(View geo) {
// Gets the MapView from the XML layout and creates it
mapView = (MapView) geo.findViewById(R.id.map_view);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
map.getUiSettings().setMyLocationButtonEnabled(true);
map.setMyLocationEnabled(true);
// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
try {
MapsInitializer.initialize(this.getActivity());
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
// Updates the location and zoom of the MapView
LatLng latLng = new LatLng(43.1, -87.9);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
map.addMarker(new MarkerOptions().position(new LatLng(43.1, -87.9)).title("Marker"));
map.animateCamera(cameraUpdate);
}
我发现,问题不是地图而是上面的文本视图。将 layout_height 更改为 wrap_content 广告知道我可以看到 MapView
为什么要设置所有参数高度match_parent???
我建议这个代码:
<LinearLayout
android:id="@+id/main_box_container2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/box_container2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:minWidth="450dp"
android:orientation="vertical" >
<TextView
android:id="@+id/geo_map_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/distribuzione_title"
android:textSize="28sp"
android:textStyle="bold" />
<com.google.android.gms.maps.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</com.google.android.gms.maps.MapView>
</LinearLayout>
我正在努力在我正在开发的应用程序中获取地图视图。 我已经在 google 控制台中成功生成了一个 API 密钥,并将权限添加到清单并检查 logcat 是否有一些失败但没有。
布局如下:
<LinearLayout
android:id="@+id/main_box_container2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/box_container2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:minWidth="450dp"
android:orientation="vertical" >
<TextView
android:id="@+id/geo_map_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/distribuzione_title"
android:textSize="28sp"
android:textStyle="bold" />
<com.google.android.gms.maps.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.google.android.gms.maps.MapView>
</LinearLayout>
</LinearLayout>
这是清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.previnet.mobile.dashboard.credem"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="23" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<permission
android:name="it.previnet.mobile.dashboard.credem.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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="it.previnet.mobile.dashboard.credem.permission.MAPS_RECEIVE" />
<application
android:allowBackup="true"
android:icon="@drawable/logo_credemvita"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".DashboardActivity"
android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDfFnshaZgWuun842Z3wzGN4gqp2dj98ew" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
这里是代码:
protected void initMap(View geo) {
// Gets the MapView from the XML layout and creates it
mapView = (MapView) geo.findViewById(R.id.map_view);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
map.getUiSettings().setMyLocationButtonEnabled(true);
map.setMyLocationEnabled(true);
// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
try {
MapsInitializer.initialize(this.getActivity());
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
// Updates the location and zoom of the MapView
LatLng latLng = new LatLng(43.1, -87.9);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
map.addMarker(new MarkerOptions().position(new LatLng(43.1, -87.9)).title("Marker"));
map.animateCamera(cameraUpdate);
}
我发现,问题不是地图而是上面的文本视图。将 layout_height 更改为 wrap_content 广告知道我可以看到 MapView
为什么要设置所有参数高度match_parent???
我建议这个代码:
<LinearLayout
android:id="@+id/main_box_container2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/box_container2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:minWidth="450dp"
android:orientation="vertical" >
<TextView
android:id="@+id/geo_map_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/distribuzione_title"
android:textSize="28sp"
android:textStyle="bold" />
<com.google.android.gms.maps.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</com.google.android.gms.maps.MapView>
</LinearLayout>