簡體   English   中英

使用Google Maps API android無法顯示地圖

[英]Map not displaying with Google Maps API android

我正在我的應用上安裝Google Maps API。 我已經運行了一切,可以啟動Google地圖活動,但地圖不會顯示。 它只顯示一個可以放大和縮小的網格。

    package com.fotolife.app;

import android.os.Bundle;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class Map extends MapActivity {

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
        setContentView(R.layout.map);


    }


}

那是我的Map.java

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

    <com.google.android.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:apiKey="I Took this out for security reasons, but it works."
/>

</LinearLayout>

這是我的map.xml

謝謝您的幫助!

如果您使用的是Google Maps Android API v2,那可能就是您的項目無效的原因。 要使其正常工作,您必須執行以下步驟:

從SDK管理器下載並配置Google Play服務

Google Play服務是Android SDK附加功能的一部分。 您可以在附加部分找到它。

將google play services lib JAR添加到項目構建路徑中

lib JAR安裝在路徑中

$ANDROIDSDK_HOME/extras/google/google_play_services/libproject/google-play-services_lib/libs

使用Google API控制台獲取API密鑰

您可以通過發出命令使用操作系統控制台生成SHA1指紋以進行調試:

$ keytool -list -v -keystore ~/.android/debug.keystore -storepass android

在Google API控制台中,轉到API Access,然后在右側窗格中選擇“創建新的Android密鑰...”。 然后將SHA1指紋放在文本框中,后跟分號和項目的包名稱。

在項目的清單文件中指定權限設置

將以下清單標記放在應用程序清單文件中(將com.example替換為項目的包名稱):

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

  <!-- Maps API needs OpenGL ES 2.0. -->
  <uses-feature android:glEsVersion="0x00020000" android:required="true"/>

在項目的清單文件中指定從API控制台生成的API密鑰

在關閉應用程序標記之前將其作為最后一個元素

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="your api key goes here"/>

將地圖添加到項目中

main.xml中

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

Main.java

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

public class Main extends Activity {

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

運行您的應用程序

你應該把一切都搞定。

嗨這對我有用:

還記得你的api-key的位置嗎? https://code.google.com/apis/console ,去那里和Key for Android應用程序(帶證書)下按編輯允許的Android應用程序。

在我的情況下,指紋指的是一個不同項目中的舊包,所以我只是去了cmd控制台並得到了我的新指紋,然后輸入了我當前的包名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM