制作标记携带的自定义信息点击时要使用的(Android的谷歌地图V2) [英] Making a marker carry custom info to be used when clicked (Android, Google Maps v2)

查看:188
本文介绍了制作标记携带的自定义信息点击时要使用的(Android的谷歌地图V2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

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

My question is: I want to add custom data to be stored in my marker, such that when i click it, "alerTimeTextView" would get the marker creation hour, from "dateCreatedAtList"?

推荐答案

试试这个办法,希望,这将帮助你解决你的问题。

定义一个的HashMap 标记作为键和提示作为值,当你创建新的标记,以在地图上添加时刻添加该< STRONG>标记作为关键的HashMap和相应的提示 alertsList ,得到具体的提示从数据的HashMap 当特定的标记的信息窗口点击。

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

这篇关于制作标记携带的自定义信息点击时要使用的(Android的谷歌地图V2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