当我触摸'onBackPressed'谷歌地图v2而不是拖动

时间:2014-02-07 10:04:29

标签: android google-maps android-fragments android-maps-v2

我的地图片段存在问题。当我加载地图时,我可以拖动地图,一切正常。在我的地图中,当我点击一个标记时,它会显示一个relativeLayout,当我触摸一个relativeLayout时,它会启动一个新活动,其中包含有关该标记的更多信息。

问题在于,当我按下设备的后退按钮时,它会返回到地图但我无法拖动地图但我可以点击另一个标记,因此,再次启动活动。

我复制了我的代码,看看是否有人可以解决这个问题:

-Map Fragment:

public class DondeFragment extends ListFragment implements OnMapClickListener, OnScrollListener, 
                                                           OnCameraChangeListener, OnClickListener {

    GlobalClass global;
    public MainActivity main;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        super.onCreateView(inflater, container, savedInstanceState);

        rootView = inflater.inflate(R.layout.fragment_donde, container, false);

        if (container != null) {
            container.removeAllViews();
        }

        addMap();

        return rootView;
    }

    public void addMap() {
        // obtengo el mapa       
        global.mapa = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        // Move the camera instantly to my position with a zoom of 15.
        LatLng miPosicion = new LatLng(main.obtLatitud, main.obtLongitud);
        global.mapa.moveCamera(CameraUpdateFactory.newLatLngZoom(miPosicion , 17) );

        // Zoom in, animating the camera.
        global.mapa.animateCamera(CameraUpdateFactory.zoomTo(17), 2000, null);        
        global.mapa.setOnCameraChangeListener(this);

        global.mapa.setOnMapClickListener(this);
    }



    @Override
    public void onStart() {
        super.onStart();

        // handler to the list
        if (mWasLoading) {
            mWasLoading = false;
            mIsLoading = true;
            mHandler.postDelayed(mAddItemsRunnable, 1000);
        }
    }


    @Override
    public void onStop() {
        super.onStop();
        // handler to the list
        mHandler.removeCallbacks(mAddItemsRunnable);
        mWasLoading = mIsLoading;
        mIsLoading = false;
        ventana = ventana;

        // destroy the map
        Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
        if (f != null) {
            getActivity().getSupportFragmentManager().beginTransaction().remove(f).commitAllowingStateLoss();
        }

    }



    @Override
    public void onMapClick(LatLng arg0) {
        if (global.stateOnClickMap == 1) {
            hideSlideUp();
            String colorLocal = ((MainActivity) getActivity()).colorCategoriaBackup;
            ((MainActivity) getActivity()).markerUnselected(colorLocal);
            ((MainActivity) getActivity()).markerBackup = null;
            Log.v("EY", "You clicked the map ");
        }
    }


    public void hideSlideUp() {
        RelativeLayout slideLayout;
        slideLayout = (RelativeLayout) rootView.findViewById(R.id.sliding_up);
        if (slideLayout.getVisibility() != View.GONE) {
            slideLayout.setVisibility(View.GONE);
        }
    }
    @Override
    public void onCameraChange(CameraPosition arg0) {
        // TODO Auto-generated method stub

        ((MainActivity) getActivity()).markerBackup = null;
        hideSlideUp();
        Log.v("ONCAMERACHANGE","CHANGE: ha cambiado la camara");

        LatLng centerMap = global.mapa.getCameraPosition().target;
        Log.v("Datos Mapa","CHANGE: Centro Mapa: " + centerMap);
        VisibleRegion widthPoint = global.mapa.getProjection().getVisibleRegion();
        Log.v("Datos Mapa","CHANGE: Visible: " + widthPoint);

        LatLng topRight = widthPoint.latLngBounds.northeast;
        LatLng bottonLeft = widthPoint.latLngBounds.southwest;

        double lat1 = topRight.latitude;
        double lon1 = topRight.longitude;
        double lat2 = bottonLeft.latitude;
        double lon2 = bottonLeft.longitude;

        double R = 6378.137; // km
        double dLat = Math.toRadians(lat2-lat1);
        double dLon = Math.toRadians(lon2-lon1);

        double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                Math.sin(dLon/2) * Math.sin(dLon/2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        double d = R * c;

        Log.v("Datos Mapa","CHANGE: Distancia total: " + d);
        ((MainActivity) getActivity()).addItemsToMap(d, centerMap);
    }

}

