2013-02-23 5 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/)에 대해 알고 있지만, 여전히 사이트 사용 지침을 따라야합니다. –

답변

10

아시다시피 새 Google지도 API v2는 2012 년 12 월에 출시되었습니다.

따라서 Google지도를 Android에 표시하는 방법은 과거의지도 (Google Maps API v1)와 매우 다릅니다. 그러나 많은 사람들은 지금과는 다른 점을 깨닫지 않을 것입니다.

먼저 Google 서비스 라이브러리, 지원 라이브러리 및 유효한 API 키를 설정하려면이 필요합니다. 수행 방법을 모르는 경우 다음 두 문서를주의 깊게 읽으십시오 : OneTwo.

두 번째 일반 Google지도를 표시하는 코드는 이전부터 알려진 MapView (Google Maps API v1)의 코드와 다릅니다.

새로운 안드로이드 개발자에게 다음과 같이 두 번째 이슈를 스스로 응답 한 형태로 소개합니다.

1. 일반 Google 맵을 (지원) 부분에 표시하십시오.

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에 ....

참고 "클래스 ="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); 
    } 
} 

특히,지도보기의 경우, 많은 사람들이 자신의 main.xml에에서 "COM. google.android.maps .MapView을 .."설정 한 후, 그들은 "COM을 가져 오는 것이 실수. 그들의 MainActivity에서 google.android.gms.maps .MapView "를 선택하십시오. ANR (오류)이 발생합니다. 반대의 경우에도 같은 결과가 나타납니다.

따라서 항상 앱의 main.xml과 MainActivity.java에서 동일한 클래스 또는 개체를 사용하거나 가져와야하는지 확인하십시오.

+1

환상적인 답변입니다. onPause(), onDestroy() 및 onResume()이 호출되지 않으면 MapView에지도가 표시되지 않습니다. –

관련 문제