应用程序显示网格屏幕而不是谷歌地图

时间:2013-06-05 15:43:19

标签: android google-maps map google-maps-api-2

我正在尝试使用基本的地图应用程序,但是,我只获得了网格屏幕。

代码:

1.ActivityMain.java

import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class MainActivity extends MapActivity {
    MapView mapView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main);
        mapView = (MapView) findViewById(R.id.map_view);       

    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}

2.activity_main.xml

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

 <com.google.android.maps.MapView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map_view"
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent"
  android:clickable="true" 
  android:enabled="true" 
  android:apiKey="[api_key]" />

</RelativeLayout>

3.Manifest.xml

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

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

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

    <permission
        android:name="com.vevolet.maps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" >
    </permission>

    <uses-permission android:name="com.vevolet.maps.permission.MAPS_RECEIVE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <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-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

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

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="[api_key]" />

        <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>
    </application>

</manifest>

我也尝试使用从应用程序生成的密钥库生成的api这个代码。但它没有用。

请告诉我问题是什么。谢谢

7 个答案:

答案 0 :(得分:1)

这是因为您混合了两个API版Google地图。一方面,您使用MapView这是一个API V1对象,另一方面您已经为API V2生成了密钥并使用了此版本的权限。

因为API V1已被弃用且不再可用,并且因为您正在实施Google Maps API V2的一半,您应该做的是:

1。删除此权限,因为它是API V1权限:

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

2。在xml中替换此代码:

 <com.google.android.maps.MapView
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/map_view"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"
 android:clickable="true" 
 android:enabled="true" 
 android:apiKey="[api_key]" />

这一个:

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

3. 从您的java代码中删除此行:

mapView = (MapView) findViewById(R.id.map_view); 

您可以查看我在Google Maps API V2上撰写的本指南,以全面了解其实施情况:

Google Maps API V2

答案 1 :(得分:1)

你的min sdk是8.你应该使用支持片段。

而不是这个

<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map_view"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:clickable="true" 
android:enabled="true" 
android:apiKey="[api_key]" />

使用以下

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

您的活动必须延长FragmentActivity。将使用SupportFragment

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

确保您已添加支持库

同时确保导入以下内容

import android.support.v4.app.FragmentActivity;  
import com.google.android.gms.maps.SupportMapFragment;    

此外,如果您已将adt更新为rev 22

右键单击项目转到属性。 Java构建路径。选择订单导出选项卡。确保选中了Android私有库。如果您已引用库项目。同样为谷歌游戏服务库项目做同样的事情。清洁和建设。

您可以按照EmilAdz的建议将其删除

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

确保您已按照以下链接中的步骤进行操作

https://developers.google.com/maps/documentation/android/

同样请确保您已在服务代码下的google api控制台中启用了Android地图。

在设备上测试。

编辑:

您应该参考地图项目中的Google服务库项目

下载Google Play服务。转到Windows。转到Android Sdk Manager。在额外内容下选择Google Play服务。如果没有安装,请安装包。

将google-play services_lib库项目复制到您的工作区。可以在以下路径中找到库项目。

<android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib library project .

将库项目导入日食。

点击文件&gt;导入,选择Android&gt;将现有的Android代码导入Workspace,并浏览工作区导入库项目。您可以检查它是否是库项目。右键单击库项目。转到属性。点击左侧面板上的Android。您将看到Is Library已选中。

参考图书馆项目

右键单击你的android地图项目。转到属性。选择Android。单击Add borwse并添加库项目。

答案 2 :(得分:1)

我觉得非常愚蠢地回答我自己的问题(特别是对它的赏金),但无论如何我都在这样做。

RaghunandanEmil Adz解决了我做的第一个错误(混合gmaps v1和v2代码),谢谢你们。 但后来又遇到了另外两个问题:

问题1:图书馆无法与我的项目建立联系 问题2:我收到错误“错误夸大了类片段”

解决方案:

对于问题1:我将google-play-services_lib文件夹复制到C:\Android\android-sdk\extras\google\google_play_services\libproject内并粘贴到另一个驱动器中,然后从新位置导入它并将其引用到我的项目中。

对于问题2:我将MapFragment替换为SupportMapFragment<fragment>(我的活动xml内),该版本适用于本机API Level 11版本碎片。

答案 3 :(得分:0)

您使用哪个证书来生成API密钥?如果从Eclipse启动,您的apk将使用调试证书进行签名,而不是您的发布证书,因此您需要为调试密钥库使用不同的API密钥。

请参阅these instructions

答案 4 :(得分:0)

看看这个答案 -

Can't make google maps v2 to work

当您的Google项目的密钥与您在清单中指定的密钥不匹配时,会发生这种情况。请再次执行所有指示步骤,并检查您的密钥。

答案 5 :(得分:0)

我遇到了同样的问题,当我使用openGL 2.0测试设备时,它对我有用。但是当我在2.2.1版本中测试时,由于缺少openGL2.0,它只显示网格屏幕。但是我使用了SupportMapFragment而不是google-maps-api-v2文档中指定的MapView。还要确保在测试之前更新Google Play服务。

答案 6 :(得分:0)

我几乎没有使用谷歌地图的不同应用程序,只有其中一个有类似于你的问题,下一个解决方案帮助了我:

  1. 转到Google API Console
  2. 打开API访问标签
  3. 选择“新建Android密钥”
  4. 在AndroidManifest.xml中使用新生成的API密钥
相关问题