2

新しい Google Maps API v2 がリリースされたばかりで、以前の Google Maps API v1 とは異なります。

新しい API を使用して Google マップを表示するにはどうすればよいですか?

4

1 に答える 1

10

ご存じのとおり、2012 年 12 月にリリースされた新しい Google Maps API v2。

そのため、Android で Google マップを表示する方法は、従来の方法 (Google Maps API v1) とは大きく異なります。しかし、多くの人はこれからこの違いに気付きません。

まず、Google サービス ライブラリ、サポート ライブラリ、および有効な API キーを事前に設定する必要があります。方法がわからない場合は、次の 2 つのドキュメントをよく読んでください: OneTwo

第二に、通常の Google マップを表示するためのコードも、以前から知られている MapView のコード (Google Maps API v1) とは異なります。

次のように、自己回答形式で新しい Android 開発者に 2 番目の問題を紹介します。

1. (Support)Fragment で通常の Google マップを表示します。

main.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. (Support)Fragment で MapView を表示します。

main.xml ....

「class="com. google.android.gms .maps. MapView「」が正解です。

古いバージョンでは「class="com. google.android .maps. マップビュー".

<?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の場合、 「com.google.android.maps.MapView..」をmain.xmlに設定した後、「 com.google.android.gms.maps.MapView」をインポートするというミスをする人が多いです。 MainActivity で。ANR(エラー)の原因となります。逆の場合も同じ結果です。

したがって、アプリの main.xml と MainActivity.java の両方で同じクラスまたはオブジェクトを使用またはインポートする必要があるかどうかを常に確認してください。

于 2013-02-23T06:23:50.130 に答える