Android Google Maps Markers With Different Values On Info Window
I have a map with a lot of markers, and I want custom values for the Info Window in each marker. For adding the markers: final JSONArray stops = new File().getStops(lineId); try {
Solution 1:
Use a HashMap
I'm not sure if this is the best solution but it works. Basically I just added the marker into a HashMap and passed this HashMap to my Custom Info Window class.
Adding the markers:
HashMap<Marker, JSONObject> stopsMarkersInfo = newHashMap<>(); // created the HashMapJSONArraystops=newFile().getStops(lineId);
for (inti=0; i < stops.length(); i++) {
BitmapDescriptorstopMarkerIcon=newFile().getStopMarkerIcon(color, Integer.parseInt(stops.getJSONObject(i).getString("sequence")));
LatLngcoordinate=newLatLng(Double.parseDouble(stops.getJSONObject(i).getString("lat")), Double.parseDouble(stops.getJSONObject(i).getString("lng")));
MarkerOptionsmarkerOptions=newMarkerOptions().icon(stopMarkerIcon).position(coordinate).anchor(.5f, .5f);
Markermarker= googleMap.addMarker(markerOptions);
stopsMarkersInfo.put(marker, stops.getJSONObject(i)); // added each marker and // his information to the HashMap
}
googleMap.setInfoWindowAdapter(newStopsInfoWindow(stopsMarkersInfo)); // passed the HashMap
Info Window Adapter:
publicclassStopsInfoWindowimplementsGoogleMap.InfoWindowAdapter {
privateHashMap<Marker, JSONObject> stopsMarkersInfo;
privateView view;
publicStopsInfoWindow(HashMap<Marker, JSONObject> stopsMarkersInfo) {
this.stopsMarkersInfo = stopsMarkersInfo;
}
@OverridepublicViewgetInfoContents(Marker marker) {
returnnull;
}
@OverridepublicViewgetInfoWindow(final Marker marker) {
JSONObject stop = stopsMarkersInfo.get(marker);
if (stop != null) {
LayoutInflater inflater = (LayoutInflater) Controller.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.item_stop_marker_info, null);
TextView stopName = (TextView) view.findViewById(R.id.stop_name);
stopName.setText(stop.getString("name"));
TextView stopLine = (TextView) view.findViewById(R.id.stop_line);
stopLine.setText(stop.getString("line"));
}
return view;
}
}
Post a Comment for "Android Google Maps Markers With Different Values On Info Window"