Google地图未显示任何地图

时间:2012-08-21 04:04:02

标签: android google-maps

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

look

我已将项目属性更改为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;
    }
}

main.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的连接。我似乎无法找到错误...

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

  1. Android Map api key

  2. maps-api-signup

  3. 查看现有的answer。并且,这是一个最好的example,用于逐步生成map api密钥。这些可能对你有所帮助。

答案 2 :(得分:1)

您必须生成 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密钥。

答案 3 :(得分:0)

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

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

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

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

要在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"
        />