将数据传递到infowindowclick谷歌地图

时间:2018-06-17 23:30:13

标签: android google-maps

我已声明当前位置latlong和所选位置latlong。我想将这些latlong传递给onInfoWindowClick()。 当我尝试使用Toast获取我从marker.setTag(mLatitude)marker.seTag(mLongitude)设置的数据时,它仅向我提供相同的数据mLongitude。请有人帮帮我。 这是我的代码:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long l) {
                if (parent.getItemAtPosition(position).toString() != "-- Pilih ATM --"){

                    mMap.clear();

                    String pilih_atm = (String) parent.getItemAtPosition(position);
//                    Toast.makeText(getActivity(), pilih_atm, Toast.LENGTH_SHORT).show();

                    SQLiteDatabase db = dbHelper.getReadableDatabase();
                    cursor = db.rawQuery("SELECT * FROM atm WHERE atm_name = '" + pilih_atm + "'",null);
                    if (cursor != null){
                        while (cursor.moveToNext()){
                            title = cursor.getString(1).toString();
                            __global_endposition = cursor.getString(2).toString();
                            String[] exp_endCoordinate = __global_endposition.split(",");
                            double lat_endposition = Double.parseDouble(exp_endCoordinate[0]);
                            double lng_endposition = Double.parseDouble(exp_endCoordinate[1]);
                            LatLng endx = new LatLng(lat_endposition, lng_endposition);

                            MarkerOptions options = new MarkerOptions();
                            options.position(endx);
                            options.title(title);
                            options.snippet(__global_endposition);
                            if (title.equals("ATM BNI")){
                                options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
                            }else if(title.equals("ATM BCA")){
                                options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
                            }else if(title.equals("ATM Mandiri")){
                                options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
                            }

                            Marker marker = mMap.addMarker(options);
                            marker.setTag(mLatitude);
                            marker.setTag(mLongitude);

                            mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter(getActivity()));

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

                                    // I want to get current location LatLong and selected location LatLong
                                    // I want execute the LatLong on this method

                                    // this just for testing
                                    Toast.makeText(getActivity(), "LatLng: "+marker.getTag()+", "+marker.getTag(), Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                        if (!cursor.isClosed()) {
                            cursor.close();
                            cursor = null;
                        }

                    }

                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

1 个答案:

答案 0 :(得分:0)

这是因为Marker对象只有一个Tag对象,当你第二次调用.setTag()时,你会覆盖以前设置的标记对象:

Marker marker = mMap.addMarker(options);
marker.setTag(mLatitude);          // tag = mLatitude
marker.setTag(mLongitude);         // tag overrides, and now tag = mLongitude

解决方案之一是:

Marker marker = mMap.addMarker(options);
marker.setTag("" + mLatitude + "," + mLongitude);    // tag now is string: "mLatitude, mLongitude"