拖动按钮单击后将标记重置为初始位置

时间:2017-10-29 00:44:27

标签: android google-maps google-maps-markers google-maps-android-api-2

我有一个“调查”,我使用edittext填充信息(姓名,姓氏,电子邮件等)和带有标记的地图,它可以拖动,用户可以将其移动到部分地图,之后有一个按钮,当它被点击时,信息被发送并存储在数据库中,在发送信息后,所有的edittext都被清理,但是标记保持在用户放置的相同位置,所以我的问题是,如何在发送调查后将标记移动到初始位置? 如何实现这一点的任何想法将不胜感激!

CUSTOM MAPVIEW:

public class CustomMapView extends MapView {
public CustomMapView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {

    switch (ev.getAction()) {
        case MotionEvent.ACTION_UP:
            System.out.println("unlocked");
            this.getParent().requestDisallowInterceptTouchEvent(false);
            break;
        case MotionEvent.ACTION_DOWN:
            System.out.println("locked");
            this.getParent().requestDisallowInterceptTouchEvent(true);
            break;
    }
    return super.dispatchTouchEvent(ev);
}
}

NEW LOGCAT:

10-28 20:16:47.515 9087-9087/com.example.lupitagarcia.yosoyvallarta E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: com.example.lupitagarcia.yosoyvallarta, PID: 9087
                                                                                  java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.clear()' on a null object reference
                                                                                      at com.example.lupitagarcia.yosoyvallarta.TerminosYC$CargarDatos.onPostExecute(TerminosYC.java:836)
                                                                                      at com.example.lupitagarcia.yosoyvallarta.TerminosYC$CargarDatos.onPostExecute(TerminosYC.java:738)
                                                                                      at android.os.AsyncTask.finish(AsyncTask.java:651)
                                                                                      at android.os.AsyncTask.access$500(AsyncTask.java:180)
                                                                                      at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:7406)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

XML:

<com.example.lupitagarcia.yosoyvallarta.CustomMapView
                android:id="@+id/mapview"
                android:layout_width="match_parent"
                android:layout_height="300dp" />

主:

googleMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
        @Override
        public void onMarkerDragStart(Marker marker) {
            LatLng position = marker.getPosition();
            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                    new LatLng(position.latitude, position.longitude), 15));
        }

        @Override
        public void onMarkerDrag(Marker marker) {
            LatLng position = marker.getPosition();
            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                    new LatLng(position.latitude, position.longitude), 15));
        }

        @Override
        public void onMarkerDragEnd(Marker marker) {
            LatLng position = marker.getPosition();
                  latitudeeeee = position.latitude;
                  longitudeeee = position.longitude;
        }
    });
}

public class CargarDatos extends AsyncTask<String, Void, String> {
    protected String doInBackground(String... urls) {

        ////////////////-------------
        String correo = campo_correo.getText().toString().trim();
        String nombre = campo_nombre.getText().toString().trim();
        String apellido= campo_apellido.getText().toString().trim();
        String telefono= campo_telefono.getText().toString().trim();
        String categoria= customspinner.getSelectedItem().toString().trim();
        String titulo = campo_titulo.getText().toString().trim();
        String descripcion = campo_descripcion.getText().toString().trim();
        String latitud = Double.toString(latitudeeeee);
        String longitud = Double.toString(longitudeeee);

        String s = (campo_publico.isChecked() ? "1" : "0");
        String r = (campo_terminos.isChecked() ? "1" : "0");

        //getting the actual path of the image
        String path = getPath(filePath);

        //Uploading code
        try {
            String uploadId = UUID.randomUUID().toString();

            //Creating a multi part request
            new MultipartUploadRequest(TerminosYC.this.getActivity(), uploadId, Constants.UPLOAD_URL)
                    .addFileToUpload(path, "image") //Adding file
                    .addParameter("name",correo) //Adding text parameter to the request
                    .addParameter("nombre",nombre)
                    .addParameter("apellido",apellido)
                    .addParameter("telefono", telefono)
                    .addParameter("categoria", categoria)
                    .addParameter("titulo", titulo)
                    .addParameter("descripcion", descripcion)
                    .addParameter("publico", s)
                    .addParameter("terminos", r)
                    .addParameter("latitud",latitud)
                    .addParameter("longitud",longitud)
                    .setNotificationConfig(new UploadNotificationConfig())
                    .setMaxRetries(2)
                    .startUpload(); //Starting the upload

        } catch (Exception exc) {
            Toast.makeText(TerminosYC.this.getActivity(), exc.getMessage(), Toast.LENGTH_SHORT).show();
        }

        // params comes from the execute() call: params[0] is the url.
        try {
            return downloadUrl(urls[0]);
        } catch (IOException e) {
            return "Unable to retrieve web page. URL may be invalid.";
        }
    }

