您好我在listarray中创建了一些表格数据。这些是正确创建的,然后我将每个标记的infowindow中显示的数据添加到hashmap。
但是infowindow似乎没有读取hashmap数据,也不知道我哪里出错了。
我所有的调试标签都表明在创建标记时数据会正确存储在hashmap中。
然后在infowindow中识别出正确的markerid,但是链接到该makerid的hashmap数据没有显示,所有标记都显示infowindow具有相同的数据。
public class MapFragment extends Fragment实现OnMapReadyCallback {
static final LatLng Loc2 = new LatLng(12.106011, -68.935108);
private GoogleMap map;
private ArrayList<Customer> customerList = new ArrayList<>();
private HashMap<String, HashMap<String, String>> outerMap = new HashMap<String, HashMap<String, String>>();
private HashMap<String, String> innerMap = new HashMap<String, String>();
private Marker marker;
final String TAG3 = "MapFragment:";
public MapFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppStart appStart = (AppStart)getActivity();
customerList = appStart.setMarkers();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_map, container, false);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SupportMapFragment fragment = (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
if (map != null) {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
map.setMyLocationEnabled(true);
}
map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
marker.hideInfoWindow();
}
});
//Create Markers & HashMap
setMarker();
map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View v = getActivity().getLayoutInflater().inflate(R.layout.infowindow, null);
TextView tvCName = (TextView) v.findViewById(R.id.tvCustomerName);
TextView tvCAddress = (TextView) v.findViewById(R.id.tvCustomerAddress);
TextView tvCTel = (TextView) v.findViewById(R.id.tvCustomerTel);
HashMap<String, String> map = outerMap.get(marker.getId());
Log.d(TAG3,"Info Window: get markerID just clicker: "+marker.getId());
Log.d(TAG3,"Info Window: get name linked to markerID just clicked: "+map.get("Name"));
tvCName.setText(map.get("Name"));
tvCAddress.setText(map.get("Address"));
tvCTel.setText(map.get("Tel"));
return v;
}
});
// Move the camera instantly to Curacao with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(Loc2, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
}
}
private void setMarker(){
for(Customer customer : customerList){
String name = customer.getCompanyName();
Double latitude = customer.getCompanyLatitude();
Double longitude = customer.getCompanyLongitude();
String address = customer.getCompanyAddress();
String tel = customer.getCompanyTel();
innerMap.put("Name", name);
innerMap.put("Address",address);
innerMap.put("Tel",tel);
LatLng latLng = new LatLng(latitude, longitude);
marker = map.addMarker(new MarkerOptions()
.position(latLng)
.title(name)
.icon(BitmapDescriptorFactory.defaultMarker(120)));
String markerID = marker.getId();
outerMap.put(markerID, innerMap);
Log.d(TAG3,"Create Marker: getName just saved: "+name);
Log.d(TAG3,"Create Marker: getMarkerID just created: "+markerID);
Log.d(TAG3,"Create Marker: getName jut saved in innerMap: "+innerMap.get("Name"));
HashMap<String, String> map = outerMap.get(markerID);
Log.d(TAG3,"Create Marker: get name just saved in innerMap and outerMap linked with marker just created: "+map.get("Name"));
}
}
}
答案 0 :(得分:0)
只需为每个innerMap
创建customer
的新实例。
删除此行:
private HashMap<String, String> innerMap = new HashMap<String, String>();
在您的setMarker()
方法中添加以下内容:
Map<String, String> innerMap = new HashMap<String, String>();
您的setMarker()
方法如下所示:
private void setMarker(){
for(Customer customer : customerList){
//...
Map<String, String> innerMap = new HashMap<String, String>(); // Add this line
innerMap.put("Name", name);
innerMap.put("Address",address);
innerMap.put("Tel",tel);
//...
}
}