2016-06-12 35 views
0

我的目標是按照指示here來製作Google地圖的主要活動。未知分類:googleMap和2個以上

我粘貼了網站的代碼(MainActivity.java的代碼如下)並導入了所有必需的包(alt + enter)。儘管如此,仍然存在一些錯誤(現在,我專注於一個問題,在不久的將來可能會問及其他問題)。

if (googleMap == null) {得到一條紅線,並說三件事情:

Unknown class googleMap(此錯誤是隻有在這個特定線)

Unexpected token(當鼠標移到null)&

Unexpected token (當鼠標經過if時)

如果有幫助,我正在使用android studio。感謝和代碼如下─

import android.content.Context; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapFragment; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
    } 

    public GoogleMap googleMap; // Might be null if Google Play services APK is not available. 

    if (googleMap == null) { 

     googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMapAsync(this); 

     // check if map is created successfully or not 
     if (googleMap == null) { 
      Toast.makeText(getApplicationContext(), 
        "Could not create Maps", Toast.LENGTH_SHORT).show(); 
     } 
     else { 
      // Changing map type 
      //TODO 
     } 
    } 

    public GpsLocation(Context mContext, TextView gpsStatusTextView) { 
     this.mContext = mContext; 
     this.gpsStatusTextView = gpsStatusTextView; 
     getLocation(); 
    } 

    public Location getLocation() { 
     try { 
      locationManager = (LocationManager) mContext 
        .getSystemService(Context.LOCATION_SERVICE); 

      // getting GPS status 
      isGPSEnabled = locationManager 
        .isProviderEnabled(LocationManager.GPS_PROVIDER); 

      // getting network status 
      isNetworkEnabled = locationManager 
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

      if (!isGPSEnabled && !isNetworkEnabled) { 
       // no network provider is enabled 
      } else { 
       this.canGetLocation = true; 
       // First get location from Network Provider 
       if (isNetworkEnabled) { 
        locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("Network", "Network"); 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       } 
       // if GPS Enabled get lat/long using GPS Services 
       if (isGPSEnabled) { 
        if (location == null) { 
         locationManager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         Log.d("GPS Enabled", "GPS Enabled"); 
         if (locationManager != null) { 
          location = locationManager 
            .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 
          } 
         } 
        } 
       } 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return location; 
    } 

    gpsLocation = new GpsLocation(this, gpsStatusTextView); 

    if (gpsLocation.canGetLocation()){ 
     double longitude = gpsLocation.getLongitude(); 
     double latitude = gpsLocation.getLatitude(); 
    } 
} 

很抱歉,如果這是一個總的的n00b問題

回答

0

如需更深入的信息:

按照Google的這些文檔:Getting Started,Project Configuration,Get API Key

入門

本指南是一個快速的開始添加地圖到Android應用程序。 Android Studio是使用Google Maps Android API構建應用的推薦開發環境。

  1. 下載Android工作室
  2. 安裝谷歌Play服務SDK
  3. 創建一個谷歌地圖項目
  4. 獲取谷歌地圖API密鑰
  5. 你好地圖!看看代碼
  6. 連接Android設備
  7. 生成並運行你的應用程序

項目配置

在該文件中描述了所有使用當您在開發項目所需要的配置您的Android應用中的Google地圖Android API。

添加到地圖Android應用程序的整個過程如下:

  • 安裝Android SDK。
  • 安裝並配置Google Play服務SDK,其中包含Google Maps Android API。 注意:如果您將Google Maps Android API與Google Maps APIs Premium Plan許可證一起使用,則必須改爲下載並配置Premium Plan SDK。
  • 獲取API密鑰。爲此,您需要在Google Developers Console中註冊一個項目,找到您的應用的簽名證書並創建一個API密鑰。
  • 將所需的設置添加到應用程序的清單中。

獲取API密鑰

要使用谷歌地圖Android的API,你必須註冊在谷歌開發者控制檯您的應用項目,並讓您可以添加到您的應用程序谷歌的API密鑰。 注意:有各種類型的API密鑰。您需要一個Android密鑰不是瀏覽器密鑰。

您的API密鑰基於您應用的數字證書的簡寫形式。所有Android應用都使用您持有私鑰的數字證書進行簽名。 (請參閱Android指南,以簽署您的應用程序以獲取有關數字證書的更多信息。)

按照以下步驟操作,您將在Main Activity工作中獲取地圖。

希望這會有所幫助。

+0

你好!我有一個API密鑰和工作,以及什麼是地圖活動? –

+0

沒有更多未知的類googleMap?很高興聽到 –

+0

aahhh,我想你誤會了我!我仍然得到這個錯誤,但我的API密鑰正在工作,我非常肯定這回合 –

0

加入gradle這個文件中的谷歌地圖的依賴,並同步您的項目

+0

我們該怎麼做? –

+0

谷歌地圖依賴'com.google.android.gms:play-services:9.0.2'已經在build.gradle –