    ProgressDialog loading;
    RequestHandler rh = new RequestHandler();


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        loading = ProgressDialog.show(TerminosYC.this.getActivity(), "Enviando reporte", "Porfavor espere...", true, true);
    }
    // onPostExecute displays the results of the AsyncTask.

    @Override
    protected void onPostExecute(String result) {

        loading.dismiss();
        showAlertDialogMensaje(TerminosYC.this.getActivity(), "Reporte", "El reporte ha sido enviado!", true);

        campo_correo.setText(null);
        campo_nombre.setText(null);
        campo_apellido.setText(null);
        campo_telefono.setText(null);
        customspinner.setSelection(0);
        campo_titulo.setText(null);
        campo_descripcion.setText(null);
        imageView.setImageResource(0);
        imagencargada.setVisibility(View.GONE);
        textoimagencargada.setVisibility(View.GONE);
        campo_terminos.setError(null);
        buttonChoose.setError(null);
        campo_publico.setChecked(false);
        campo_terminos.setChecked(false);

        areaLeyendaImagen.setVisibility(View.GONE);
        areaFotoCargada.setVisibility(View.GONE);
        imagencargada.setVisibility(View.GONE);
        textoimagencargada.setVisibility(View.GONE);
        area_cerrarIagen.setVisibility(View.GONE);
        cerrarImagen.setVisibility(View.GONE);

        bb.setEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            bb.setBackgroundTintList(ContextCompat.getColorStateList(TerminosYC.this.getContext(), R.color.enable));
        }

logcat的:

10-28 19:59:48.687 3297-3297/com.example.lupitagarcia.yosoyvallarta E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: com.example.lupitagarcia.yosoyvallarta, PID: 3297
                                                                                  java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.clear()' on a null object reference
                                                                                      at com.example.lupitagarcia.yosoyvallarta.TerminosYC$CargarDatos.onPostExecute(TerminosYC.java:830)
                                                                                      at com.example.lupitagarcia.yosoyvallarta.TerminosYC$CargarDatos.onPostExecute(TerminosYC.java:737)
                                                                                      at android.os.AsyncTask.finish(AsyncTask.java:651)
                                                                                      at android.os.AsyncTask.access$500(AsyncTask.java:180)
                                                                                      at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:7406)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

1 个答案:

答案 0 :(得分:0)

创建一个包含所需初始位置LatLng的变量。

private static final LatLng initialPosition = new LatLng(21.606789, -102.206598);

然后是您的CargarDatos

public class CargarDatos extends AsyncTask<String, Void, String> {

    private GoogleMap googleMap;

    // Create a constructor that holds your googleMap.
    public CargarDatos (GoogleMap googleMap) {
        this.googleMap = googleMap;
    }

    // Some of your stuff ...

    @Override
    protected void onPostExecute(String result) {

        googleMap.clear();
        googleMap.addMarker(new MarkerPosition().position(initialPosition).title("StartPosition"));
    }

}

然后致电您的CargarDatos

CargarDatos cargarDatos = new CargarDatos(googleMap);
cargarDatos.execute();

编辑:

public class CustomMapView extends MapView {

    // Some of your stuff...

    public void clear() {
        this.clear();
    }
}