【问题标题】:why Can't I see the map为什么我看不到地图
【发布时间】:2013-10-07 19:48:28
【问题描述】:

我想运行演示 Google 地图,但它没有显示地图。这是我的代码。我检查了 API 密钥是否正确

package com.example.googlemaps;
import com.google.android.maps.MapActivity;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends MapActivity{

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

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

} 

Manifest.xml

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

    <uses-sdk android:minSdkVersion="8"/>
    <uses-permission android:name="android.permission.INTERNET" />

    <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.androidhive.googlemaps.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>

布局xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:apiKey="@string/Api_key"
/>

【问题讨论】:

  • 我遇到了同样的问题,但我使用了SupportMapFragmenthere 就是例子。
  • @MrSuS 从过去 3 到 4 小时我一直在挣扎。我不想直接跳到 mapfragment 我想从地面学习..
  • 您是否已导入并包含 google-play-services-lib 作为您应用的库依赖项? Here's 一个很好的教程,解释了一切。
  • @ErikSoderstrom 我导入了 google-play-services-lib ,没有导入它会显示错误

标签: android google-maps-android-api-2


【解决方案1】:

这是获取地图的旧方法。谷歌现在已经更新了。 使用

<fragment
            android:id="@+id/map"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            class="com.google.android.gms.maps.SupportMapFragment" />

在您的 xml 布局中。

在清单文件中提供您的 API 密钥

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

在您的应用程序标签中。

也不要扩展MapActivity。而是扩展普通Activity

然后在您的 OnCreate 中编写以下代码

int status = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(getBaseContext());

    if (status != ConnectionResult.SUCCESS) {
        // Google Play Services are not available

        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this,
                requestCode);
        dialog.show();

    } else {
        // Google Play Services are available

        // Getting reference to the SupportMapFragment
        SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);

        // Getting Google Map
        myMap = fragment.getMap();

        // Enabling MyLocation in Google Map
        myMap.setMyLocationEnabled(true);

        // Getting LocationManager object from System Service
        // LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        // Creating a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Getting the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        // Getting Current Location From GPS
        Location location = locationManager.getLastKnownLocation(provider);

        if (location != null) {
            onLocationChanged(location);
        }

        locationManager.requestLocationUpdates(provider, 20000, 0, this);
    }

还在 OnResume 上添加以下代码

int resultCode = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(getApplicationContext());

    if (resultCode == ConnectionResult.SUCCESS) {

    } else {
        GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                RQS_GooglePlayServices);
    }

就是这样。 万事如意!

【讨论】:

    【解决方案2】:

    版本 1 googlemaps 官方已弃用。所以只需使用版本 2 并添加 google-play-services 库。肯定会有助于获取 googlemap。

    1. 公共类 MainActivity 扩展 FragmentActivity { 受保护的无效 onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

      mGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
          .getMap();    } }
      

    1. <uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="17" />
      
      <permission
          android:name="com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE"
          android:protectionLevel="signature" />
      
      <uses-feature
          android:glEsVersion="0x00020000"
          android:required="true" />
      
      <uses-permission android:name="com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE"
      

      />

      <application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" >
          <activity
              android:name="com.vogella.android.locationapi.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>
      
          <meta-data
              android:name="com.google.android.maps.v2.API_KEY"
              android:value="AIzaSyDeYchyvOUsy_I68_RMtfsI5QVkqweIp9w" />
      </application>
      


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

      1. 参考这个链接,http://www.vogella.com/articles/AndroidGoogleMaps/article.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 2018-03-05
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多