- 来自我的MainActivity的与地图相关的方法:

// method to launch the activity detail of marker
public void goToAttract(View v) {
    int indice = -1;
    TextView tv = (TextView) v.findViewById(R.id.titulo_in_map);
    String name= (String) tv.getText();

    for (int i = 0; i < global.listaMapa.size(); i++) {
        if (global.listaMapa.get(i).nombre.equals(name)) {
            indice = i;
        }
    }

    b = new Bundle();
    Intent i = new Intent(this, DetalleLugar.class);
    setItemActivity(indice);
    i.putExtras(b);
    startActivity(i); 
}  


// information to the intent
public void setItemActivity(int position) {
    b.putInt("id", global.lista.get(position).id);
    b.putString("nombre", global.lista.get(position).nombre);
    b.putString("descripcion", global.lista.get(position).descripcion);
    b.putDouble("latitud", global.lista.get(position).latitud);
    b.putDouble("longitud", global.lista.get(position).longitud);
    b.putString("iconoPath", global.lista.get(position).icono);
    b.putString("iconoPathPng", global.lista.get(position).iconoCategoriaPng);
    b.putInt("iconoId", global.lista.get(position).iconoId);
    b.putInt("categoriaId", global.lista.get(position).categoriaId);
    b.putString("categoriaNombre", global.lista.get(position).categoriaNombre);
    b.putString("iconoCategoriaPath", global.lista.get(position).iconoCategoria);
    b.putInt("iconoCategoriaId", global.lista.get(position).iconoCategoriaId);
    b.putString("color", global.lista.get(position).color);
    b.putString("colorCategoria", global.lista.get(position).colorCategoria);
    b.putBoolean("verificado", global.lista.get(position).verificado);
    b.putBoolean("importante", global.lista.get(position).importante);
    b.putString("url", global.lista.get(position).url);
    b.putString("foursquare", global.lista.get(position).foursquare);
    b.putDouble("distance", global.lista.get(position).distance);
    //b.putStringArray(key, new String[]{value1, value2});
}

// Add items to the map with custom marker
public void addItemsToMap(double d, LatLng pos) {

    global.listaMapa.clear();
    global.readPlacesToMap(d, pos.latitude, pos.longitude, 25, 0, global.idCategoria);
    global.mapa.clear();
    global.mapa.setOnMarkerClickListener(this);

    for (int i = 0; i < global.listaMapa.size(); i++) {
        int width = 100;
        int height = 100;

        Bitmap.Config conf2 = Bitmap.Config.ARGB_8888;
        Bitmap bmp2 = Bitmap.createBitmap(width, height, conf2);

        Canvas canvas2 = new Canvas(bmp2);

        Paint contenido = new Paint();
        contenido.setColor(Color.parseColor(global.listaMapa.get(i).colorCategoria));
        contenido.setFlags(Paint.ANTI_ALIAS_FLAG);

        Paint borde = new Paint();
        borde.setColor(Color.WHITE);
        borde.setFlags(Paint.ANTI_ALIAS_FLAG);

        Paint sombra = new Paint();
        sombra.setColor(Color.argb(120, 0, 0, 0));
        sombra.setFlags(Paint.ANTI_ALIAS_FLAG);         

        canvas2.drawCircle(width/2+4, height/2+4, 23, sombra);
        canvas2.drawCircle(width/2, height/2, 25, borde);
        canvas2.drawCircle(width/2, height/2, 20, contenido);


        LatLng posItem = new LatLng(global.listaMapa.get(i).latitud,global.listaMapa.get(i).longitud);
        global.mapa.addMarker(new MarkerOptions()
            .position(posItem)
            .title(global.listaMapa.get(i).nombre)
            .icon(BitmapDescriptorFactory.fromBitmap(bmp2)));
    }


    LatLng miPosicion = new LatLng(obtLatitud, obtLongitud);
    global.mapa.addMarker(new MarkerOptions()
        .position(miPosicion)
        .title("Mi posición")
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.mi_pos)));
}




