谷歌地图未在Android中显示

时间:2013-09-18 02:49:36

标签: android google-maps

我在我的示例android项目中使用谷歌地图,但它只是显示标题没有其他没有地图显示,我已经按照lynda教程,地图键也使用谷歌地图模拟器生成并测试真实的欺骗,他们仍然不起作用。以下是代码。请帮助

布局文件

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

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

    <com.google.android.maps.MapView
        android:id="@+id/map"
        android:clickable="true"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:apiKey="MyMApKey" />

</RelativeLayout>

地图活动:

package com.example.maptest;

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

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;

public class MainActivity extends MapActivity {
      LocationListener listener ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MapView view = (MapView) findViewById(R.id.map);
        view.setBuiltInZoomControls(true);
        final MapController control = view.getController();
        final LocationManager manager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);
          listener = new LocationListener() {

            @Override
            public void onStatusChanged(String provider, int status,
                    Bundle extras) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub

                control.setCenter(new GeoPoint((int)location.getLatitude(), (int)location.getLongitude()));
                manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0,listener);

            }
        };

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

}

清单:

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

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="com.google.android.maps" />

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

这是输出enter image description here

4 个答案:

答案 0 :(得分:1)

注意:截至2012年12月3日,Google Maps Android API版本1已正式弃用。这意味着从2013年3月18日起,您将无法再为此版本请求API密钥。 Google Maps Android API v1不会添加任何新功能。但是,使用v1的应用程序将继续在设备上运行。鼓励现有和新开发人员使用

转到此链接google map api v2 给出了逐步描述。 https://developers.google.com/maps/documentation/android/start#the_google_maps_api_key

答案 1 :(得分:0)

您的Android清单中没有定义Api Key。没有它你的地图不会加载。其次,您没有在Android清单中定义所有权限。 OpenGl,这也是必须的。 请参阅此代码。这会帮你.. :) 我的主要活动:

公共类MainActivity扩展了FragmentActivity {

private static final int GPS_ERRORDIALOG_REQUEST = 9001;
Button btn1;
EditText et1;
EditText et2;

GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (servicesOK()) {
        Toast.makeText(this, "Ready to map!!", Toast.LENGTH_LONG).show();
        setContentView(R.layout.testmap);

    } else {
        setContentView(R.layout.activitymain);

    }

    /*
     * et1 = (EditText)findViewById(R.id.edittext1); et2 =
     * (EditText)findViewById(R.id.edittext2); btn1 =
     * (Button)findViewById(R.id.button1);
     * 
     * btn1.setOnClickListener(this);
     */

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

/*
 * @Override public void onClick(View v) {
 * 
 * Object lat = et1; Object lon = et2;
 * 
 * Uri uri=Uri.parse("geo:"+lat.toString()+","+lon.toString()); Intent i=new
 * Intent(Intent.ACTION_VIEW, uri); startActivity(i); Log.d("The Location:",
 * lat.toString()+lon.toString());
 * 
 * 
 * }
 */
public boolean servicesOK() {

    int isAvailable = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(this);

    if (isAvailable == ConnectionResult.SUCCESS) {

        return true;

    } else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {

        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
                this, GPS_ERRORDIALOG_REQUEST);
        dialog.show();

    } else {

        Toast.makeText(this, "Cant connect!!", Toast.LENGTH_SHORT).show();

    }
    return false;
}

}

我的布局:

xmlns:maps="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

最后是Android Manifest。这是您必须考虑的最重要的想法:

package="com.mike.maps"
android:versionCode="1"
android:versionName="1.0" >

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

<permission
    android:name="com.mike.maps.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.mike.maps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" >
</uses-feature>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.mike.maps.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.mike.maps.SecondActivity" >
    </activity>

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="Your API KEY goes here" >
    </meta-data>
</application>

答案 2 :(得分:0)

Google map api v1已被正式弃用,您无法为api v1请求新的api密钥。只有较旧的api密钥才能与已创建的google map api v1配合使用。

请尝试实施Google Map API V2。

答案 3 :(得分:0)