Pregunta

Estoy usando la clase de compatibilidad de Android con el hack para usar MAPViews en un fragmento que se encuentra aquí: https://github.com/petedoyle/android-support-v4-googlemaps

Desafortunadamente, lo que estoy encontrando es que si el mapfragment se elimina de la actividad, y luego se le reí, obtengo el "solo se le permite tener una sola vista de mapview en un" error "de mapactivity.

Entiendo el principio detrás del error e intenté destruir el MAPVIEW en el método Fragments OnPause. Desafortunadamente, parece que no puedo destruir el mapview por completo, ya que todavía lo estoy recibiendo. Mi código se ve así:

private RelativeLayout layout; 
private MapView mp;

public void onResume(){
    super.onResume();
    Bundle args = getArguments();
    if(mp == null)
    {
        mp = new MapView(getActivity(), this.getString(R.string.map_api_key)); 
        mp.setClickable(true);
    }

    String request = args.getString("requestId");
    layout = (RelativeLayout) getView().findViewById(R.id.mapholder);
    layout.addView(mp);
    //TextView txt = (TextView) getView().findViewById(R.id.arguments);
    //txt.setText(request);
}

public void onPause(){
    super.onPause();
    layout.removeView(mp);
    mp = null;
}

¿Alguien tiene alguna idea sobre la referencia que estoy descuidando para destruir aquí?

¿Fue útil?

Solución

Encontré el mismo problema. Así es como lo resolví:

  • Como debería ser solo una instancia de MAPView en la actividad, la inicializo en el método de OnCreate en la actividad:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // initialize MapView programmatically to be used in Fragment :
        this.mActivityMapView = new MapView(MainActivity.this, getString(R.string.debug_mapview_apikey));
    
        setContentView(R.layout.activity_main);
    }
    
  • Luego lo recupero en el método de fragmento onCreateView:

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.mMapView = ((MainActivity) getActivity()).getMapView();
        return this.mMapView;
    }
    
  • Y lo destruyo en el método de fragmento en el inestador:

    public void onDestroy() {
        NoSaveStateFrameLayout parentView = (NoSaveStateFrameLayout) this.mMapView.getParent();
        parentView.removeView(this.mMapView);
        super.onDestroy();
    }
    

Otros consejos

Es posible que desee eliminar la vista de mapa en el método OnPause (en lugar del método OnDestroy)

public void onPause() {
    NoSaveStateFrameLayout parentView = (NoSaveStateFrameLayout) this.mMapView.getParent();
    parentView.removeView(this.mMapView);
    super.onPause();}

De esta manera, puede agregar un mapfragment al backstack. (FragmentTransaction.Addtobackstack evita que se destruya un fragmento)

Las respuestas de Max y Bleeker funcionan bien, pero debe tener especial cuidado si desea apoyar los cambios en la orientación de la pantalla, como yo. Resulta que cuando gira la pantalla, volverá a llamar a OnCreateView sin llamar al método OnPause, agregando así MAPVIEW dos veces. Para arreglarlo, dentro de OnCreateview hice lo siguiente:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    this.mMapView = ((MyGameActivity) getActivity()).getMapView();
    if (this.mMapView.getParent()!=null)
    {
        NoSaveStateFrameLayout parentView = (NoSaveStateFrameLayout) this.mMapView.getParent();
        parentView.removeView(this.mMapView);
    }
    return this.mMapView;
}

Traté de obtener una solución simple a este problema. En la clase que maneja los detalles de mi vista (una extensión de relativeLiveAyout), declaré un private static MapView myMapView;.

Luego, anule el método OnFinishInflate () como el siguiente fragmento de código:

    @Override
protected void onFinishInflate() {  
    super.onFinishInflate();

    //myMapView = (MapView) findViewById(R.id.map_view);
    if (myMapView==null) 
        myMapView = new MapView(context, getResources().getString(R.string.map_view_api_key));  
    if (myMapView.getParent() != null)
        ((ViewGroup)myMapView.getParent()).removeView(myMapView);

    myMapView.setClickable(true);
    myMapView.setEnabled(true);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    myMapView.setLayoutParams(lp);          
    this.addView(myMapView);

}

De esta manera solo tiene una instancia de MAPVIEW.

Así es como resolví este problema en caso de fragmento.

Crea el diseño

 <LinearLayout
        android:id="@+id/ll_mapcontainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" />

Puede colocar esto como un diseño en cualquier lugar de su archivo XML donde desea mostrar el mapa

Ahora el código será así.

package com.yuviii.sample;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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 com.yuviii.sample.R;

/**
 * Created by yubraj Poudel on 12/20/15.
 */
public class ContactUsFragment extends Fragment  {
    static final LatLng PRACTICAL_ANSWER_LOCATION = new LatLng(53.558, 9.927);
    private SupportMapFragment fragment;
    private GoogleMap map;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View v = inflater.inflate(R.layout.page_contactus, container, false);
        return v;
    }



    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
                    initializeMap();
    }

    private void initializeMap() {

        FragmentManager fm = getChildFragmentManager();
        fragment = (SupportMapFragment) fm.findFragmentById(R.id.ll_mapcontainer);
        if (fragment == null) {
            fragment = SupportMapFragment.newInstance();
            fm.beginTransaction().replace(R.id.ll_mapcontainer, fragment).commit();
        }

    }
    @Override
    public void onResume() {
        super.onResume();
        if (map == null) {
            map = fragment.getMap();
            map.addMarker(new MarkerOptions().position(PRACTICAL_ANSWER_LOCATION));
        }
    }   
}

Esto funciona para mí. Disfrutar !!!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top