Making a marker carry custom info to be used when clicked (Android, Google Maps v2)

后端 未结 2 1158
北恋
北恋 2020-12-22 03:18

I have the following code:

public class AlertViewOnMap extends Activity {

    //declarations
    ArrayList dateCreatedAtList = new ArrayList&l         


        
相关标签:
2条回答
  • 2020-12-22 03:54

    Try this way,hope,this will help you to solve your problem.

    Define one HashMap with Marker as key and Alert as value and when you create new Marker to add on map at time add this Marker as key in HashMap and respective Alert data from alertsList,get specific Alert data from HashMap when particular Marker info window clicked.

    public class AlertViewOnMap extends Activity {
    
        //declarations
        ArrayList<String> dateCreatedAtList = new ArrayList<String>();
    
        TextView busNumberTextView;
        TextView descriptionTextView;
        TextView alertTimeTextView;
    
        DateFormat dateFormat = new SimpleDateFormat("HH:mm");
        private HashMap<Marker,Alert> markerDataMap;
    
    
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            this.setContentView(com.fourbox.bocterapp.R.layout.details_design);
    
            busNumberTextView = (TextView) findViewById(R.id.textViewAlertBusNumber);
            descriptionTextView = (TextView) findViewById(R.id.textViewAlertDescription);
            alertTimeTextView = (TextView) findViewById(R.id.textViewAlertTime);
    
            busNumber = getIntent().getIntExtra("busNumber", 0);
            description = getIntent().getStringExtra("description");
            coordinatesLatitude =  getIntent().getDoubleExtra("coordinatesLatitude", 0);
            coordinatesLongitude = getIntent().getDoubleExtra("coordinatesLongitude", 0);
    
            alertTime.setTime(getIntent().getLongExtra("createdAt", 0));
    
            busNumberList = getIntent().getStringArrayListExtra("busNumberList");
            descriptionList = getIntent().getStringArrayListExtra("descriptionList");
            coordinatesLatitudeList =     getIntent().getStringArrayListExtra("coordinatesLatitudeList");
            coordinatesLongitudeList =     getIntent().getStringArrayListExtra("coordinatesLongitudeList");
            dateCreatedAtList = getIntent().getStringArrayListExtra("dateCreatedAtList");
    
            GoogleMap mMap;
            mMap = ((MapFragment)     getFragmentManager().findFragmentById(com.fourbox.bocterapp.R.id.mapFragment)).getMap();
            reuniteAlertListFromGetExtra();
            placeAllMarkersOnMap(mMap, alertsList);
    
            LatLng latLng = new LatLng(coordinatesLatitude, coordinatesLongitude);
    
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(latLng) // Center Set
                    .zoom(18.0f)                // Zoom
                    .bearing(0)                // Orientation of the camera to east
                    .tilt(30)                   // Tilt of the camera to 30 degrees
                    .build();                   // Creates a CameraPosition from the builder
            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    
            busNumberTextView.setText(String.valueOf(busNumber));
            descriptionTextView.setText(description);
            alertTimeTextView.setText(String.valueOf(dateFormat.format(alertTime)));
        }
    
        public void placeAllMarkersOnMap(GoogleMap mMap, ArrayList<Alert> alertsList) {
            markerDataMap = new HashMap<Marker, Alert>();
            for(int i=0; i<alertsList.size(); i++) {
                markerDataMap.put(mMap.addMarker(new MarkerOptions()
                        .position(new     LatLng(alertsList.get(i).getCoordinates().getLatitude(),     alertsList.get(i).getCoordinates().getLongitude()))
                        .title(alertsList.get(i).getDescription())
                        .snippet(String.valueOf(alertsList.get(i).getBusNumber())
                        )),alertsList.get(i));
    
            }
        }
    
        public void reuniteAlertListFromGetExtra() {
    
            for (int i =0; i<busNumberList.size(); i++) {
    
                Alert alert = new Alert();
                ParseGeoPoint parseGeoPoint = new ParseGeoPoint();
    
                parseGeoPoint.setLatitude(Double.valueOf(coordinatesLatitudeList.get(i)));
                parseGeoPoint.setLongitude(Double.valueOf(coordinatesLongitudeList.get(i)));
    
                alert.setBusNumber(Integer.valueOf(busNumberList.get(i)));
                alert.setDescription(descriptionList.get(i));
                alert.setCoordinates(parseGeoPoint);
                alert.setCreatedAt(new Date(Long.valueOf(dateCreatedAtList.get(i))));
    
                alertsList.add(alert);
            }
        }
    
        public GoogleMap.OnMarkerClickListener getInfoMarkerClickListener() {
            return new GoogleMap.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(Marker marker) {
                    Alert alert = markerDataMap.get(marker);
                    busNumberTextView.setText(alert.getDescription());
                    descriptionTextView.setText(alert.getBusNumber());
                    alertTimeTextView.setText(dateCreatedAtList.get(alert.getId()));
                    marker.showInfoWindow();
                    return true;
                }
            };
        }
    }
    
    0 讨论(0)
  • 2020-12-22 04:02

    To asnwer your question: it doesn't seem like Marker allows any 'set' methods like View's tag as per here

    I think Haresh's solution is the generally right one.

    I'd just make the following correction to the following method:

    public GoogleMap.OnMarkerClickListener getInfoMarkerClickListener()
    

    The line:

     alertTimeTextView.setText(dateCreatedAtList.get(alert.getId()));
    

    should be :

     alertTimeTextView.setText(alert.getCreatedAt());
    
    0 讨论(0)
提交回复
热议问题