2013-10-07 45 views
0

我想运行演示Google地图,但它没有显示地图。这里是我的code.I已经检查API密钥是正确的为什么我看不到地图

package com.example.googlemaps; 
import com.google.android.maps.MapActivity; 
import android.os.Bundle; 
import android.view.Menu; 

public class MainActivity extends MapActivity{ 

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

    @Override 
    protected boolean isRouteDisplayed() { 
     // TODO Auto-generated method stub 
     return true; 
    } 

} 

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.googlemaps" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="8"/> 
    <uses-permission android:name="android.permission.INTERNET" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <uses-library android:name="com.google.android.maps" /> 
     <activity 
      android:name="com.androidhive.googlemaps.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

布局XML

<?xml version="1.0" encoding="utf-8"?> 
<com.google.android.maps.MapView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mapView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:clickable="true" 
    android:apiKey="@string/Api_key" 
/> 
+1

我面临同样的问题,但我使用'SupportMapFragment',[这里](http://www.truiton.com/2013/05/android-supportmapfragment-example/)是例子。 –

+0

@MrSuS从最后3到4小时我strugling.i不要笏wat直接跳到mapfragment我想从地平面学习.. –

+0

你是否已经导入并包含谷歌播放服务的lib作为您的应用程序库依赖项? [这是](http://www.vogella.com/articles/AndroidGoogleMaps/article.html#maps_device)一个很好的教程,解释一切。 –

回答

2

对于获取地图老样子。 Google已经更新了它。 在XML布局中使用

<fragment 
      android:id="@+id/map" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      class="com.google.android.gms.maps.SupportMapFragment" /> 

提供清单文件,您的API密钥作为

<meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="YOUR-API-KEY" /> 
在应用程序代码

也不延伸MapActivity。相反,延长正常Activity

然后写下面的代码在你的OnCreate

int status = GooglePlayServicesUtil 
       .isGooglePlayServicesAvailable(getBaseContext()); 

    if (status != ConnectionResult.SUCCESS) { 
     // Google Play Services are not available 

     int requestCode = 10; 
     Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, 
       requestCode); 
     dialog.show(); 

    } else { 
     // Google Play Services are available 

     // Getting reference to the SupportMapFragment 
     SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 

     // Getting Google Map 
     myMap = fragment.getMap(); 

     // Enabling MyLocation in Google Map 
     myMap.setMyLocationEnabled(true); 

     // Getting LocationManager object from System Service 
     // LOCATION_SERVICE 
     LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 

     // Creating a criteria object to retrieve provider 
     Criteria criteria = new Criteria(); 

     // Getting the name of the best provider 
     String provider = locationManager.getBestProvider(criteria, true); 

     // Getting Current Location From GPS 
     Location location = locationManager.getLastKnownLocation(provider); 

     if (location != null) { 
      onLocationChanged(location); 
     } 

     locationManager.requestLocationUpdates(provider, 20000, 0, this); 
    } 

还添加以下代码上的onResume

int resultCode = GooglePlayServicesUtil 
       .isGooglePlayServicesAvailable(getApplicationContext()); 

    if (resultCode == ConnectionResult.SUCCESS) { 

    } else { 
     GooglePlayServicesUtil.getErrorDialog(resultCode, this, 
       RQS_GooglePlayServices); 
    } 

就是这样。 一切顺利!

1

版本1谷歌地图正式depricated.so只是使用版本2并添加谷歌播放服务libs.surely它将有助于获得谷歌地图。

  1. 公共类MainActivity延伸FragmentActivity { 保护无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout。activity_main);

    mGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) 
        .getMap(); } } 
    

  1. <uses-sdk 
        android:minSdkVersion="8" 
        android:targetSdkVersion="17" /> 
    
    <permission 
        android:name="com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE" 
        android:protectionLevel="signature" /> 
    
    <uses-feature 
        android:glEsVersion="0x00020000" 
        android:required="true" /> 
    
    <uses-permission android:name="com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE" 
    

    />

    <application 
        android:allowBackup="true" 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" 
        android:theme="@style/AppTheme" > 
        <activity 
         android:name="com.vogella.android.locationapi.maps.MainActivity" 
         android:label="@string/app_name" > 
         <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="AIzaSyDeYchyvOUsy_I68_RMtfsI5QVkqweIp9w" /> 
    </application> 
    


  1. <fragment 
        android:id="@+id/map" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        class="com.google.android.gms.maps.SupportMapFragment" /> 
    

    1. 参考此链接,http://www.vogella.com/articles/AndroidGoogleMaps/article.html
相关问题