// On marker click
@Override
public boolean onMarkerClick(final Marker marker) {
    if(marker != null) {
        global.stateOnClickMap = 1;

        if (markerBackup != null) {
            Log.v("BACKUP MARKER", "marker name: " + markerBackup.getTitle());
            markerBackup.setIcon(BitmapDescriptorFactory.fromBitmap(markerUnselected(colorCategoriaBackup)));
        }

        markerBackup = marker;

        if (bmp2 == null) {
            Log.v("MARKER","bitmap es null");
        }
        else {
            Log.v("MARKER","bitmap no es null");
            bmp2.recycle();
        }

        int indice = -2;

        for (int i = 0; i < global.listaMapa.size(); i++) {
            if (global.listaMapa.get(i).nombre.equals(marker.getTitle())) {
                indice = i;
            }
        }

        int width = 100;
        int height = 100;

        Bitmap.Config conf2 = Bitmap.Config.ARGB_8888;
        bmp2 = Bitmap.createBitmap(width, height, conf2);

        canvas2 = new Canvas(bmp2);

        Paint contenido = new Paint();
        contenido.setColor(Color.parseColor(global.listaMapa.get(indice).colorCategoria));
        colorCategoriaBackup = global.listaMapa.get(indice).colorCategoria;
        contenido.setFlags(Paint.ANTI_ALIAS_FLAG);

        Paint borde = new Paint();
        borde.setColor(Color.WHITE);
        borde.setFlags(Paint.ANTI_ALIAS_FLAG);

        Paint sombra = new Paint();
        sombra.setColor(Color.argb(120, 0, 0, 0));
        sombra.setFlags(Paint.ANTI_ALIAS_FLAG);

        canvas2.drawCircle(width/2+4, height/2+4, 38, sombra);
        canvas2.drawCircle(width/2, height/2, 40, borde);
        canvas2.drawCircle(width/2, height/2, 35, contenido);

        marker.setIcon(BitmapDescriptorFactory.fromBitmap(bmp2));

        // Introduzo los valores del slide
        RelativeLayout slideLayout;
        slideLayout = (RelativeLayout) findViewById(R.id.sliding_up);
        slideLayout.setVisibility(View.VISIBLE);

        Animation slide = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);
        slideLayout.startAnimation(slide);

        TextView title;
        title = (TextView) findViewById(R.id.titulo_in_map);
        title.setText(global.listaMapa.get(indice).nombre);

        TextView description;
        description = (TextView) findViewById(R.id.descripcion_in_map);
        description.setText(checkDescription(global.listaMapa.get(indice).descripcion));

        TextView category;
        category = (TextView) findViewById(R.id.categoria_map);
        category.setText(global.listaMapa.get(indice).categoriaNombre.toUpperCase());

        TextView distanceLayout;
        distanceLayout = (TextView) findViewById(R.id.distance_map);
        double distancePosition = calculateDistance(global.listaMapa.get(indice).latitud, global.listaMapa.get(indice).longitud);
        distanceLayout.setText(getFormatDistance(distancePosition));

        LinearLayout backgroundColor = (LinearLayout) findViewById(R.id.iconMap);
        backgroundColor.setBackgroundColor(Color.parseColor(global.listaMapa.get(indice).colorCategoria));

        com.applidium.shutterbug.FetchableImageView imgIcon = 
                (com.applidium.shutterbug.FetchableImageView) findViewById(R.id.imgMap);
        imgIcon.setImage("http://192.168.1.200:9001" + global.listaMapa.get(indice).iconoCategoriaPng);

        return true;

    } else {
        RelativeLayout slideLayout;
        slideLayout = (RelativeLayout) findViewById(R.id.sliding_up);
        slideLayout.setVisibility(View.INVISIBLE);
        return false;
    }
}

0 个答案:

没有答案
相关问题