在Fragment中未检测到拖动标记事件

时间:2015-05-06 15:53:50

标签: android google-maps marker

我遇到了OnDragClickListener事件的问题。我有一个活动调用了一个片段,我加载了Google地图的地图。

MainActivity.java

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;


public class MainActivity extends FragmentActivity {

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

        MainActivityFragment fragment = (MainActivityFragment)getFragmentManager().findFragmentById(R.id.map_location);

        fragment.setLng(2.136342);
        fragment.setLat(41.393751);
        fragment.updateCameraLocation(null);
    }

}

MainActivityFragment.java

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;

import java.util.ArrayList;


/**
 * A placeholder fragment containing a simple view.
 */
public class MainActivityFragment extends Fragment{
    private final static String TAG = "MainActivityFragment";
    private final static int INITIAL_ZOOM = 17;

    private MapView mapView;
    private GoogleMap map;
    private ArrayList<Marker> markers = new ArrayList<Marker>();

    private double lat;
    private double lng;

    public MainActivityFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Log.d(TAG, "Latitud: " + Double.toString(this.getLat()) + " Longitud: " + Double.toString(this.getLng()));

    }

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

          // Gets the MapView from the XML layout and creates it
        mapView = (MapView) v.findViewById(R.id.mapview);
        mapView.onCreate(savedInstanceState);

        // Gets to GoogleMap from the MapView and does initialization stuff
        map = mapView.getMap();
        map.getUiSettings().setMyLocationButtonEnabled(false);
        map.setMyLocationEnabled(true);

        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        map.getUiSettings().setZoomControlsEnabled(true);
        map.getUiSettings().setMapToolbarEnabled(true);
        map.setTrafficEnabled(true);

        map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                                      @Override
                                      public void onMapClick(LatLng latLng) {
                                          Log.d(TAG, "Latitud: " + Double.toString(latLng.latitude) + " Longitud: " + Double.toString(latLng.longitude));
                                          String title = "Marcador nuevo!!!";
                                          String snippet = "Más snippet!!!";

                                          createMarker(new LatLng(latLng.latitude, latLng.longitude), title, snippet);

                                          Log.d(TAG, "size después de add: " + markers.size() + " marker idMarker 0: " + markers.get(0).getId());

                                          Log.d(TAG, "Longitud marker: " + markers.size() + " marker idMarker 1: " + markers.get(0).getId());
                                      }
                                  }

        );

        map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                                         @Override
                                         public boolean onMarkerClick(Marker marker) {


                                             Log.d(TAG, "id marker: " + marker.getId());
                                             Log.d(TAG, "total markers: " + markers.size());

                                             return false;
                                         }
                                     }
        );

        map.setOnMarkerDragListener(new OnMarkerDragListener() {
            @Override
            public void onMarkerDragStart(Marker marker) {
                Log.d(TAG, "onMarkerDragStart!!!");
            }

            @Override
            public void onMarkerDrag(Marker marker) {
                Log.d(TAG, "onMarkerDrag!!!");
            }

            @Override
            public void onMarkerDragEnd(Marker marker) {
                Log.d(TAG, "onMarkerDragEnd!!!");
            }
        });

        // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
        try {
            MapsInitializer.initialize(this.getActivity());
        } catch (Exception e) {
            e.printStackTrace();
        }

        //updateCameraLocation(null);
        //createMarker();
        Log.d(TAG, "Fin de onCReateView");
        return v;
    }

    public void updateCameraLocation(Integer zoom){
        CameraUpdate cameraUpdate = null;

        if (zoom == null){
            cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(this.getLat(), this.getLng()), this.INITIAL_ZOOM);
        }else{
            cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(this.getLat(), this.getLng()), zoom.intValue());
        }

        if (cameraUpdate != null) {
            Log.d(TAG, "Vamos a crear los markers");
            String txt = getResources().getString(R.string.txt_you_here);
            String snippet = "Snippet!!!";
            createMarker(new LatLng(this.getLat(), this.getLng()), txt, snippet);
            map.animateCamera(cameraUpdate);
        }
    }

    private void createMarker(LatLng latLng, String title, String snippet){

        Marker marker = map.addMarker(new MarkerOptions()
                        .position(latLng)
                        .title(title)
                        .snippet(snippet)
                        .draggable(true)
        );
        marker.setVisible(true);
        marker.showInfoWindow();
        String idMarker = marker.getId();
        Log.d(TAG, "id marker: " + idMarker);
        markers.add(marker);
        Log.d(TAG, "size después de put: " + markers.size() + " marker idMarker: " + markers.get(0).getId());
    }

    @Override
    public void onResume() {
        mapView.onResume();
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }


    public void setLat(double lat){
        this.lat = lat;
    }

    public double getLat(){
        return this.lat;
    }

    public void setLng(double lng){
        this.lng = lng;
    }

    public double getLng(){
        return this.lng;
    }
}

方法onMapClick(LatLng latLng)onMarkerClick(Marker marker)工作正常,但方法onMarkerDragStart(Marker marker)onMarkerDrag(Marker marker)onMarkerDragEnd(Marker marker)无法正常工作。

发生了什么事?

1 个答案:

答案 0 :(得分:0)

为了使标记能够拖动,您需要在创建标记时实施marker.setDraggable(true);