2013-06-06 112 views
1

過去4天我一直在使用Google Maps API v2,但顯示Google地圖失敗,但只顯示灰色地圖。日食不顯示在運行在模擬器和真實設備的應用程序的任何錯誤(在我的地方API密鑰的)(連接到PC的模擬器),我曾提到.The API密鑰是我通過顯示調試一個證書指紋。任何人都可以。幫幫我!提前致謝。谷歌地圖只顯示灰色地磚而不顯示地圖

清單文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.VertexVerveInc.GPSLocator" 
android:versionCode="1" 
android:versionName="1.0" > 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<uses-permission android:name="android.permission.INTERNET"/> 

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

<application 
    android:allowBackup="true" 
    android:icon="@drawable/icon" 
    android:label="@string/app_name"> 
    <uses-library android:name="com.google.android.maps"/> 
    <activity 
     android:name="GPSLocatorActivity" 
     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文件

<LinearLayout 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 
android:id="@+id/mapView" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:enabled="true" 
android:clickable="true" 
android:apiKey="my api key" 
/> 
</LinearLayout> 

GPSLocatorActivity.java文件

package com.VertexVerveInc.GPSLocator; 


import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapView; 
import com.google.android.maps.MapController; 
import com.google.android.maps.GeoPoint; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 

import android.location.Geocoder; 
import android.location.Address; 

import com.google.android.maps.Overlay; 
import android.graphics.Canvas; 
import android.graphics.Point; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 

import java.util.List; 
import java.util.Locale; 
import java.io.IOException; 

import android.os.Bundle; 
import android.widget.Toast; 

public class GPSLocatorActivity extends MapActivity { 

private MapView mapView; 
private MapController mapController; 

private LocationManager locationManager; 
private LocationListener locationListener; 

@SuppressWarnings("deprecation") 

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

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  

    locationListener = new GPSLocationListener(); 

    locationManager.requestLocationUpdates(
     LocationManager.GPS_PROVIDER, 
     0, 
     0, 
     locationListener); 

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

    // enable Street view by default 
    mapView.setStreetView(true); 

    // enable to show Satellite view 
    mapView.setSatellite(true); 

    // enable to show Traffic on map 
    mapView.setTraffic(true); 
    mapView.setBuiltInZoomControls(true); 

    mapController = mapView.getController(); 
    mapController.setZoom(16); 
} 

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

private class GPSLocationListener implements LocationListener 
{ 
    @Override 
    public void onLocationChanged(Location location) { 
     if (location != null) { 
      GeoPoint point = new GeoPoint(
        (int) (location.getLatitude() * 1E6), 
        (int) (location.getLongitude() * 1E6)); 

      /* Toast.makeText(getBaseContext(), 
        "Latitude: " + location.getLatitude() + 
        " Longitude: " + location.getLongitude(), 
        Toast.LENGTH_SHORT).show();*/ 

      mapController.animateTo(point); 
      mapController.setZoom(16); 

      // add marker 
      MapOverlay mapOverlay = new MapOverlay(); 
      mapOverlay.setPointToDraw(point); 
      List<Overlay> listOfOverlays = mapView.getOverlays(); 
      listOfOverlays.clear(); 
      listOfOverlays.add(mapOverlay); 

      String address = ConvertPointToLocation(point); 
      Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show(); 

      mapView.invalidate(); 
     } 
    } 

    public String ConvertPointToLocation(GeoPoint point) { 
     String address = ""; 
     Geocoder geoCoder = new Geocoder(
       getBaseContext(), Locale.getDefault()); 
     try { 
      List<Address> addresses = geoCoder.getFromLocation(
       point.getLatitudeE6()/1E6, 
       point.getLongitudeE6()/1E6, 1); 

      if (addresses.size() > 0) { 
      for (int index = 0; index <         addresses.get(0).getMaxAddressLineIndex(); index++) 
        address += addresses.get(0).getAddressLine(index) + " "; 
      } 
     } 
     catch (IOException e) {     
      e.printStackTrace(); 
     } 

     return address; 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 
} 

class MapOverlay extends Overlay 
{ 
    private GeoPoint pointToDraw; 

    public void setPointToDraw(GeoPoint point) { 
     pointToDraw = point; 
    } 

    public GeoPoint getPointToDraw() { 
     return pointToDraw; 
    } 

    @Override 
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { 
     super.draw(canvas, mapView, shadow);     

     // convert point to pixels 
     Point screenPts = new Point(); 
     mapView.getProjection().toPixels(pointToDraw, screenPts); 

     // add marker 
     Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.red); 
     canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null); // 24 is the  height of image   
     return true; 
    } 
} 

}

+0

創建新的地圖鍵..地圖v2。 – NaserShaikh

+0

在這裏你已經發布了整個AndroidManifest.xml文件?好像你缺少一些內容 –

+0

是的,這是整個AndroidManifest.xml文件 – user2439492

回答

4

如果您使用的APK簽署,調試的主要出口方式,地圖看起來真實設備的空白。您正在使用未簽名的apk,即調試密鑰。如果您導出apk,請使用釋放鍵。

https://developers.google.com/maps/documentation/android/start#the_google_maps_api_key

發佈證書指紋中給出。

而且有些權限缺少manifest.please補充說。

<uses-feature 
    android:glEsVersion="0x00020000" 
    android:required="true" /> 
    <permission 
    android:name="com.VertexVerveInc.GPSLocator.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature" /> 

<uses-permission android:name="com.VertexVerveInc.GPSLocator.permission.MAPS_RECEIVE" /> 

而在佈局中,您使用的是地圖v1,切換到地圖v2。

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

http://www.vogella.com/articles/AndroidGoogleMaps/article.html

+0

好吧......謝謝...我需要在** GPSLocatorActivity.java **文件中進行任何更改嗎? – user2439492

+0

不要每次都改變它。在選擇答案之前選擇它,然後決定哪一個適合你。並且是的,所有東西都會變化。覆蓋所有取得的東西並獲取當前位置,還有一個叫做LocationManager的類。 locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE); \t locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME,MIN_DISTANCE,this); //你也可以使用LocationManager.GPS_PROVIDER和LocationManager.PASSIVE_PROVIDER @ user2439492 – Shadow

1

使用如下權限:

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

檢查您的API密鑰的正確與否。

1

爲了證明你有生成兩個版API密鑰圖:在一個調試鍵(用於調試在Eclipse開發)和一個發佈密鑰(用於發佈應用程序)。
現在,如果您自己測試應用程序,則需要調試密鑰。

1

您使用的地圖com.google.android.maps.MapView是Google Map API的v1版本。不幸的是,它被正式棄用。

您應該使用版本2的Google Maps API的新版本。

Here這是如何使用Google Maps API v2的好例子。

希望這有助於:-)