2011-05-07 35 views
0

我想在我的应用中显示谷歌地图。我得到了map api key,并在我的xml文件中使用它,但google map没有显示,只有矩形块显示在我运行该应用程序时。我怎样才能在我的应用程序中显示谷歌地图?如何在我的应用中显示谷歌地图

活动类:

public class ShowMap extends MapActivity { 

    private MapController mapController; 
    private MapView mapView; 
    private LocationManager locationManager; 

    public void onCreate(Bundle bundle) { 
     super.onCreate(bundle); 
     setContentView(R.layout.main); // bind the layout to the activity 

     // create a map view 
     RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.mainlayout); 
     mapView = (MapView) findViewById(R.id.mapview); 
     mapView.setBuiltInZoomControls(true); 
     mapView.setStreetView(true); 
     mapController = mapView.getController(); 
     mapController.setZoom(14); // Zoon 1 is world view 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 
       0, new GeoUpdateHandler()); 
    } 

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

    public class GeoUpdateHandler implements LocationListener { 

     @Override 
     public void onLocationChanged(Location location) { 
      int lat = (int) (location.getLatitude() * 1E6); 
      int lng = (int) (location.getLongitude() * 1E6); 
      GeoPoint point = new GeoPoint(lat, lng); 
      mapController.animateTo(point); // mapController.setCenter(point); 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
     } 

     @Override 
     public void onProviderEnabled(String provider) { 
     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
     } 
    } 

,并layout/main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    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:clickable="true" 
     android:enabled="true" 
     android:apiKey="0mT--u1GbHdhnBJgPZU8zhoF2e4qdpCag32e7lQ" /> 

</RelativeLayout> 
+0

代码看起来还好。你确定API密钥是正确的吗? – slhck 2011-05-07 09:44:42

回答

0

我认为你已经使用调试键。而不是使用使用释放钥匙:

keytool -list -keystore filename.keystore 

欲了解更多详情请查看this

尝试this.Hope这会为你工作... :)

+0

如何使用“keytool -list -keystore filename.keystore”其实我是java和android中的新手,所以请详细解释... – SRam 2011-05-07 09:53:03

+0

您是否创建了应用程序的密钥库。创建一个.bat文件(创建一个叫做shell.bat的新文件并编辑它,然后在其中写入cmd,然后保存,现在关闭它,双击它,它会在该位置打开命令提示符),位于密钥库的相同位置文件,然后在该命令提示符处键入上面的行。然后按照相同的过程生成调试密钥。试试这个。 – 2011-05-07 09:58:26