重新启用长按地图

时间:2017-11-29 07:06:28

标签: java android google-maps android-intent onlongclicklistener

我有一张地图,用户可以选择长按并添加标记(它会根据意图打开新活动,因此他可以添加有关标记的信息)。多次长按,我遇到了问题,打开了多个新活动。我找到了this解决方案,并且能够阻止多次长按,但是在用户打开新活动后重置长按不会对我有效。

我的地图片段:

 //Add marker on long click
 mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

     @Override
     public void onMapLongClick(final LatLng arg0) {
         // disabling long click
         mMap.setOnMapLongClickListener(null);

         RequestQueue queue = Volley.newRequestQueue(getActivity());
                    String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + String.valueOf(arg0.latitude) + "," + String.valueOf(arg0.longitude) + "&key=myKey";

         // Request a string response from the provided URL.
         StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
             @Override
             public void onResponse(String response) {
                 try {
                     JSONArray jObj = new JSONObject(response).getJSONArray("results").getJSONObject(0).getJSONArray("address_components");

                     Intent intent = new Intent(getActivity(), AddRestaurantActivity.class);

                      for (int i = 0; i < jObj.length(); i++) {
                          String componentName = new JSONObject(jObj.getString(i)).getJSONArray("types").getString(0);
                          if (componentName.equals("postal_code") || componentName.equals("locality") || componentName.equals("street_number") || componentName.equals("route")
                                                    || componentName.equals("neighborhood") || componentName.equals("sublocality") || componentName.equals("administrative_area_level_2")
                                                    || componentName.equals("administrative_area_level_1") || componentName.equals("country")) {
                                                intent.putExtra(componentName, new JSONObject(jObj.getString(i)).getString("short_name"));
                           }
                      }

                       intent.putExtra("latitude", arg0.latitude);
                       intent.putExtra("longitude", arg0.longitude);

                       startActivity(intent);

                   } catch (JSONException e) {
              e.printStackTrace();
              }
          }
    }, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        int x = 1;
    }
 });
 // Add the request to the RequestQueue.
 queue.add(stringRequest);

    }
});

2 个答案:

答案 0 :(得分:2)

为长按创建一个侦听器,例如:

 GoogleMap.OnMapLongClickListener onMapLongClickListener =new GoogleMap.OnMapLongClickListener() {
        @Override
        public void onMapLongClick(LatLng latLng) {

            mMap.setOnMapLongClickListener(null);

            RequestQueue queue = Volley.newRequestQueue(getActivity());
            String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + String.valueOf(arg0.latitude) + "," + String.valueOf(arg0.longitude) + "&key=myKey";

            // Request a string response from the provided URL.
            StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONArray jObj = new JSONObject(response).getJSONArray("results").getJSONObject(0).getJSONArray("address_components");

                        Intent intent = new Intent(getActivity(), AddRestaurantActivity.class);

                        for (int i = 0; i < jObj.length(); i++) {
                            String componentName = new JSONObject(jObj.getString(i)).getJSONArray("types").getString(0);
                            if (componentName.equals("postal_code") || componentName.equals("locality") || componentName.equals("street_number") || componentName.equals("route")
                              || componentName.equals("neighborhood") || componentName.equals("sublocality") || componentName.equals("administrative_area_level_2")
                              || componentName.equals("administrative_area_level_1") || componentName.equals("country")) {
                                intent.putExtra(componentName, new JSONObject(jObj.getString(i)).getString("short_name"));
                            }
                        }

                        intent.putExtra("latitude", arg0.latitude);
                        intent.putExtra("longitude", arg0.longitude);

                        startActivity(intent);
                        mMap.setOnMapLongClickListener(onMapLongClickListener);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    int x = 1;
                }
            });
            // Add the request to the RequestQueue.
            queue.add(stringRequest);
        }
    };

并在你的onMapReady,onMapLongClickListener的setListener:

 mMap.setOnMapLongClickListener(onMapLongClickListener);

您可以根据您使用的上一个代码禁用LongClicklistener,并在获得响应时添加侦听器,Code包含在我的 onMapLongClick()中,您只需复制代码并添加侦听器on onMapReady()如上所述。

让我知道它是否能解决您的问题。

答案 1 :(得分:0)

找到了一种解决方法 - 我使用ProgressDialog禁用用户长按两次:

 GoogleMap.OnMapLongClickListener onMapLongClickListener =new GoogleMap.OnMapLongClickListener() {
        @Override
        public void onMapLongClick(LatLng latLng) {

            ProgressDialog pd = ProgressDialog.show(this,"","Loading. Please wait...",true);

            RequestQueue queue = Volley.newRequestQueue(getActivity());
            String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + String.valueOf(arg0.latitude) + "," + String.valueOf(arg0.longitude) + "&key=myKey";

            // Request a string response from the provided URL.
            StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONArray jObj = new JSONObject(response).getJSONArray("results").getJSONObject(0).getJSONArray("address_components");

                        Intent intent = new Intent(getActivity(), AddRestaurantActivity.class);

                        for (int i = 0; i < jObj.length(); i++) {
                            String componentName = new JSONObject(jObj.getString(i)).getJSONArray("types").getString(0);
                            if (componentName.equals("postal_code") || componentName.equals("locality") || componentName.equals("street_number") || componentName.equals("route")
                              || componentName.equals("neighborhood") || componentName.equals("sublocality") || componentName.equals("administrative_area_level_2")
                              || componentName.equals("administrative_area_level_1") || componentName.equals("country")) {
                                intent.putExtra(componentName, new JSONObject(jObj.getString(i)).getString("short_name"));
                            }
                        }

                        intent.putExtra("latitude", arg0.latitude);
                        intent.putExtra("longitude", arg0.longitude);

                        startActivity(intent);
                        pd.cancel();

                        mMap.setOnMapLongClickListener(onMapLongClickListener);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    pd.cancel();
                }
            });
            // Add the request to the RequestQueue.
            queue.add(stringRequest);
        }
    };