Android简单地图视图未显示mapv2

时间:2013-08-29 12:11:39

标签: android google-maps android-maps-v2

这是我从示例地图应用程序复制的代码,即使示例地图应用程序没有显示地图。

所以我用API密钥分别复制和粘贴代码。这次也没有显示地图。

主要活动

package com.example.map;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

    private GoogleMap mMap;

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

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

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }
}

XML文件

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.SupportMapFragment"/>

Android清单文件

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

  <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"/>
  <!-- External storage for caching. -->
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <!-- My Location -->
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <!-- Maps API needs OpenGL ES 2.0. -->
  <uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

  <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

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

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

OUT PUT:

http://i.stack.imgur.com/CzKs5.png

3 个答案:

答案 0 :(得分:0)

尝试添加这两个权限:

<permission android:name="com.example.map.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> <uses-permission android:name="com.example.map.permission.MAPS_RECEIVE"/>

并尝试在真实设备上测试此应用程序。

查看堆栈跟踪将非常有用。

答案 1 :(得分:0)

I changed the code like this

**Manifest**

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

  <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"/>
  <!-- External storage for caching. -->
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <!-- My Location -->
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


  <permission android:name="com.example.map.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> 
  <uses-permission android:name="com.example.map.permission.MAPS_RECEIVE"/>
  <!-- Maps API needs OpenGL ES 2.0. -->
  <uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

  <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

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

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





**LOGCAT**

Again the map is not showing

08-30 10:20:30.444: I/Choreographer(1166): Skipped 30 frames!  The application may be doing too much work on its main thread.

08-30 10:20:30.724: D/dalvikvm(1166): GC_FOR_ALLOC freed 302K, 11% free 3645K/4056K, paused 72ms, total 95ms

08-30 10:20:31.084: D/dalvikvm(1166): GC_FOR_ALLOC freed 154K, 13% free 3655K/4188K, paused 200ms, total 201ms

08-30 10:20:31.474: D/dalvikvm(1166): GC_FOR_ALLOC freed 286K, 16% free 3524K/4188K, paused 150ms, total 174ms

08-30 10:20:31.554: D/dalvikvm(1166): GC_FOR_ALLOC freed 154K, 16% free 3522K/4188K, paused 31ms, total 31ms

08-30 10:20:31.625: D/dalvikvm(1166): GC_FOR_ALLOC freed 152K, 16% free 3522K/4188K, paused 45ms, total 45ms

08-30 10:20:31.674: D/dalvikvm(1166): GC_FOR_ALLOC freed 172K, 16% free 3523K/4188K, paused 31ms, total 32ms

08-30 10:20:31.734: D/dalvikvm(1166): GC_FOR_ALLOC freed 152K, 16% free 3523K/4188K, paused 32ms, total 33ms

08-30 10:20:31.794: D/dalvikvm(1166): GC_FOR_ALLOC freed 152K, 16% free 3523K/4188K, paused 45ms, total 46ms

08-30 10:20:31.854: D/dalvikvm(1166): GC_FOR_ALLOC freed 152K, 16% free 3523K/4188K, paused 35ms, total 37ms




**Console**
[2013-08-30 10:20:22 - MAp] ------------------------------

[2013-08-30 10:20:22 - MAp] Android Launch!

[2013-08-30 10:20:22 - MAp] adb is running normally.

[2013-08-30 10:20:22 - MAp] Performing com.example.map.MainActivity activity launch

[2013-08-30 10:20:22 - MAp] Automatic Target Mode: Unable to detect device compatibility. 

Please select a target device.

[2013-08-30 10:20:25 - MAp] Application already deployed. No need to reinstall.

[2013-08-30 10:20:25 - MAp] Starting activity com.example.map.MainActivity on device 
emulator-5556

[2013-08-30 10:20:27 - MAp] ActivityManager: Starting: Intent { 
act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] 
cmp=com.example.map/.MainActivity }

答案 2 :(得分:0)

添加您的清单文件 - uses-permission android:name =&#34; com.example.map.permission.MAPS_RECEIVE&#34; 添加Api键,当你进入时给出: 浏览器应用程序的关键(有引用者)

点击&#34; API访问&#34; https://code.google.com/apis/console/?pli=1#project:606531687416:access

同时检查地图的所有有效服务。例如来自服务菜单的Google Maps Android API v2链接。 希望它能奏效。