2013-02-23 99 views
2

新版Google Maps API v2剛剛發佈,與之前的Google Maps API v1不同。如何通過Android上的新Google Maps API v2顯示Google地圖?

如何使用新API顯示Google地圖?

+0

爲了挽救這個問題,我編輯了一個實際的問題。回答你自己的問題是完全可以接受的,但我們確實要求你以問答的形式這樣做。您的原始帖子不是以問題的形式出現的,這會影響可搜索性等等。我已將其重寫爲正確的格式,並因此重新打開。我們都知道[自我回應功能](http://blog.stackoverflow.com/2012/05/encyclopedia-stack-exchange/),但你仍然必須遵循網站的指導方針來使用它。 – 2013-03-01 04:00:41

回答

10

如您所知,2012年12月發佈的新版Google Maps API v2。

因此,在Android上顯示Google地圖的方法與過去的方法(Google Maps API v1)非常不同。但是很多人從現在開始不可能意識到這種差異。

首先,提前需要設置Google Service Library,Support Library和有效的API密鑰。如果您不知道如何操作,請仔細閱讀以下兩個文件:OneTwo

第二個,顯示普通Google Map的代碼也不同於自過去已知的MapView(Google Maps API v1)。

我以自答的形式向新Android開發人員介紹第二個問題,如下所示;

1.在(Support)Fragment中顯示普通Google Map。

main.xml中 ...

注意, 「類=」 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.在(支持)片段中顯示MapView。

main.xml中 ....

注意, 「類=」 融爲一體。 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類型。

And 您必須添加「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); 
    } 
} 

特別,在圖形頁面的情況下,很多人都犯了一個錯誤,在他們的main.xml中設置「融爲一體。google.android.maps .MapView。」之後,他們導入「融爲一體。google.android.gms.maps .MapView「在他們的MainActivity中。它會導致ANR(錯誤)。在相反的情況下是相同的結果。

因此,請始終檢查是否必須在應用程序的main.xml和MainActivity.java中使用或導入相同的類或對象。

+1

夢幻般的答案。我通過你的回答意識到,如果onPause(),onDestroy()和onResume()沒有被調用,那麼MapView將不會顯示地圖。 – 2015-01-17 17:33:52