本文介绍了谷歌Android API"无法加载地图"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想谷歌显示地图的地图。

我跟着这个。

我修正了一些问题和应用程序运行,但我无法显示地图。

下面我所做的:


  • 创建谷歌的控制台上的API项目

  • 创建一个ID

  • 激活谷歌地图Android版API V2的服务

  • 生成新的Andr​​oid键

  • 在清单
  • 进口这一关键
  • 随后这段视频,安装库

在这里,我的.java:

 公共类MainActivity扩展FragmentActivity {私有静态GoogleMap的MMAP = NULL;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    INT状态= GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());    如果(状态== ConnectionResult.SUCCESS){
       如果(MMAP == NULL){          MMAP =((SupportMapFragment)getSupportFragmentManager()findFragmentById(R.id.map)。)的GetMap();          如果(MMAP!= NULL){
            UiSettings设置= mMap.getUiSettings();            settings.setZoomControlsEnabled(真);
            settings.setCompassEnabled(真);
            settings.setRotateGesturesEnabled(真);
            settings.setTiltGesturesEnabled(真);
            settings.setScrollGesturesEnabled(真);
            settings.setZoomControlsEnabled(真);
            settings.setZoomGesturesEnabled(真);
            settings.setMyLocationButtonEnabled(假);            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            mMap.setMyLocationEnabled(假);
            }
          }
       }
    }
    保护无效onResume(){
        super.onResume();        INT状态= GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());        如果(状态== ConnectionResult.SUCCESS){
           如果(MMAP == NULL){              MMAP =((SupportMapFragment)getSupportFragmentManager()findFragmentById(R.id.map)。)的GetMap();              如果(MMAP!= NULL){
                UiSettings设置= mMap.getUiSettings();                settings.setZoomControlsEnabled(真);
                settings.setCompassEnabled(真);
                settings.setRotateGesturesEnabled(真);
                settings.setTiltGesturesEnabled(真);
                settings.setScrollGesturesEnabled(真);
                settings.setZoomControlsEnabled(真);
                settings.setZoomGesturesEnabled(真);
                settings.setMyLocationButtonEnabled(假);                mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                mMap.setMyLocationEnabled(假);
              }
          }
     }
 }

我activity_main.xml中:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>
    <片段
          机器人:ID =@ + ID /图
          机器人:layout_width =match_parent
          机器人:layout_height =match_parent
          类=com.google.android.gms.maps.SupportMapFragment/>
< / LinearLayout中>

和我的表现:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.map
    安卓版code =1
    机器人:=的versionName1.0>    <允许机器人:名字=com.example.map.permission.MAPS_RECEIVE安卓的ProtectionLevel =签名/>
    <使用许可权的android:NAME =com.example.map.permission.MAPS_RECEIVE/>
    <使用许可权的android:NAME =android.permission.INTERNET对/>
    <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>
    <使用许可权的android:NAME =com.google.android.providers.gsf.permission.READ_GSERVICES/>
    <使用许可权的android:NAME =android.permission.ACCESS_COARSE_LOCATION/>
    <使用许可权的android:NAME =android.permission.ACCESS_FINE_LOCATION/>
<使用许可权的android:NAME =android.permission.ACCESS_NETWORK_STATE/>    <用途特征
        机器人:glEsVersion =0x00020000
        机器人:要求=真/>    <用途-SDK
        安卓的minSdkVersion =12
        机器人:targetSdkVersion =17/>    <应用
        机器人:allowBackup =真
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <使用库机器人:名字=com.google.android.maps/>
        <活动
            机器人:名字=com.example.map.MainActivity
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        &所述;元数据
            机器人:名字=com.google.android.maps.v2.API_KEY
            机器人:值=<&的myKey GT;/>    < /用途>< /清单>


解决方案

您的应用程序标签内(在清单)地址:

 <使用库机器人:名字=com.google.android.maps/>

这个替换您的XML片段:

 <片段
      机器人:ID =@ + ID /图
      机器人:layout_width =match_parent
      机器人:layout_height =match_parent
      类=com.google.android.gms.maps.SupportMapFragment
    />

