2014-04-09 30 views
0

我想开发一个显示谷歌地图的Android应用程序。我遵循以下链接开发。
http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/

以下是我的源代码。

activity_main.xml中
-------------------------------------

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

<?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(); 
} 

} 



AndroidM anifest.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



我不知道为什么地图上没有显示的屏幕截图。

+0

@HemantChandDungriyal他显然是在标题和问题说,他是 – tyczj

+0

我只是试图在emulaor运行 – Wanderer

+0

试穿真实的设备 – duggu

回答

相关问题