2017-05-09 60 views
0

我一直想要在我一直在開發的應用中使用Android Google Map API,並且在實施時遇到問題。我遵循Google Developers提供的教程(https://developers.google.com/maps/documentation/android-api/map-with-marker),當我運行該應用程序時,導致出現此構建錯誤:Build Error。我已經找到了解決這個問題的方法,但一直沒能找到解決方案。Android Google Map V2 API導致構建錯誤

activity_maps.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
 
      xmlns:tools="http://schemas.android.com/tools" 
 
      android:id="@+id/map" 
 
      android:name="com.google.android.gms.maps.SupportMapFragment" 
 
      android:layout_width="match_parent" 
 
      android:layout_height="match_parent" 
 
      tools:context="com.example.mapwithmarker.MapsMarkerActivity" />

MapsWithMarkerActivity

package com.example.mapwithmarker; 

    import android.os.Bundle; 
    import android.support.v7.app.AppCompatActivity; 

    import com.google.android.gms.maps.CameraUpdateFactory; 
    import com.google.android.gms.maps.GoogleMap; 
    import com.google.android.gms.maps.OnMapReadyCallback; 
    import com.google.android.gms.maps.SupportMapFragment; 
    import com.google.android.gms.maps.model.LatLng; 
    import com.google.android.gms.maps.model.MarkerOptions; 

    /** 
    * An activity that displays a Google map with a marker (pin) to indicate a particular location. 
    */ 
    public class MapsMarkerActivity extends AppCompatActivity 
    implements OnMapReadyCallback { 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      // Retrieve the content view that renders the map. 
      setContentView(R.layout.activity_maps); 
      // Get the SupportMapFragment and request notification 
      // when the map is ready to be used. 
      SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
      mapFragment.getMapAsync(this); 
     } 

     @Override 
     public void onMapReady(GoogleMap googleMap) { 
      // Add a marker in Sydney, Australia, 
      // and move the map's camera to the same location. 
      LatLng sydney = new LatLng(-33.852, 151.211); 
      googleMap.addMarker(new MarkerOptions().position(sydney) 
      .title("Marker in Sydney")); 
      googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
     } 
} 

的build.gradle(模塊APP)

apply plugin: 'com.android.application' 

     android { 
      compileSdkVersion 24 
      buildToolsVersion "23.0.3" 
      defaultConfig { 
      applicationId "com.example.mapwithmarker" 
      minSdkVersion 15 
      targetSdkVersion 24 
      versionCode 1 
      versionName "1.0" 
      testInstrumentationRunner 
    "android.support.test.runner.AndroidJUnitRunner" 
      resValue "string", "google_maps_key", 
    (project.findProperty("GOOGLE_MAPS_API_KEY") ?: "") 
     } 
     buildTypes { 
      release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 

}

dependencies { 
     compile fileTree(dir: 'libs', include: ['*.jar']) 
     androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
      exclude group: 'com.android.support', module: 'support-annotations' 
     }) 
     compile 'com.android.support:appcompat-v7:24.2.1' 
     compile 'com.google.android.gms:play-services:9.8.0' 
     testCompile 'junit:junit:4.12' 
    } 

AndroidManifest.xml中

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

     <application 
      android:allowBackup="true" 
      android:icon="@mipmap/ic_launcher" 
      android:label="@string/app_name" 
      android:supportsRtl="true" 
      android:theme="@style/AppTheme"> 

      <meta-data 
       android:name="com.google.android.gms.version" 
       android:value="@integer/google_play_services_version" /> 


      <meta-data 
       android:name="com.google.android.geo.API_KEY" 
       android:value="@string/google_maps_key" /> 

      <activity 
       android:name=".MapsMarkerActivity" 
       android:label="@string/title_activity_maps"> 
       <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 

        <category android:name="android.intent.category.LAUNCHER" /> 
       </intent-filter> 
      </activity> 
     </application> 

    </manifest> 

我使用Nexus 5X API 23(在Android 6.0,API 23)模擬器來運行這個。 非常感謝提前

回答

0

其中一種方法來擺脫這一點,只需添加您需要的依賴。在你的情況下,

compile 'com.google.android.gms:play-services:9.8.0'

更換到

compile 'com.google.android.gms:play-services-maps:9.8.0'

+0

嗨,已經得到了應用,成功地構築,所以向你表示感謝。我遇到了一個新的錯誤,導致身份驗證在服務器上失敗。我是否需要爲此使用另一個API密鑰? – mjonjones

+0

'我遇到了一個導致身份驗證在服務器上失敗的新錯誤。這似乎是另一個與Google地圖無關的問題。即使您沒有獲得api鍵,也沒有在構建gradle中獲得此「GOOGLE_MAPS_API_KEY」值,那麼您將看到一個空白區域而不是地圖。 – Blackkara