【问题标题】:Google maps shows grey tiles only not map谷歌地图只显示灰色瓷砖不显示地图
【发布时间】:2013-06-02 02:01:23
【问题描述】:

过去 4 天我一直在使用 Google Maps API v2,但无法显示 Google 地图,但只显示灰色图块。在模拟器和真实设备(作为模拟器连接到 PC)上运行应用程序时,Eclipse 不会显示任何错误。我提到的 API 密钥(代替我的 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>

ma​​in.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;
    }
} 

}

【问题讨论】:

  • 创建新的地图键..地图v2。
  • 这里你已经发布了整个 AndroidManifest.xml 文件?您似乎缺少一些内容
  • 是的,这是整个 AndroidManifest.xml 文件

标签: android google-maps


【解决方案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 密钥是否正确。

【讨论】:

    【解决方案2】:

    如果您使用签名的apk,调试密钥导出方式,地图在真实设备中看起来是空白的。您正在使用未签名的 apk,即调试密钥。如果您导出 apk,请使用发布密钥。

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

    提供发行证书指纹。

    清单中缺少一些权限。请添加。

        <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

    【讨论】:

    • 好的...谢谢...我还需要在 GPSLocatorActivity.java 文件中进行任何更改吗?
    • 不要每次都更改它。在选择哪个答案之前考虑一下,然后决定哪个对你来说是合适的。是的,一切都会改变。覆盖所有内容都被贬低并获得当前位置,有一个名为 LocationManager 的类。 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this); //也可以使用LocationManager.GPS_PROVIDER和LocationManager.PASSIVE_PROVIDER @user2439492
    【解决方案3】:

    首先您在设备上进行交叉检查。您可以在设备上添加您的 apk 文件。

    如果同样的问题仍然存在,则仅是您的 API 密钥有问题。 带有调试签名证书的 API 密钥

    生成 API 密钥的过程是 https://developers.google.com/maps/documentation/android/start#the_google_maps_api_key

    【讨论】:

      【解决方案4】:

      为了显示地图,您必须生成两个 v2 API 密钥:一个调试密钥(用于在开发期间在 Eclipse 中调试)和一个发布密钥(用于发布应用程序)。
      现在,如果您自己测试应用程序,您需要一个调试密钥。

      【讨论】:

        【解决方案5】:

        您使用的地图com.google.android.maps.MapView 是 Google Map API 的 v1。不幸的是,它已被正式弃用。

        您应该使用 Google Maps API 的新版本,即版本 2。

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

        希望这会有所帮助:-)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-08-01
          • 1970-01-01
          • 2014-06-27
          • 1970-01-01
          • 2011-02-08
          • 1970-01-01
          • 1970-01-01
          • 2011-08-11
          相关资源
          最近更新 更多