让您的活动延长FragmentActivity和的onCreate和onResume称之为code:

  INT状态= GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());如果(状态== ConnectionResult.SUCCESS){
   如果(MMAP == NULL){      MMAP =((SupportMapFragment)getSupportFragmentManager()findFragmentById(R.id.map)。)的GetMap();      如果(MMAP!= NULL){
        UiSettings设置= mMap.getUiSettings();        settings.setZoomControlsEnabled(真);
        settings.setCompassEnabled(真);
        settings.setRotateGesturesEnabled(真);
        settings.setTiltGesturesEnabled(真);
        settings.setScrollGesturesEnabled(真);
        settings.setZoomControlsEnabled(真);
        settings.setZoomGesturesEnabled(真);
        settings.setMyLocationButtonEnabled(假);        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        mMap.setMyLocationEnabled(假);
      }
    }
}

不要忘了你的声明MMAP变量:

 私人GoogleMap的MMAP;

I'm trying to display Google Map's map.

I followed this tutorial.

I Fixed some problems and the application runs, but I could not display the map.

Here what I did:

  • Created an API project on the google console
  • Created an ID
  • Activated "Google Maps Android API v2" service
  • Generated new Android key
  • Imported this key in manifest
  • Followed this video to install the library

And here my .java:

public class MainActivity extends FragmentActivity {

private static GoogleMap mMap = null;

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

    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

    if(status == ConnectionResult.SUCCESS){
       if (mMap == null) {

          mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

          if (mMap != null) {
            UiSettings settings = mMap.getUiSettings();

            settings.setZoomControlsEnabled(true);
            settings.setCompassEnabled(true);
            settings.setRotateGesturesEnabled(true);
            settings.setTiltGesturesEnabled(true);
            settings.setScrollGesturesEnabled(true);
            settings.setZoomControlsEnabled(true);
            settings.setZoomGesturesEnabled(true);
            settings.setMyLocationButtonEnabled(false);

            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            mMap.setMyLocationEnabled(false);
            }
          }
       }
    }
    protected void onResume() {
        super.onResume();

        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

        if(status == ConnectionResult.SUCCESS){
           if (mMap == null) {

              mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

              if (mMap != null) {
                UiSettings settings = mMap.getUiSettings();

                settings.setZoomControlsEnabled(true);
                settings.setCompassEnabled(true);
                settings.setRotateGesturesEnabled(true);
                settings.setTiltGesturesEnabled(true);
                settings.setScrollGesturesEnabled(true);
                settings.setZoomControlsEnabled(true);
                settings.setZoomGesturesEnabled(true);
                settings.setMyLocationButtonEnabled(false);

                mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                mMap.setMyLocationEnabled(false);
              }
          }
     }
 }

my activity_main.xml:

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


</LinearLayout>

And my manifest:

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

    <permission android:name="com.example.map.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
    <uses-permission android:name="com.example.map.permission.MAPS_RECEIVE"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <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" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="com.google.android.maps" />
        <activity
            android:name="com.example.map.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="<MyKey>"/>

    </application>

</manifest>
解决方案

Inside your application tag (in manifest) add:

<uses-library android:name="com.google.android.maps" />

Replace your fragment XML by this:

<fragment
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.SupportMapFragment"
    />

Let your activity extend FragmentActivity and onCreate and onResume call this code:

int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

if(status == ConnectionResult.SUCCESS){
   if (mMap == null) {

      mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

      if (mMap != null) {
        UiSettings settings = mMap.getUiSettings();

        settings.setZoomControlsEnabled(true);
        settings.setCompassEnabled(true);
        settings.setRotateGesturesEnabled(true);
        settings.setTiltGesturesEnabled(true);
        settings.setScrollGesturesEnabled(true);
        settings.setZoomControlsEnabled(true);
        settings.setZoomGesturesEnabled(true);
        settings.setMyLocationButtonEnabled(false);

        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        mMap.setMyLocationEnabled(false);
      }
    }
}

Don't forget to declare your mMap variable like this:

private GoogleMap mMap;

这篇关于谷歌Android API&QUOT;无法加载地图&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:17