【发布时间】:2016-03-10 15:47:54
【问题描述】:
我想在这个哈希图中添加谷歌地图标记:
private HashMap<Marker, MyMarker> mMarkersHashMap;
不将它们添加到谷歌地图。这是我目前拥有的代码:
MarkerOptions markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
Marker currentMarker = mMap.addMarker(markerOption);
mMarkersHashMap.put(currentMarker, myMarker);
此代码将标记添加到哈希映射,但也将标记添加到映射(使用 addMarker),这是我不想要的。任何建议
这是我正在处理的功能:
private void plotMarkers(ArrayList<MyMarker> markers)
{
if(markers.size() > 0)
{
for (final MyMarker myMarker : markers)
{
MarkerOptions markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
Marker currentMarker = mMap.addMarker(markerOption);
//Passes markers into hashmap so they can be used by the information window methods
mMarkersHashMap.put(currentMarker, myMarker);
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Intent ListIntent = new Intent(getApplicationContext(), InfoWindowList.class);
MyMarker myMarker = mMarkersHashMap.get(marker);
String title = myMarker.getmLabel();
ListIntent.putExtra("COUNTY", title);
startActivity(ListIntent);
}
});
mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
}
}
mClusterManager.addItems(markers);
}
我希望mClusterManager.addItems(markers); 将标记添加到地图中。
这是我的信息窗口函数,它需要哈希图在哈希图中的标记上设置信息窗口:
public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter
{
public MarkerInfoWindowAdapter()
{
}
@Override
public View getInfoWindow(Marker marker)
{
return null;
}
@Override
public View getInfoContents(Marker marker)
{
View v = getLayoutInflater().inflate(R.layout.infowindow_layout, null);
MyMarker myMarker = mMarkersHashMap.get(marker);
ImageView markerIcon = (ImageView) v.findViewById(R.id.marker_icon);
TextView markerLabel = (TextView)v.findViewById(R.id.marker_label);
TextView anotherLabel = (TextView)v.findViewById(R.id.another_label);
//anotherLabel.setOnClickListener(newsfeed);
markerIcon.setImageResource(manageMarkerIcon(myMarker.getmIcon()));
anotherLabel.setText("Newsfeed");
markerLabel.setText(myMarker.getmLabel());
return v;
}
}
MyMarker 类:
public class MyMarker implements ClusterItem {
private String mLabel;
private String mIcon;
private Double mLatitude;
private Double mLongitude;
private final LatLng mPosition;
public MyMarker(String label, String icon, Double latitude, Double longitude)
{
this.mLabel = label;
this.mLatitude = latitude;
this.mLongitude = longitude;
this.mIcon = icon;
mPosition = new LatLng(latitude, longitude);
}
//mPosition = LatLng(mLatitude, mLongitude);
@Override
public LatLng getPosition() {
return mPosition;
}
public String getmLabel()
{
return mLabel;
}
public void setmLabel(String mLabel)
{
this.mLabel = mLabel;
}
public String getmIcon()
{
return mIcon;
}
public void setmIcon(String icon)
{
this.mIcon = icon;
}
public Double getmLatitude()
{
return mLatitude;
}
public void setmLatitude(Double mLatitude)
{
this.mLatitude = mLatitude;
}
public Double getmLongitude()
{
return mLongitude;
}
public void setmLongitude(Double mLongitude)
{
this.mLongitude = mLongitude;
}
}
【问题讨论】:
-
只是不要在地图上添加标记,只在 hashmap 上添加标记,您可以发布更多课程代码。
-
我知道这是我必须做的,我想知道它是如何完成的。是的,我现在将添加更多代码
标签: java android google-maps google-maps-api-3