在android google map v2中只制作一次可点击的标记

时间:2016-05-16 05:07:32

标签: android google-maps google-maps-markers android-maps-v2

我在Google地图中显示一组标记,我想让制作者只能点击一次。如果我再次点击相同的标记,我应该收到类似"already clicked"之类的消息。我该怎么办?

  public void getData() {
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(DATA_URL,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    parseData(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {


                }
            });

    //Creating request queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    //Adding request to the queue
    requestQueue.add(jsonArrayRequest);
}
private void parseData(JSONArray array) {


    for (int i = 0; i < array.length(); i++) {
        JSONObject json = null;
        try {
            json = array.getJSONObject(i);


            place1 = json.getString("place");


            Lat1 = json.getString("latitude");


            Long1 = json.getString("longitude");
            sensor = json.getString("sensor1");
            bin_capacity = json.getString("capacity");






            if (Integer.parseInt(sensor) >= 0 && Integer.parseInt(sensor) <= 60) {
                LatLng latLng = new LatLng(Double.parseDouble(Lat1), Double.parseDouble(Long1));
                arrayLatlong.add(latLng);


                for (int j = 0; j < arrayLatlong.size(); j++) {
                    arrayPlace.add(place1);
                    bin.add(bin_capacity);


                   myMarker= map.addMarker(new MarkerOptions().position(latLng).title(bin_capacity));
                    arrayMarker.add(myMarker);




                    map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                                                    @Override
                                                     public boolean onMarkerClick(Marker marker) {


                                                         for (int k = 0; k < arrayMarker.size(); k++) {

                                                             if (marker.equals(arrayMarker.get(k))) {

                                                                     counter++;
                                                                     capacity.setText("Capacity = "+counter);



                                                                 }

        } catch (JSONException e) {
            e.printStackTrace();
        }


    }
}

4 个答案:

答案 0 :(得分:1)

使用布尔值来维持状态:

boolean isMarkerClicked = false;

并在onclicklistener中更新

map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                                                    @Override
                                                     public boolean onMarkerClick(Marker marker) {
          if(!isMarkerClicked){
               isMarkerClicked = true;
             //put your rest of code that will work on marker click
            }else{
             //show here toast message 
           }


 }
});

答案 1 :(得分:0)

试试这个

在标记点击侦听器此代码

googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            //Toast.makeText(MapActivity.this, marker.getTitle(), Toast.LENGTH_SHORT).show();// display toast
            return false;
        }
    });

//标记标题和可点击的消息使用此代码

googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {

            if (marker.getTitle().equals("Your Location")) {

            } else {
                Toast.makeText(MapActivity.this, map.get("" + marker_list.indexOf(marker.getTitle())).get("stylistUserId"), Toast.LENGTH_SHORT).show();


            }
        }
    });

答案 2 :(得分:0)

在这种情况下,您可以创建一个Temporary ArrayList() or HashMap()

onMarkerClick()方法中检查我们的temp list是否包含标记,是否显示toast以及添加temp list的标记。

答案 3 :(得分:0)

替换以下代码:

 map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {


     for (int k = 0; k < arrayMarker.size(); k++) {

      if (marker.equals(arrayMarker.get(k))) {

       counter++;
       capacity.setText("Capacity = " + counter);



      }

     } catch (JSONException e) {
      e.printStackTrace();
     }


    }
}

用这个:

map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
   @Override
   public boolean onMarkerClick(Marker marker) {

    if (arrayMarker.contains(marker)) {
       //show here toast message 
    } else {
        arrayMarker.add(marker);
       //put your rest of code that will work on marker click
    }
   }
  }