【问题标题】:Reenabling a Long Click on Map重新启用长按地图
【发布时间】:2018-05-12 19:35:00
【问题描述】:

我有一张地图,用户可以选择长按并添加标记(它会有意打开新活动,因此他可以添加有关标记的信息)。我遇到了多次长时间点击的问题,打开了多个新活动。我找到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);

    }
});

【问题讨论】:

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


    【解决方案1】:

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

     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 设置监听器:

     mMap.setOnMapLongClickListener(onMapLongClickListener);
    

    您可以按照之前使用的代码禁用 LongClicklistener 并在收到响应时添加侦听器,代码包含在我的 onMapLongClick() 中,您只需复制代码并添加侦听器如上所述,在 onMapReady() 上。

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

    【讨论】:

    • 谢谢萨钦。我今晚会试试这个,让你知道,如果可行,请接受你的回答:)
    • 我尝试了这个解决方案,但不幸的是它不起作用。由于这个方法在 onMapReady 中,我添加了这个:queue.add(stringRequest); } }; mMap.setOnMapLongClickListener(onMapLongClickListener); 另外,这行代码给出了mMap.setOnMapLongClickListener(onMapLongClickListener); 给出了错误 - 未初始化。
    【解决方案2】:

    找到一个解决方法 - 我使用 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);
            }
        };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      • 2014-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多