【问题标题】:How do you display Google Maps via new Google Maps API v2 on Android?如何在 Android 上通过新的 Google Maps API v2 显示 Google Maps?
【发布时间】:2013-02-23 06:23:50
【问题描述】:

新版 Google Maps API v2 刚刚发布,与之前的 Google Maps API v1 有所不同。

如何使用新 API 显示 Google 地图?

【问题讨论】:

  • 为了挽救这个问题,我将其编辑为一个实际问题。回答您自己的问题是完全可以接受的,但我们确实要求您以问答的形式回答。您的原始帖子不是一个问题,这会影响可搜索性等。我已将其重写为正确的格式并因此重新打开。我们都知道the self-answer functionality,但您仍然必须遵循该网站的指南才能使用它。

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


【解决方案1】:

如您所知,新的 Google Maps API v2 于 2012 年 12 月发布。

因此,在 Android 上显示 Google Map 的方法与过去(Google Maps API v1)有很大不同。但从现在开始,很多人不太可能意识到这种差异。

首先,需要提前设置好谷歌服务库、支持库和有效的API密钥。如果你不知道怎么做,请仔细阅读这两个文件:OneTwo

第二,显示普通谷歌地图的代码也不同于过去已知的MapView(谷歌地图API v1)。

我将第二期以自答形式介绍给新晋Android开发者如下;

1.在 (Support)Fragment 中显示普通的谷歌地图。

ma​​in.xml...

注意“class="com.google.android.gms.maps.SupportMapFragment"”是正确的。

旧版本使用“class="com.google.android.maps.SupportMapFragment"”

<?xml version="1.0" encoding="utf-8"?>
<!-- This can go anywhere in your layout (see other demos for some examples). -->
<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"/>

MainActivity.java.... 请注意,要在 MainActivity 中导入的所有类必须如下所示;请检查导入的类是否有 com.google.android.gms.maps.xxxxxxx 类型。

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;

/**
 * This shows how to create a simple activity with a map and a marker on the map.
 * <p>
 * Notice how we deal with the possibility that the Google Play services APK is not
 * installed/enabled/updated on a user's device.
 */
public class BasicMapActivity extends android.support.v4.app.FragmentActivity {
    /**
     * Note that this may be null if the Google Play services APK is not available.
     */
    private GoogleMap mMap;

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

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

    /**
     * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
     * installed) and the map has not already been instantiated.. This will ensure that we only ever
     * call {@link #setUpMap()} once when {@link #mMap} is not null.
     * <p>
     * If it isn't installed {@link SupportMapFragment} (and
     * {@link com.google.android.gms.maps.MapView
     * MapView}) will show a prompt for the user to install/update the Google Play services APK on
     * their device.
     * <p>
     * A user can return to this Activity after following the prompt and correctly
     * installing/updating/enabling the Google Play services. Since the Activity may not have been
     * completely destroyed during this process (it is likely that it would only be stopped or
     * paused), {@link #onCreate(Bundle)} may not be called again so we should call this method in
     * {@link #onResume()} to guarantee that it will be called.
     */
    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();
            }
        }
    }

    /**
     * This is where we can add markers or lines, add listeners or move the camera. In this case, we
     * just add a marker near Africa.
     * <p>
     * This should only be called once and when we are sure that {@link #mMap} is not null.
     */
    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }
}

2。在(支持)Fragment 中显示 MapView。

ma​​in.xml....

注意“class="com.google.android.gms.maps.MapView"”是正确的。

旧版本使用“class="com.google.android.maps.MapView”。

<?xml version="1.0" encoding="utf-8"?>
<!-- This can go anywhere in your layout. -->
<com.google.android.gms.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

MainActivity.java...

请注意,所有要在 MainActivity 中导入的类必须如下所示;请检查导入的类是否有 com.google.android.gms.maps.xxxxxxx 类型。

并且您必须添加“mMapView.onCreate(savedInstanceState);”在 OnCreate() 下

import android.os.Bundle;
import com.google.android.gms.maps.MapView;

/**
 * This shows how to create a simple activity with a raw MapView and add a marker to it. This
 * requires forwarding all the important lifecycle methods onto MapView.
 */
public class RawMapViewDemoActivity extends android.support.v4.app.FragmentActivity {
    private MapView mMapView;

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

        mMapView = (MapView) findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState);

    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();

    }

    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
    }
    @Override
    protected void onDestroy() {
        mMapView.onDestroy();
        super.onDestroy();
    }
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }
}

特别是在 MapView 的情况下,很多人犯了一个错误,在他们的 main.xml 中设置“com.google.android.maps.MapView..”后,他们导入了“com. google.android.gms.maps.MapView”在他们的 MainActivity 中。它会导致 ANR(错误)。在相反的情况下是相同的结果。

因此,请始终检查是否必须在应用的 main.xml 和 MainActivity.java 中使用或导入相同的类或对象。

【讨论】:

  • 很棒的答案。我通过您的回答意识到,如果未调用 onPause()、onDestroy() 和 onResume(),则 MapView 将不会显示地图。
猜你喜欢
  • 2013-09-26
  • 2019-01-06
  • 2013-02-15
  • 1970-01-01
  • 1970-01-01
  • 2013-03-24
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多