在Android应用程序中使用谷歌地图不在模拟器中显示地图

时间:2014-04-09 14:07:56

标签: android google-maps

我正在尝试开发一个显示谷歌地图的Android应用程序。我按照以下链接进行开发。
http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/

以下是我的源代码。 />
activity_main.xml中
-------------------------------------

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

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

</RelativeLayout>



MainActivity.java
-------------------------------------

package com.example.mymap;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

private GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
        // Loading map
        initilizeMap();

    } catch (Exception e) {
        e.printStackTrace();
    }       
}

@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;
}

@SuppressLint("NewApi")
private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap();

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

@Override
protected void onResume() {
    super.onResume();
    initilizeMap();
}

}



AndroidManifest.xml中
-------------------------------------

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

<permission android:name="com.example.mymap.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.mymap.permission.MAPS_RECEIVE" />

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

<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-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<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" >
    <activity
        android:name="com.example.mymap.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>

    <meta-data android:name="com.google.android.gms.version" 
        android:value="@integer/google_play_services_version" />

   <meta-data
 android:name="com.google.android.maps.v2.API_KEY"
 android:value="AIzaSyCSL4M7HtfrdODC5GdfXEiPRgd6s4CohH0" /> 

</application>

</manifest>



有关模拟器的屏幕截图,请参阅下图

enter image description here



我不知道为什么地图没有显示。

1 个答案:

答案 0 :(得分:1)

尝试在支持Google地图的Genymotion模拟器上运行: https://cloud.genymotion.com/page/launchpad/download/

您必须添加额外的.jar才能在Genymotion中安装Google Play服务,您将在此处获取相关信息:

How do you install Google frameworks (Play, Accounts, etc.) on a Genymotion virtual device?