BBonDoo
BBonDoo

Reputation: 766

How do you display Google Maps via new Google Maps API v2 on Android?

The new Google Maps API v2 has just been released, and it differs from the previous Google Maps API v1.

How do you display Google Maps using the new API?

Upvotes: 2

Views: 8482

Answers (1)

BBonDoo
BBonDoo

Reputation: 766

As you know,the new Google Maps API v2 released in Dec,2012.

Accordingly, the method to Display Google Map on Android very differs from that of the past(Google Maps API v1). But many people are not likely to realize this differences from now on.

First of all, to set the Google Service Library, the Support Library, and the valid API key is required in advance. If you don't know how to do it, please read these two documents carefully: One and Two.

Second, the code to display the ordinary Google Map also differs from that of MapView which has been known since the past(Google Maps API v1).

I introduce the second issue to the new Android developers in the self-answered form as follows;

1. Display the ordinary Google Map in (Support)Fragment.

main.xml...

Note that "class="com.google.android.gms.maps.SupportMapFragment"" is correct.

The old version used "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.... Note that all classes to be imported in the MainActivity must be like below; Please check if the imported classes have com.google.android.gms.maps.xxxxxxx type.

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. Display MapView in (Support)Fragment.

main.xml....

Note that "class="com.google.android.gms.maps.MapView"" is correct.

The old version used "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...

Note that all classes to be imported in the MainActivity must be like below; Please check if the imported classes have com.google.android.gms.maps.xxxxxxx type.

And you must add "mMapView.onCreate(savedInstanceState);" under 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);
    }
}

Specially, in case of MapView, many people make a mistake that after setting "com.google.android.maps.MapView.." in their main.xml, they import the "com.google.android.gms.maps.MapView" in their MainActivity. It causes the ANR(error). In the reverse case is the same result.

Therefore, always check if the same class or object must be used or imported in both main.xml and MainActivity.java of your app.

Upvotes: 10

Related Questions