【问题标题】:Google maps not showing any map [duplicate]谷歌地图没有显示任何地图[重复]
【发布时间】:2012-08-21 04:04:02
【问题描述】:

我不知道我的问题是什么,谷歌地图没有显示

我已将项目属性更改为 Google API 2.3.3,并且没有显示任何错误

这是我的代码:

Mapping.java

package com.mapping;

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

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

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class Mapping extends MapActivity {

    private MapView mapView = null;
    private Geocoder geoCoder = null;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);

        // latitude and longitude of Dallas, TX
        // set as starting point 
        int lat = (int)(37.422006 * 1000000); //the geocoder requires integers...
        int lon = (int)(-122.084095 * 1000000);
        //make these into a GeoPoint:
        GeoPoint startPoint = new GeoPoint(lat, lon);
        mapView.getController().setZoom(12);
        mapView.getController().setCenter(startPoint);

        geoCoder = new Geocoder(this);
    }

    public void mapHandler(View v) {
        switch(v.getId()) {
        case R.id.btnSat:
            mapView.setSatellite(true);
            break;
        case R.id.btnTraf:
            mapView.setTraffic(true);
            break;
        case R.id.btnNorm:
            mapView.setSatellite(false);
            mapView.setTraffic(false);
            break;          
        }
    }

    public void geocode(View v) {
        EditText geoLocation = (EditText) findViewById(R.id.txtLocation);
        if(Geocoder.isPresent()) {
            try {
                String addr = geoLocation.getText().toString();

                List<Address> locationList = geoCoder.getFromLocationName(addr, 5);
                if(locationList != null && locationList.size() > 0) {
                    int lat = (int)(locationList.get(0).getLatitude() * 1000000);
                    int lon = (int)(locationList.get(0).getLongitude() * 1000000);

                    GeoPoint setPoint = new GeoPoint(lat, lon);
                    mapView.getController().setZoom(14);
                    mapView.getController().setCenter(setPoint);
                }
            } catch (IOException error) {
                Log.i("Caught IOException", "-----Printing Stack Trace-----");
                error.printStackTrace();
            }
        } else {
            geoLocation.setText("No Geocoder Available");
        }
    }

    protected boolean isLocationDisplayed() {
        return false;
    }

    protected boolean isRouteDisplayed() {
        return false;
    }
}

ma​​in.xml

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnSat" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Satellite"
            android:onClick="mapHandler" />

        <Button 
            android:id="@+id/btnTraf"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Traffic"
            android:onClick="mapHandler" />

        <Button 
            android:id="@+id/btnNorm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Normal"
            android:onClick="mapHandler" />

    </LinearLayout>

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/txtLocation"
            android:layout_width="200sp"
            android:layout_height="wrap_content"
            android:text="Dallas" />

        <Button 
            android:id="@+id/btnGeocode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Find Location"
            android:onClick="geocode" />

    </LinearLayout>

    <com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:apiKey="0G_pKeFNWX5lw7PQ7AzKnl2XbRs7bHZ3p6ECosQ" />
</LinearLayout>

AndroidManifest.xml

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

    <uses-sdk android:minSdkVersion="10" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <activity
            android:label="@string/app_name"
            android:name=".Mapping" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <uses-library android:name="com.google.android.maps" />
    </application>

</manifest>

谁能帮帮我?我整天都在拉头发。该程序运行正常,如您所见,我可以截屏,因此它必须是设备与 Google API 的连接。我似乎找不到错误...

【问题讨论】:

  • 您是否在清单中添加了所有必需的权限和库?你有有效的钥匙吗?
  • 是的,先生,我相信...等等,我会出示我的清单
  • 您使用的是自己的地图 api 密钥吗?检查一次。
  • 您的清单看起来不错.. 您的 apiKey 怎么样?这可能会导致这样的问题..顺便说一句,日志有什么问题吗?

标签: android google-maps


【解决方案1】:

不要使用现有的地图 api 密钥或其他任何东西。您必须使用您的 md5 指纹代码生成您自己的地图 API 密钥。看看下面的链接 -

  1. Android Map api key

  2. maps-api-signup

查看现有的answer。而且,这是一个最好的example,用于逐步生成地图 api 密钥。这些肯定对你有帮助。

【讨论】:

  • 我做了所有的指令,并且能够获得指纹,但它不是 MD5 而是 SHA1
【解决方案2】:

您必须生成自己的 apiKey。如果您还没有完成,请点击此链接Obtaining a Google Maps Android API Key

【讨论】:

    【解决方案3】:

    您必须生成 MD5 key 才能注册 Google Key
    要从您的 PC 生成 MD5 Key,步骤如下:


    Open the command prompt and follow the steps
    
    C:\Program Files\Java\<JDK_version_number>\bin>keytool -genkey -v -keystore projectkey.keystore   
                       -alias aliasname -keyalg RSA -keysize 2048 -validity 15000    
    
      //The Above path should be set Accordingly to your Machine
    
    Enter keystore password: ------------
    What is your first and last name?
    [Unknown]: ------------
    What is the name of your organizational unit?
    [Unknown]: ------------
    What is the name of your organization?
    [Unknown]: ------------
    What is the name of your City or Locality?
    [Unknown]: ------------
    What is the name of your State or Province?
    [Unknown]: ------------
    What is the two-letter country code for this unit?
    [Unknown]: ------------
    
    D:\android-sdk-windows-1.6_r1\tools>keytool -v -list -alias aliasname -keystore projectkey.keystore
    Enter keystore password:
    aliasname, Dec 7, 2010, PrivateKeyEntry,
    Certificate fingerprint (MD5): CA:CF:AA:0E:5A:2B:88:C8:64:F1:FA:F7:29:21:50:FF  
    

    现在,前往 Here 并使用该 MD5 密钥注册您的 Google API 密钥。

    【讨论】:

      【解决方案4】:

      只需在 cmd 提示符下写下这些行以提取 MD5 指纹。

      keytool.exe -list -alias androiddebugkey -keystore "C:\android\debug.keystore" -storepass android -keypass android

      获取 MD5 指纹后 复制 MD5 证书指纹并导航您的网络浏览器至:http://code.google.com/android/maps-api-signup.html。按照页面上的说明完成申请并获取 Google Maps 密钥。

      要在您的 Android 应用程序中使用 Google 地图,您需要修改您的 AndroidManifest.xml 文件,将元素与 INTERNET 权限一起添加:

      要在您的 Android 应用程序中显示 Google 地图,请修改位于 res/layout 文件夹中的 main.xml 文件。您应使用该元素在您的活动中显示 Google 地图。另外,让我们使用元素来定位活动中的地图:

      例如:

      <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="0l4sCTTyRmXTNo7k8DREHvEaLar2UmHGwnhZVHQ"
              />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-20
        • 1970-01-01
        • 2014-12-27
        • 1970-01-01
        • 2020-04-03
        相关资源
        最近更新 更多