问题描述
所以我的应用程序要求我为某个总线创建一个路由。我完成了这一工作,并为标记创建了一个自定义信息窗口。现在,我被要求添加一个功能,在该功能中我需要在标记周围的地图POI(兴趣点)上显示。我已经成功创建了这个。但是,我想为这些POI标记提供一个不同的信息窗口。这是我的第一个信息窗口:
public class InfoAdapter实现GoogleMap.InfoWindowAdapter {
LayoutInflater inflater = null;
private TextView textViewstopName;
私人TextView到达时间;
public InfoAdapter(LayoutInflater inflater){
this.inflater = inflater;
}
@Override
public View getInfoWindow(Marker marker){
View v = inflater.inflate(R.layout.businfo_layout,null);
if(marker!= null){
textViewstopName =(TextView)v.findViewById(R.id.businfo);
textViewstopName.setText(marker.getTitle());
arrivalTime =(TextView)v.findViewById(R.id.arrivalinfo);
arrivalTime.setText(marker.getSnippet());
}
return(v);
}
@Override
public View getInfoContents(Marker marker){
return(null);
$ / code $ / pre
这里是我的第二个(我想要现在为POI默认一个): public class PlacesAdapter implements GoogleMap.InfoWindowAdapter {
LayoutInflater inflater = null ;
public PlacesAdapter(LayoutInflater inflater){
this.inflater = inflater;
}
@Override
public View getInfoWindow(Marker marker){
return null;
}
@Override
public View getInfoContents(Marker marker){
return null;
这是我称之为第一个的地方: p>
private void SetupStopMarkers(){
map.setInfoWindowAdapter(new InfoAdapter(getLayoutInflater()));
addMarkersToMap(markerPoints);
$ / code>
这里是我称之为第二个的地方:
else {
map.setInfoWindowAdapter(new PlacesAdapter(getLayoutInflater()));
...
}
我的info_layout:
< RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width = match_parent
android:layout_height =match_parent
android:orientation =horizontal
android:weightSum =1
android:background =@ drawable / businfo_dialog >
< ImageView
android:id =@ + id / busicon
android:layout_width =30dp
android:layout_height =30dp
android:src =@ drawable / busstop_icon/>
< ImageView
android:id =@ + id / clockicon
android:layout_width =30dp
android:layout_height =30dp layout_below =@ + id / busicon
android:layout_alignParentLeft =true
android:paddingLeft =5dp
android:src =@ drawable / clock
android :paddingBottom来= 10dp/>
TextView
android:id =@ + id / businfo
android:layout_width =wrap_content
android:layout_height =wrap_content
android:textColor =#FFFFFFFF
android:drawablePadding =10dp
android:layout_centerVertical =true
android:layout_toRightOf =@ + id / busicon
android:layout_toEndOf =@ + id / busicon
android:paddingRight =10dp
android:paddingTop =5dp
android:layout_alignParentTop =true/>
TextView
android:id =@ + id / arrivalinfo
android:layout_width =wrap_content
android:layout_height =wrap_content
android:textColor =#FFFFFFFF
android:drawablePadding =10dp
android:layout_centerVertical =true
android:layout_below =@ + id / businfo
android:layout_toRightOf =@ + id / clockicon
android:layout_toEndOf =@ + id / clockicon
android:paddingRight =10dp
android:paddingTop =5dp/ >
android:id =@ + id / placesinfo
android:layout_width =wrap_content
android:layout_height =wrap_content
android: layout_below =@ id / arrivalinfo
android:paddingTop =5dp
android:paddingBottom =35dp
android:paddingLeft =10dp
android:paddingRight = 10dp
android:textColor =#0099cc
android:layout_centerVertical =true
android:layout_centerHorizontal =true
android:text =@ string / more_info />
< TextView
android:id =@ + id / eta_i
android:layout_width =wrap_content
android:layout_height =wrap_content
android: layout_toRightOf =@ + id / arrivalinfo
android:layout_toEndOf =@ + id / arrivalinfo
android:paddingLeft =5dp
android:paddingTop =5dp
android:layout_alignTop =@ + id / arrivalinfo
android:text =ETA:
android:textStyle =bold
android:textColor =#FFFFFFFF/>
< / RelativeLayout>
但是在调用第二个信息窗口之后,地图中的每个标记都变为第二个信息窗口。有没有办法做到这一点?任何帮助表示感谢。
解决方案明白了。所以要获得多个Info窗口,我必须得到与标记对应的标记ID。所以我创建了一个ArrayList,它接受标记的id。
ArrayList< String> markerPlaces = new ArrayList<>();
然后我将这些标记添加到地图中填充它:
Marker marker = map.addMarker(new MarkerOptions()。position(position)
.title(venuesfound.get(i).getName())
.snippet(\\\
Open:+ venuesfound.get(i).getOpenNow()
+\\\
(+ venuesfound.get(i).getCategory()+) )
.icon(BitmapDescriptorFactory.fromResource(R.drawable.measle_blue)));
markerPlaces.add(marker.getId());
然后在InfoAdapter上,我添加了一个条件,如果标记ID在ArrayList中,I创建,然后放入不同的inflater。
public class InfoAdapter implements GoogleMap.InfoWindowAdapter {
LayoutInflater inflater = null;
private TextView textViewstopName;
私人TextView到达时间;
public InfoAdapter(LayoutInflater inflater){
this.inflater = inflater; (标记符!=空){
if(markerPlaces.containsKey(marker.getId())
@Override
public View getInfoWindow(Marker marker){
if )){
... //在这里添加新的充气器。
} //检查标记是否是位置标记或POI标记的一部分。
else {
View v = inflater.inflate(R.layout.businfo_layout,null);
textViewstopName =(TextView)v.findViewById(R.id.businfo);
textViewstopName.setText(marker.getTitle());
arrivalTime =(TextView)v.findViewById(R.id.arrivalinfo);
arrivalTime.setText(marker.getSnippet());
return(v);
}
}
返回null;
}
@Override
public View getInfoContents(Marker marker){
return(null);
}
}
谢谢大家的帮助!这当然让我想到了!
So my application requires me to create a route for a certain bus. I have accomplished this and have created a custom info window for the marker. Now, I was asked to add a feature in which I need to show on the map POIs (points of interests) around the marker. I have successfully created this. However, I want a different Info window for these POI markers.
Here is my first info window:
public class InfoAdapter implements GoogleMap.InfoWindowAdapter {
LayoutInflater inflater = null;
private TextView textViewstopName;
private TextView arrivalTime;
public InfoAdapter(LayoutInflater inflater) {
this.inflater = inflater;
}
@Override
public View getInfoWindow(Marker marker) {
View v = inflater.inflate(R.layout.businfo_layout, null);
if (marker != null) {
textViewstopName = (TextView) v.findViewById(R.id.businfo);
textViewstopName.setText(marker.getTitle());
arrivalTime = (TextView) v.findViewById(R.id.arrivalinfo);
arrivalTime.setText(marker.getSnippet());
}
return (v);
}
@Override
public View getInfoContents(Marker marker) {
return (null);
}
}
And here is my second one (I wanted to have to default one for the POIs for now):
public class PlacesAdapter implements GoogleMap.InfoWindowAdapter{
LayoutInflater inflater = null;
public PlacesAdapter(LayoutInflater inflater) {
this.inflater = inflater;
}
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
return null;
}
}
Here is where I called the first one:
private void SetupStopMarkers(){
map.setInfoWindowAdapter(new InfoAdapter(getLayoutInflater()));
addMarkersToMap(markerPoints);
}
Here is where I called the second one:
else{
map.setInfoWindowAdapter(new PlacesAdapter(getLayoutInflater()));
...
}
My info_layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1"
android:background="@drawable/businfo_dialog">
<ImageView
android:id="@+id/busicon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/busstop_icon"/>
<ImageView
android:id="@+id/clockicon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="@+id/busicon"
android:layout_alignParentLeft="true"
android:paddingLeft="5dp"
android:src="@drawable/clock"
android:paddingBottom="10dp"/>
<TextView
android:id="@+id/businfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:drawablePadding="10dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/busicon"
android:layout_toEndOf="@+id/busicon"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/arrivalinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:drawablePadding="10dp"
android:layout_centerVertical="true"
android:layout_below="@+id/businfo"
android:layout_toRightOf="@+id/clockicon"
android:layout_toEndOf="@+id/clockicon"
android:paddingRight="10dp"
android:paddingTop="5dp"/>
<TextView
android:id="@+id/placesinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/arrivalinfo"
android:paddingTop="5dp"
android:paddingBottom="35dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#0099cc"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="@string/more_info"/>
<TextView
android:id="@+id/eta_i"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/arrivalinfo"
android:layout_toEndOf="@+id/arrivalinfo"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:layout_alignTop="@+id/arrivalinfo"
android:text="ETA:"
android:textStyle="bold"
android:textColor="#FFFFFFFF"/>
</RelativeLayout>
But after calling the second info window, every marker in the map changes to that second info window. Is there a way to do this? Any help is appreciated.
解决方案 Got it. So to get multiple Info windows, I had to get the marker ids corresponding to the markers. So I created a ArrayList that takes in the "id" of the marker.
ArrayList<String> markerPlaces = new ArrayList<>();
I then populated it as I add the markers to the map:
Marker marker = map.addMarker(new MarkerOptions().position(position)
.title(venuesfound.get(i).getName())
.snippet("\nOpen: " + venuesfound.get(i).getOpenNow()
+ "\n(" + venuesfound.get(i).getCategory() + ")")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.measle_blue)));
markerPlaces.add(marker.getId());
Then on the InfoAdapter, I just added a condition that if the marker id is in the ArrayList that I created, then put in a different inflater.
public class InfoAdapter implements GoogleMap.InfoWindowAdapter {
LayoutInflater inflater = null;
private TextView textViewstopName;
private TextView arrivalTime;
public InfoAdapter(LayoutInflater inflater) {
this.inflater = inflater;
}
@Override
public View getInfoWindow(Marker marker) {
if (marker != null) {
if(markerPlaces.containsKey(marker.getId())) {
... //Add new inflater here.
} //checks if the marker is part of the Position marker or POI marker.
else{
View v = inflater.inflate(R.layout.businfo_layout, null);
textViewstopName = (TextView) v.findViewById(R.id.businfo);
textViewstopName.setText(marker.getTitle());
arrivalTime = (TextView) v.findViewById(R.id.arrivalinfo);
arrivalTime.setText(marker.getSnippet());
return (v);
}
}
return null;
}
@Override
public View getInfoContents(Marker marker) {
return (null);
}
}
Thank you all for the help! It certainly got me thinking!
这篇关于多个InfoWindows Android Maps V2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!