Java 重新启用长时间单击地图
我有一个地图,用户可以在地图上长时间单击并添加一个标记(它会打开新的活动,以便他可以添加有关标记的信息)。我有一个问题,多次长点击,打开多个新的活动。我找到了解决方案,能够阻止多次长时间单击,但在用户打开新活动后重置长时间单击对我不起作用 我的地图片段:Java 重新启用长时间单击地图,java,android,google-maps,android-intent,onlongclicklistener,Java,Android,Google Maps,Android Intent,Onlongclicklistener,我有一个地图,用户可以在地图上长时间单击并添加一个标记(它会打开新的活动,以便他可以添加有关标记的信息)。我有一个问题,多次长点击,打开多个新的活动。我找到了解决方案,能够阻止多次长时间单击,但在用户打开新活动后重置长时间单击对我不起作用 我的地图片段: //Add marker on long click mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() { @Override
//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);
}
});
//长按添加标记
mMap.setOnMapLongClickListener(新的GoogleMap.OnMapLongClickListener(){
@凌驾
在MaplongClick上的公共无效(最终LatLng arg0){
//禁用长时间单击
mMap.setOnMapLongClickListener(空);
RequestQueue=Volley.newRequestQueue(getActivity());
字符串url=”https://maps.googleapis.com/maps/api/geocode/json?latlng=“+String.valueOf(arg0.latitude)+”,“+String.valueOf(arg0.longitude)+”&key=myKey”;
//从提供的URL请求字符串响应。
StringRequest StringRequest=新建StringRequest(Request.Method.GET,url,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
试一试{
JSONArray jObj=新的JSONObject(response).getJSONArray(“结果”).getJSONObject(0).getJSONArray(“地址组件”);
Intent Intent=new Intent(getActivity(),AddRestaurantActivity.class);
对于(int i=0;i
为长时间单击创建侦听器,例如:
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);
}
};
您可以根据前面使用的代码禁用LongClicklistener,并在收到响应时添加侦听器,代码包含在我的onMapLongClick()中,您只需复制代码并在onMapReady()上添加侦听器,如上所述
让我知道它是否解决了您的问题。为长时间单击创建一个侦听器,例如:
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);
}
};
您可以根据前面使用的代码禁用LongClicklistener,并在收到响应时添加侦听器,代码包含在我的onMapLongClick()中,您只需复制代码并在onMapReady()上添加侦听器,如上所述
让我知道它是否解决了您的问题。找到了解决方法-我使用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);
}
};
GoogleMap.OnMapLongClickListener OnMapLongClickListener=新建GoogleMap.OnMapLongClickListener(){
@凌驾
在MaplongClick(LatLng LatLng)上的公共无效{
ProgressDialog pd=ProgressDialog.show(此为“,”正在加载。请稍候…,为true);
RequestQueue=Volley.newRequestQueue(getActivity());
字符串url=”https://maps.googleapis.com/maps/api/geocode/json?latlng=“+String.valueOf(arg0.latitude)+”,“+String.valueOf(arg0.longitude)+”&key=myKey”;
//从提供的URL请求字符串响应。
StringRequest StringRequest=新建StringRequest(Request.Method.GET,url,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
试一试{
JSONArray jObj=新的JSONObject(response).getJSONArray(“结果”).getJSONObject(0).getJSONArray(“地址组件”);
Intent Intent=new Intent(getActivity(),AddRestaurantActivity.class);
对于(int i=0;i