【问题标题】:Making a marker carry custom info to be used when clicked (Android, Google Maps v2)使标记带有单击时要使用的自定义信息(Android,Google Maps v2)
【发布时间】:2014-12-15 04:21:23
【问题描述】:

我有以下代码:

public class AlertViewOnMap extends Activity {

    //declarations
    ArrayList<String> dateCreatedAtList = new ArrayList<String>();

    TextView busNumberTextView;
    TextView descriptionTextView;
    TextView alertTimeTextView;

    DateFormat dateFormat = new SimpleDateFormat("HH:mm");


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

        for(int i =0; i<alertsList.size(); i++) {

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

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

                busNumberTextView.setText(marker.getSnippet());
                descriptionTextView.setText(marker.getTitle());
                alertTimeTextView.setText(dateCreatedAtList.get(marker.getId()));
                marker.showInfoWindow();
                return true;
            }
        };
    }    
}

我的问题是:我想添加自定义数据以存储在我的标记中,这样当我单击它时,“alerTimeTextView”会从“dateCreatedAtList”获取标记创建时间?

【问题讨论】:

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


    【解决方案1】:

    试试这个方法,希望能帮助你解决问题。

    定义一个 HashMap,其中 Marker 为键,Alert 为值,当您创建新的 Marker 以添加到地图时添加此 Marker 作为 HashMap 中的 key 和 alertsList 中的相应 Alert 数据,从 HashMap 中获取特定的 Alert 数据strong> 当特定的标记信息窗口被点击时。

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

    【讨论】:

    • 它正在工作,兄弟!非常感谢!上帝保佑喜欢你的优秀乐于助人的程序员。
    【解决方案2】:

    回答您的问题:根据here,Marker 似乎不允许任何“设置”方法,例如 View 的标签

    我认为 Haresh 的解决方案通常是正确的。

    我只是对以下方法进行以下更正:

    public GoogleMap.OnMarkerClickListener getInfoMarkerClickListener()
    

    行:

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

    应该是:

     alertTimeTextView.setText(alert.getCreatedAt());
    

    【讨论】:

    • 是的,这是一个错误,我知道它不会起作用。但问题是我希望我的 alertTimeTextView 在我单击另一个标记时发生变化。立即实施 Haresh 解决方案。
    • 您的意思是您希望避免保留标记和警报的映射?就像我说的,我认为没有办法
    猜你喜欢
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多