2017-11-28 16 views
0

사용자가지도를 길게 클릭하고 새로운 태그 및 정보를 추가 할 수있는 새로운 활동을 열 수있는 앱을 만들고 있습니다. 사용자가 길게 한 번 클릭하면, 그가 충분히 빠르면지도에서 두 번 길게 클릭 할 수 있고 두 번째 활동은 두 번 열립니다. 이 동작을 비활성화하는 방법을 찾으려고합니다. 이미 몇 가지 예제를 시도해 보았지만 플래그를 추가하려고했지만 아무런 효과가 없었습니다.맵 길게 누르기에서 활성화 된 복수 반복 활성화

긴 클릭을 두 번 사용 중지하고 싶습니다. 또한 로더를 추가하고 싶습니다. 사용자가 이미 오래 동안 클릭하여 새 활동을 열어서 클릭 한 경우 긴 클릭을 사용 중지하고 새 활동이 닫힌 경우 다시 사용하도록 설정하십시오..

내지도 조각은 다음과 같습니다

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

    @Override 
    public void onMapLongClick(final LatLng arg0) { 

     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); 

    } 
}); 

을 그리고 이것은 플래그 추가 (다른 사람의 사이에서)가 thisthis 답변, 열 활동 노력이다

private void setRestaurant(final String userId, final String message, final String pickDate, final String pickTime, final String location, final String lat, final String lon, final String sendTo, final boolean enableComments) { 
    // Tag used to cancel the request 
    String tag_string_req = "req_add_restaurant"; 

    final String commentsEnabled = (enableComments) ? "0" : "1"; 

    pDialog.setMessage(getString(R.string.setting_a_restaurant)); 
    showDialog(); 

    ApiInterface apiService = 
      ApiClient.getClient().create(ApiInterface.class); 

    Call<DefaultResponse> call = apiService.addrestaurant(userId, message, lat, lon, pickDate, pickTime, sendTo, commentsEnabled); 
    call.enqueue(new Callback<DefaultResponse>() { 
     @Override 
     public void onResponse(Call<DefaultResponse> call, retrofit2.Response<DefaultResponse> response) { 

      // Launch main activity 
      Intent intent = new Intent(SetRestaurantActivity.this, 
        MainActivity.class); 
      // I TRIED TO BLOCK IT HERE 
      intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
      // I ALSO TRIED: 
      // intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
      startActivity(intent); 
      finish(); 

      Toast.makeText(getApplicationContext(), R.string.sucessfully_created_restaurant, Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 

답변

2

간단한 추가 플래그 변수. 이 경우에는 isRequestProcess 변수를 사용하고 있습니다.

Boolean isRequestProcess = false; 
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() { 

@Override 
public void onMapLongClick(final LatLng arg0) { 
    if(isRequestProcess){ 
     return; 
    } 
    isRequestProcess = 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); 
        isRequestProcess = false; 

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

      @Override 
      public void onErrorResponse(VolleyError error) { 
       int x = 1; 
      } 

     } 
    } 
} 
+0

답변 해 주셔서 감사합니다. Saheb. 이것은 특정 지점에서 작동합니다. 동시에 두 개의 액티비티를 열지 못하게하지만, 두 번 클릭하면 새로운 액티비티가 한 번 열립니다. 닫으면 다시 열립니다 ... – Kemo

+0

isRequestProcess 대신 ProgressDialog를 사용했는데 이제 작동합니다 :'ProgressDialog pd = ProgressDialog.show (this, "", "로드 중, 잠시만 기다려주십시오 ...", true); // 파일 다운로드 pd.cancel();' – Kemo