未知 class:googleMap 和另外 2 个

Unknown class: googleMap and 2 more

我的目标是按照说明 here 使 Google 地图成为主要 Activity。

我粘贴了网站上的代码(MainActivity.java 下面的代码)并导入了所有需要的包(alt+enter)。尽管如此,仍然存在一些错误(目前,我专注于一个,可能会在不久的将来询问更多关于其他的)。

if (googleMap == null) { 变成一条红线并说明了 3 件事:

Unknown class googleMap(此错误仅出现在 特定的 行)

Unexpected token(当鼠标悬停在 null 上时)&

Unexpected token(当鼠标悬停在 if 上时)

如果有帮助,我正在使用 android 工作室。谢谢,代码如下-

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 问题

在 gradle 文件中添加 google 地图依赖项并同步您的项目

如需深入信息:

通过 Google 遵循这些文档:Getting Started, Project Configuration, Get API Key

入门

This guide is a quick start to adding a map to an Android app. Android Studio is the recommended development environment for building an app with the Google Maps Android API.

  1. Download Android Studio
  2. Install the Google Play services SDK
  3. Create a Google Maps project
  4. Get a Google Maps API key
  5. Hello Map! Take a look at the code
  6. Connect an Android device
  7. Build and run your app

项目配置

本文档描述了在您的 Android 应用程序中使用 Google 地图 Android API 时您在开发项目中需要的所有配置。

The overall process of adding a map to an Android application is as follows:

  • Install the Android SDK.
  • Install and configure the Google Play services SDK, which includes the Google Maps Android API. Note: If you use the Google Maps Android API with a Google Maps APIs Premium Plan license, you must download and configure the Premium Plan SDK instead.
  • Get an API key. To do this, you will need to register a project in the Google Developers Console, find the signing certificate for your app, and create an API key.
  • Add the required settings to your application's manifest.

获取API密钥

To use the Google Maps Android API, you must register your app project on the Google Developers Console and get a Google API key which you can add to your app. Note: There are various types of API keys. You need an Android key not a browser key.

Your API key is based on a short form of your app's digital certificate. All Android apps are signed with a digital certificate for which you hold the private key. (Refer to the Android guide to signing your applications for more information about digital certificates.)

按照这些步骤,您将在 Main Activity 中获得一张地图。

希望这对您有所帮助。