2013-02-11 2 views
7

장치를 회전 할 때 SupportMapFragment의 성능을 향상시키고 자합니다. 조각을 다시 만들어야하는 것처럼 보입니다. 이것에 대해서는 잘 모르겠지만 장치가 회전 할 때지도 타일을 다시로드해야합니다. 조각을 다시 인스턴스화하지 않고도 전체 맵 조각을 유지하고 재사용 할 수있는 성능 관점에서 이해할 수 있습니다. 이것에 대한 통찰력은 인정 될 것이다.재활용 안드로이드 맵 V2 SupportMap 회전 할 때의 조각

xml에 SupportMapFragment를 선언하고 API에 설명 된대로 SetupMapIfNeeded()를 사용합니다.

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(); 
     } 
    } 
} 
+0

이 Partick 어떤 곳을 가지고 있나요? –

답변

10

샘플에서 RetainMapActivity 클래스를 확인하십시오. 매력처럼 작동합니다. 여기입니다 :

public class RetainMapActivity extends FragmentActivity { 

private GoogleMap mMap; 

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

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 

    if (savedInstanceState == null) { 
     // First incarnation of this activity. 
     mapFragment.setRetainInstance(true); 
    } else { 
     // Reincarnated activity. The obtained map is the same map instance in the previous 
     // activity life cycle. There is no need to reinitialize it. 
     mMap = mapFragment.getMap(); 
    } 
    setUpMapIfNeeded(); 
} 

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

private void setUpMapIfNeeded() { 
    if (mMap == null) { 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 
     if (mMap != null) { 
      setUpMap(); 
     } 
    } 
} 

private void setUpMap() { 
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
} 

}

+0

나는 그 예를 놓쳤다! 덕분에 – Patrick

+0

하지만 마커를 다시 그려야합니다! 그냥지도 상태 (줌, 위치 등 ..) 유지하지만 마커 polyno ... – Xenione

+0

이 솔루션은 작동하는 것 같습니다. Asynk 메서드를 사용하더라도. 그러나 7 회전 후에지도가 너무 많이 움직입니다. – user3528466

관련 문제