2015-05-27 53 views
2

我知道這些都是很多的答案了這樣的問題,但沒有爲我工作。Android的工作室 - java的:包com.google.android.gms.maps不存在

我最近將我的Eclipse項目導入到Android Studio(它不是Gradle樣式)。我想添加谷歌地圖到這個項目。

我加premission到AndroidManifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

創建活動:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<fragment 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" android:id="@+id/map" tools:context=".MapsActivity" 
    android:name="com.google.android.gms.maps.SupportMapFragment" /> 

,並添加以下代碼:

package com.myappname.app; 

import android.app.Activity; 
import android.os.Bundle; 

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

/** 
* Created by Dogface on 5/27/2015. 
*/ 
public class GMapActivity extends Activity { 

    private GoogleMap mMap; // Might be null if Google Play services APK is not available. 

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

    @Override 
    protected void onResume() { 
     super.onResume(); 
     setUpMapIfNeeded(); 
    } 

    /** 
    * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly 
    * installed) and the map has not already been instantiated.. This will ensure that we only ever 
    * call {@link #setUpMap()} once when {@link #mMap} is not null. 
    * <p/> 
    * If it isn't installed {@link SupportMapFragment} (and 
    * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to 
    * install/update the Google Play services APK on their device. 
    * <p/> 
    * A user can return to this FragmentActivity after following the prompt and correctly 
    * installing/updating/enabling the Google Play services. Since the FragmentActivity may not 
    * have been completely destroyed during this process (it is likely that it would only be 
    * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this 
    * method in {@link #onResume()} to guarantee that it will be called. 
    */ 
    private void setUpMapIfNeeded() { 
     // Do a null check to confirm that we have not already instantiated the map. 
     if (mMap == null) { 
      // Try to obtain the map from the SupportMapFragment. 
      mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
        .getMap(); 
      // Check if we were successful in obtaining the map. 
      if (mMap != null) { 
       setUpMap(); 
      } 
     } 
    } 

    /** 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, we 
    * just add a marker near Africa. 
    * <p/> 
    * This should only be called once and when we are sure that {@link #mMap} is not null. 
    */ 
    private void setUpMap() { 
     mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
    } 

} 

,但是當我編譯。我得到這些錯誤:

Error:(6, 35) java: package com.google.android.gms.maps does not exist 
Error:(7, 35) java: package com.google.android.gms.maps does not exist 
Error:(8, 41) java: package com.google.android.gms.maps.model does not exist 
Error:(9, 41) java: package com.google.android.gms.maps.model does not exist 
Error:(16, 13) java: cannot find symbol 
    symbol: class GoogleMap 
    location: class com.app.GMapActivity 
Error:(21, 32) java: cannot find symbol 
    symbol: variable activity_maps 
    location: class com.app.R.layout 
Error:(50, 22) java: cannot find symbol 
    symbol: class SupportMapFragment 
    location: class com.app.GMapActivity 
Error:(50, 42) java: cannot find symbol 
    symbol: method getSupportFragmentManager() 
    location: class com.app.GMapActivity 
Error:(66, 57) java: cannot find symbol 
    symbol: class LatLng 
    location: class com.app.GMapActivity 
Error:(66, 28) java: cannot find symbol 
    symbol: class MarkerOptions 
    location: class com.app.GMapActivity 

回答

5

您需要在您的的build.gradle文件中添加這些依賴關係:

compile 'com.google.android.gms:play-services-base:7.3.0' 
compile 'com.google.android.gms:play-services-location:7.3.0' 
+0

我也沒有成功。注意,它不搖籃 – m3hdi

+0

@ m3hdi你怎麼能不擁有的gradle一個Android Studio項目??? –

-1

推出Android SDK管理器,從額外安裝谷歌庫。

您應該在/ extras/Google文件夾中看到Google地圖庫。

導入這個項目作爲庫到應用程序。

+0

這是在Android Studio中推薦的方式嗎? –

+0

@IgorGanapolsky這是日食 – Steve