无法在 Android Studio 中添加地图,如谷歌“入门"中所述页;Android Studio 报错 [英] Cannot add map in Android Studio as stated in Google "Getting Started" page; Android Studio gives error

查看:32
本文介绍了无法在 Android Studio 中添加地图,如谷歌“入门"中所述页;Android Studio 报错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照说明使用 Google Maps API 制作应用.为此,我完全按照此处所述的说明进行操作:我已经拥有 Android Studio 并开发了几个非常基本的联系人列表应用程序,但为此我获得了 API 密钥,设置了位置和网络权限并设置了 OpenGL ES v2.我直接从网站复制了以下两段代码并将其粘贴到活动 .xml 文件和 java 类中.

I'm trying to follow the directions to make an app with the Google Maps API. To do this, I'm following the instructions exactly as described here: I already had Android Studio and worked on a couple of very elementary Contact List apps, but for this I got the API key, set up the location and network permissions and set up OpenGL ES v2. I copied the following two sections of code right from the website and pasted it to the activity .xml file and the java class.

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>

到布局 XML 文件,然后到 MainActivity.java 文件.

to the layout XML file and the following to the MainActivity.java file.

package com.example.mapdemo;

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

public class MainActivity extends Activity {

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

我遇到了第一个代码块的问题.Android Studio 将android.gms.maps.MapFragment"作为错误返回,将其突出显示为红色.对于 R 的所有用法,它也给了我一个错误……R 的任何用法都以红色突出显示.我尝试导入 android.R 但没有奏效.但我想一次一个问题.

I run into a problem with the first block of code. Android Studio returns the "android.gms.maps.MapFragment" as an error, by highlighting it red. It also gives me an error for all usages of R... any usage of Ris highlighted in red. I tried importing android.R but didn't work. But I guess one problem at a time.

有什么想法吗?谢谢!

推荐答案

我刚刚开始了一个新项目并按照该链接中的说明进行操作.它对我有用.需要注意的一件事是,我无需更改 MainActivity.java 中的任何内容.

I just started a new project and followed the instructions from that link. It worked for me. One thing to note is that I didn't have to change anything in MainActivity.java.

我建议在 Android Studio 中启动一个新项目,选择Blank Activity".修改 AndroidManifest.xmlbuild.gradleactivity_main.xml,但将 MainActivity.java 中的所有内容保留为是.

I would recommend starting a new project in Android Studio, select "Blank Activity". Modify AndroidManifest.xml, build.gradle, and activity_main.xml, but leave everything in MainActivity.java as is.

需要注意的一点是,从 SDK Manager 中,您需要安装:

One thing to note is that from the SDK Manager, you need to install:

  • Android 支持库
  • Android 支持库
  • Google Play 服务
  • Google 存储库

来自我的工作示例:

在 build.gradle 中:

In build.gradle:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'

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

AndroidManifest.xml:

AndroidManifest.xml:

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

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


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

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

</application>

我在 MainActivity 中设置了一个 Fragment.

I got it set up with a Fragment within MainActivity.

activity_main.xml(这里没什么特别的):

activity_main.xml (nothing special here):

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".MainActivity" tools:ignore="MergeRootFrame" />

fragment_main.xml:

fragment_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

MainActivity.java:

MainActivity.java:

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.view.Menu;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.location.Location;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public static class PlaceholderFragment extends Fragment {

        MapView mMapView;
        private GoogleMap googleMap;
        Location location;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // inflate and return the layout
            View v = inflater.inflate(R.layout.fragment_main, container,
                    false);
            mMapView = (MapView) v.findViewById(R.id.mapView);
            mMapView.onCreate(savedInstanceState);

            mMapView.onResume();// needed to get the map to display immediately

            try {
                MapsInitializer.initialize(getActivity().getApplicationContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
            googleMap = mMapView.getMap();
            googleMap.setMyLocationEnabled(true);

            location = googleMap.getMyLocation();

            if (location != null) {

                // latitude and longitude
                LatLng lat = new LatLng(location.getLatitude(), location.getLongitude());

                //double latitude = 17.385044;
                //double longitude = 78.486671;
                //LatLng lat = new LatLng(latitude,longitude);


                // create marker

                MarkerOptions marker = new MarkerOptions().position(
                        lat).title("Hello Maps");

                // Changing marker icon
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

                // adding marker
                googleMap.addMarker(marker);

                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(lat).zoom(12).build();


                googleMap.animateCamera(CameraUpdateFactory
                        .newCameraPosition(cameraPosition));

            }

            // Perform any camera updates here
            return v;
        }


        @Override
        public void onResume() {
            super.onResume();
            mMapView.onResume();
        }

        @Override
        public void onPause() {
            super.onPause();
            mMapView.onPause();
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            mMapView.onDestroy();
        }

        @Override
        public void onLowMemory() {
            super.onLowMemory();
            mMapView.onLowMemory();
        }
    }
}

结果:

这篇关于无法在 Android Studio 中添加地图,如谷歌“入门"中所述页;Android Studio 报错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