2014-03-05 36 views
1

我正在編寫一個將顯示一個簡單的谷歌地圖的機器人。一切似乎都很好。當我運行它時,它只顯示帶有Google徽標的灰色網格,但不顯示地圖。不能顯示谷歌地圖

下面是代碼

 package com.example.myroute; 

     import com.google.android.maps.MapView.LayoutParams; 
     import android.view.View; 
     import android.widget.LinearLayout; 
     import com.google.android.maps.MapActivity; 
     import com.google.android.maps.MapView; 
     import android.os.Bundle; 
     import com.google.android.maps.GeoPoint; 
     import com.google.android.maps.MapController; 




    public class MapsActivity extends MapActivity 
    {  

MapView mapView; 
MapController mc; 
GeoPoint p; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    mapView = (MapView) findViewById(R.id.mapView); 
    LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom); 
    View zoomView = mapView.getZoomControls(); 

    zoomLayout.addView(zoomView, 
     new LinearLayout.LayoutParams(
      LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT)); 
    mapView.displayZoomControls(true); 


    mc = mapView.getController(); 
    String coordinates[] = {"1.352566007", "103.78921587"}; 
    double lat = Double.parseDouble(coordinates[0]); 
    double lng = Double.parseDouble(coordinates[1]); 

    p = new GeoPoint(
     (int) (lat * 1E6), 
     (int) (lng * 1E6)); 

    mc.animateTo(p); 
    mc.setZoom(17); 
    mapView.invalidate(); 
    mapView.setStreetView(true); 
} 

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

這裏是清單文件

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

<uses-sdk 
    android:minSdkVersion="7" 
    android:targetSdkVersion="19" /> 

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<permission 
     android:name="com.example.myroute.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature"/> 
    <uses-permission android:name="com.example.myroute.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-feature 
      android:glEsVersion="0x00020000" 
      android:required="true"/> 
<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name"> 

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

    <activity android:name="com.example.myroute.MapsActivity" 
       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> 

這裏是的main.xml文件

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

<com.google.android.maps.MapView 
    android:id="@+id/mapView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:enabled="true" 
    android:apiKey="AIzaSyABftaHTCmU-ooh7XH4LMBxLLcFEj5mY_8" 
    android:clickable="true" 

    /> 
<LinearLayout android:id="@+id/zoom" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    /> 

</RelativeLayout> 
+0

您是在模擬器還是在Real設備中運行? – Kedarnath

+0

@Kedarnath我在真實的設備上運行它。 – user1555875

+0

好的,你使用調試密鑰還是創建了另一個用於創建MAP-KEY的密鑰? – Kedarnath

回答

1

在manifest文件中添加谷歌API金鑰在應用程序標籤中如下:

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

是的,我確實嘗試將其添加到清單,但它不起作用 – user1555875

2

通常,灰色網格意味着您的API密鑰存在問題。由於您使用調試證書來生成您的API密鑰,我建議您使用sdk中的Keytool創建一個新的簽名密鑰。

Original source

enter image description here

2

你有問題的原因是您正在使用的Google Maps API V1的實現可能與您生成使用API ConsoleGoogle Maps API V2一個key

我看到,因爲您使用的對象類似於GeoPoints,它們在API V2中變爲LatLng點,而在API V2中未使用的點爲MapController

由於Google Maps API V1現在不再支持一年多了,我建議您將xml佈局和java代碼更改爲Google Maps API V2代碼。

爲了讓您一開始,這裏是一個博客帖子我在你的應用程序集成Google Maps API V2寫道:

Google Maps API V2 Guide

如果您還沒有生成的密鑰以正確的方式,我建議你去在本指南以及並確保您提交的所有步驟:

Google Maps API V2 Key

0

MapViewandroid v2不像v1。所以,相反的

public class MapsActivity extends MapActivity 

ü應使用

public class MapsActivity extends Activity 

public class MapsActivity extends Fragment 

API-KEYü必須manifest聲明它裏面application tag,也提到在google_play_service版本UR使用不xml file所以從xml刪除這個android:apiKey="AIzaSyABftaHTCmU-ooh7XH4LMBxLLcFEj5mY_8"並將其添加到manifest

<meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 
<meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="your api key value" /> 

從我的角度來看它能夠更好地使用MapFragment代替MapView Tutorial for How to use Mapview和MapFragment Tutorial for How to use MapFragment

代碼片段使用MapView

public class MapFragment extends Fragment { 

    MapView m; 

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

     return v; 
    } 

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

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

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

xml文件

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

安卓的minSdkVersion = 「7」 應該是安卓的minSdkVersion = 「8」 u能 谷歌,爲什麼?通過這樣做,如果你沒有看到地圖,那麼有 API-KEY一些問題請確保你有使用我們的計算機的SHA - 1 指紋; com.example.myroute爲您的地圖產生API-KEY。如果您的 谷歌播放服務的版本不匹配你也看不到地圖。