2010-11-08 84 views
0

实景地图我都映射为m的Android应用,但它不能很好地工作。我花了最后两个小时一行一行地看代码,但我不知道是什么问题。我在教程http://mobiforge.com/developing/story/using-google-maps-android中完成了所有操作。我在清单中使用谷歌地图。地图显示标志,不显示地图(而不是背景网格线),几秒钟后缩放控件消失,不再回来。什么可能是一个问题? alt text地图显示的标记,但不显示在后台

这是活动类。

package com.roomate.www; 
import java.util.List; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 
import com.google.android.maps.MapView.LayoutParams; 
import com.google.android.maps.Overlay; 

import android.view.View; 
import android.widget.LinearLayout; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Point; 
import android.os.Bundle; 
import android.util.Log; 

public class Location extends MapActivity{ 
MapView mapView; 
    MapController mc; 
    GeoPoint p; 

    class MapOverlay extends com.google.android.maps.Overlay 
    { 
     @Override 
     public boolean draw(Canvas canvas, MapView mapView, 
     boolean shadow, long when) 
     { 
      super.draw(canvas, mapView, shadow);     

      //---translate the GeoPoint to screen pixels--- 
      Point screenPts = new Point(); 
      mapView.getProjection().toPixels(p, screenPts); 

      //---add the marker--- 
      Bitmap bmp = BitmapFactory.decodeResource(
       getResources(), R.drawable.marker);    
      canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);   
      return true; 
     } 
    } 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.map_result); 

    mapView = (MapView) findViewById(R.id.mapView); 
    mapView.setStreetView(true); 
     LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom); 
     View zoomView = mapView.getZoomControls(); 

     zoomLayout.addView(zoomView, 
      new LinearLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT)); 
     mapView.displayZoomControls(true); 

     mc = mapView.getController(); 
     String coordinates[] = {"1.352566007", "103.78921587"}; 
     double lat = Double.parseDouble(coordinates[0]); 
     double lng = Double.parseDouble(coordinates[1]); 

     p = new GeoPoint(
      (int) (lat * 1E6), 
      (int) (lng * 1E6)); 

     mc.animateTo(p); 
     mc.setZoom(17); 
     MapOverlay mapOverlay = new MapOverlay(); 
     List<Overlay> listOfOverlays = mapView.getOverlays(); 
     listOfOverlays.clear(); 
     listOfOverlays.add(mapOverlay);   

     mapView.invalidate(); 

} 
@Override 
    protected boolean isRouteDisplayed() { 
     return false; 
    } 
} 

这是地图的布局。

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    > 
    <LinearLayout 
    android:layout_gravity="left" 
    android:gravity="left" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <ImageButton 
    android:id="@+id/imbReturnMap" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:fitsSystemWindows="true" 
    android:src="@drawable/close" 
    android:layout_gravity="left"> 
    </ImageButton> 
    </LinearLayout> 

    <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="right" 
    android:layout_gravity="right"> 
    <ImageButton 
    android:id="@+id/imbCloseM" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:fitsSystemWindows="true" 
    android:src="@drawable/close" 
    android:layout_gravity="right"> 
    </ImageButton> 
    </LinearLayout> 

</LinearLayout> 

<RelativeLayout 
    android:id="@+id/mainlayout" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
    <com.google.android.maps.MapView 
     android:id="@+id/mapView" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:apiKey="0WR3Cl0evv0U6G2xH9ZnvEFYbDiSDUu_GRovTUQ" 
       /> 
    <LinearLayout android:id="@+id/zoom" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     /> 

</RelativeLayout> 
</LinearLayout> 

回答

3

由您的屏幕截图显示,您的设备或仿真器没有任何连接。零棒图标告诉你这一点。

如果这是一个模拟器,它可能无法在启动时检测到互联网,或者您可能按[F8]关闭连接。重新启动模拟器,如果一切顺利,你会得到的模拟信号强度正常的两间酒吧。

在等待模拟器重新启动时,还要确认您在清单中保留了INTERNET权限。这对仿真器的互联网访问整体没有影响 - 然而,缺乏的权限也将导致您所看到的行为。

0

首先,你必须在你的模拟器安装谷歌API包。

其次Google改变了获取map api key的过程。

this教程可以解决你的问题

而且在地图上显示的标记见this