背景:
应用程序概述:我正在开发一个Android应用程序,它将在列表视图中显示一个位置列表,然后最终允许用户在用户点击时在Google Map上显示这些位置(使用Google Map v2 API)列表视图屏幕上的按钮。此时我只想在列表视图中显示地点列表,只需显示我当前位置的地图,并显示我的位置标记。
当前状态:到目前为止,应用程序正确显示了包含地点的列表视图,但是当单击按钮以显示地图时,我得到一个显示缩放控件的空白屏幕。您将在下面找到LogCat。我认为关键错误信息如下:
04-03 09:52:24.295:E / Google Maps Android API(1896):无法加载地图。无法联系Google服务器。
平台:我正在开发在Froyo上运行的应用程序以及稍后......但是现在我只是想让它在Froyo上运行。
键:我创建了一个启用了以下服务的密钥:“Google Maps Android API v2”和“Places API”已启用。
这是完整的LogCat。注意:LogCat以无数个“无法联系Google服务器”结束,所以我用最后3条消息将其剪掉。
04-03 09:42:17.447: D/Your Location(1896): latitude:32.91296010000001, longitude: -117.17031355
04-03 09:42:36.577: D/dalvikvm(1896): threadid=1: still suspended after undo (sc=1 dc=1 s=Y)
04-03 09:42:36.577: D/dalvikvm(1896): GC_FOR_MALLOC freed 6014 objects / 359192 bytes in 82ms
04-03 09:42:39.467: D/Places Status(1896): OK
04-03 09:43:53.437: D/dalvikvm(1896): GC_FOR_MALLOC freed 7413 objects / 369560 bytes in 226ms
04-03 09:43:54.537: D/dalvikvm(1896): DexOpt: couldn't find field Landroid/content/res/Configuration;.smallestScreenWidthDp
04-03 09:43:54.537: W/dalvikvm(1896): VFY: unable to resolve instance field 23
04-03 09:43:54.537: D/dalvikvm(1896): VFY: replacing opcode 0x52 at 0x0012
04-03 09:43:54.537: D/dalvikvm(1896): VFY: dead code 0x0014-0018 in Lcom/google/android/gms/common/GooglePlayServicesUtil;.b (Landroid/content/res/Resources;)Z
04-03 09:43:56.117: D/dalvikvm(1896): GC_FOR_MALLOC freed 3000 objects / 314664 bytes in 84ms
04-03 09:43:56.307: W/dalvikvm(1896): Unable to resolve superclass of Lmaps/p/s; (425)
04-03 09:43:56.307: W/dalvikvm(1896): Link of class 'Lmaps/p/s;' failed
04-03 09:43:56.307: W/dalvikvm(1896): Unable to resolve superclass of Lmaps/y/bo; (3818)
04-03 09:43:56.307: W/dalvikvm(1896): Link of class 'Lmaps/y/bo;' failed
04-03 09:43:56.307: W/dalvikvm(1896): Unable to resolve superclass of Lmaps/i/k; (4206)
04-03 09:43:56.307: W/dalvikvm(1896): Link of class 'Lmaps/i/k;' failed
04-03 09:43:56.317: E/dalvikvm(1896): Could not find class 'maps.i.k', referenced from method maps.z.ag.a
04-03 09:43:56.317: W/dalvikvm(1896): VFY: unable to resolve new-instance 3538 (Lmaps/i/k;) in Lmaps/z/ag;
04-03 09:43:56.317: D/dalvikvm(1896): VFY: replacing opcode 0x22 at 0x006d
04-03 09:43:56.397: D/dalvikvm(1896): VFY: dead code 0x006f-007f in Lmaps/z/ag;.a (Landroid/view/LayoutInflater;Lcom/google/android/gms/maps/GoogleMapOptions;ZLjava/lang/String;)Lmaps/z/ag;
04-03 09:43:57.727: D/dalvikvm(1896): GC_FOR_MALLOC freed 4941 objects / 398416 bytes in 102ms
04-03 09:43:58.367: E/Google Maps Android API(1896): Failed to load map. Could not contact Google servers.
04-03 09:51:41.015: D/dalvikvm(1896): GC_FOR_MALLOC freed 5101 objects / 340064 bytes in 97ms
04-03 09:52:24.295: E/Google Maps Android API(1896): Failed to load map. Could not contact Google servers.
04-03 09:53:33.888: D/dalvikvm(1896): GC_EXTERNAL_ALLOC freed 1948 objects / 134008 bytes in 260ms
04-03 09:57:34.449: E/Google Maps Android API(1896): Failed to load map. Could not contact Google servers.
04-03 09:57:34.649: D/libEGL(1896): loaded /system/lib/egl/libGLES_android.so
04-03 09:57:34.679: D/libEGL(1896): loaded /system/lib/egl/libEGL_adreno200.so
04-03 09:57:34.869: D/libEGL(1896): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
04-03 09:57:34.879: D/libEGL(1896): loaded /system/lib/egl/libGLESv2_adreno200.so
04-03 09:57:35.009: E/Google Maps Android API(1896): Failed to load map. Could not contact Google servers.
04-03 09:57:35.109: E/Google Maps Android API(1896): Failed to load map. Could not contact Google servers.
04-03 09:57:35.199: E/Google Maps Android API(1896): Failed to load map. Could not contact Google servers.
负责显示地图的代码如下:
public class PlacesMapActivity extends FragmentActivity {
// Nearest places
PlacesList nearPlaces;
private SupportMapFragment mMapFragment;
LatLng latLng;
double latitude;
double longitude;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_places);
// Getting intent data
Intent i = getIntent();
// Users current geo location
String user_latitude = i.getStringExtra("user_latitude");
String user_longitude = i.getStringExtra("user_longitude");
latLng = new LatLng(Double.parseDouble(user_latitude), Double.parseDouble(user_longitude));
// Nearplaces list
nearPlaces = (PlacesList) i.getSerializableExtra("near_places");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Try to obtain the map from the SupportMapFragment.
mMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Not found so make a new instance and add it to the transaction for swapping
if (mMapFragment == null) {
mMapFragment = SupportMapFragment.newInstance();
ft.add(R.id.map, mMapFragment);
}
ft.commit();
}
@Override
public void onAttachedToWindow() {
// Load the map here such that the fragment has a chance to completely load or else the GoogleMap value may be null
GoogleMap googleMap;
googleMap = (mMapFragment).getMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("My Spot")
.snippet("This is my spot!")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
super.onAttachedToWindow();
}
@Override
public void onAttachFragment(Fragment fragment) {
// TODO Auto-generated method stub
super.onAttachFragment(fragment);
}
根据调试器结果,似乎通过intent传递的额外值都存在且正确。
以下是Map布局的XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
不确定为何没有显示地图。想法?
答案 0 :(得分:0)
缺少以下权限:
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
一旦我添加了权限,我就能看到地图。