Java Android/Google地图:如何为不同的标记获取不同的信息窗口?
这是我第一次使用Java Android/Google地图:如何为不同的标记获取不同的信息窗口?,java,android,google-maps,onclicklistener,infowindow,Java,Android,Google Maps,Onclicklistener,Infowindow,这是我第一次使用谷歌地图,如果这是一个初学者的问题,很抱歉。我正在开发一个连接到便携式传感器的应用程序。传感器每2分钟发送一次污染数据。每次我得到新的数据时,谷歌地图上都会放置一个标记,我想在标记的信息窗口中显示这些数据,这样你就可以看到污染是如何随位置的不同而不同的 我的问题是,到目前为止,我所有的标记信息窗口都更新了最新的数据。我需要这样做,每个标记都有自己的信息窗口和唯一的数据 我认为我需要以某种方式实现OnInfo WindowClickListener,还需要每个标记的ID。我试着在其
谷歌地图
,如果这是一个初学者的问题,很抱歉。我正在开发一个连接到便携式传感器的应用程序。传感器每2分钟发送一次污染数据。每次我得到新的数据时,谷歌地图上都会放置一个标记,我想在标记的信息窗口中显示这些数据,这样你就可以看到污染是如何随位置的不同而不同的
我的问题是,到目前为止,我所有的标记信息窗口都更新了最新的数据。我需要这样做,每个标记都有自己的信息窗口和唯一的数据
我认为我需要以某种方式实现OnInfo WindowClickListener,还需要每个标记的ID。我试着在其他线程中寻找答案,但到目前为止我还不明白该如何解决这个问题
谢谢你给我的任何帮助
这是我现在的代码。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
....
/* Here we create the infoWindow **/
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
public View getInfoWindow(Marker arg0) {
View v = getLayoutInflater().inflate(R.layout.custom_infowindow, null);
TextView tv = (TextView) v.findViewById(R.id.infoText);
tv.setText("CO2 data: "+String.valueOf(co_mV) + "mV" +"\n" + "N2 data: "+String.valueOf(no2_mV) +" mV");
return v;
}
public View getInfoContents(Marker arg0) {
return null;
}
});
}
....
@Override
public void onLocationChanged(Location location) {
if (!initiateApp) {
if(location.distanceTo(mLastLocation) < 20) {
markerArrayList.get(markerArrayList.size()-1).remove();
markerArrayList.remove(markerArrayList.size()-1);
Toast.makeText(
getApplicationContext(),
"Reading to close to last reading, replace last reading", Toast.LENGTH_SHORT).show();
}
}
if (markerArrayList.size() == 3) {
markerArrayList.get(0).remove();
markerArrayList.remove(0);
}
//Place current location marker
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
mCurrLocationMarker = mMap.addMarker(markerOptions);
markerArrayList.add(mCurrLocationMarker);
mLastLocation = location;
Log.d("ADebugTag", "Value: " + Double.toString(location.getLatitude()));
Log.d("ADebugTag", "Value: " + Double.toString(location.getLongitude()));
//move map camera
if(initiateApp){
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
}
initiateApp = false;
boolean contains = mMap.getProjection()
.getVisibleRegion()
.latLngBounds
.contains(latLng);
if(!contains){
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
像这样创建一个infowWindowAdapter
public class YourCustomInfoWindowAdpater implements GoogleMap.InfoWindowAdapter {
private final View mymarkerview;
private Context context;
private List<YourModelClass> nearByModel;
private Location currentMarker;
public YourCustomInfoWindowAdpater(Context context) {
this.context = context;
currentMarker = new Location();
mymarkerview = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.custominfowindow, null);
}
public View getInfoWindow(Marker marker) {
render(marker, mymarkerview);
return mymarkerview;
}
public View getInfoContents(Marker marker) {
View v = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custominfowindow, null);
// Getting the position from the marker
LatLng latLng = marker.getPosition();
// Getting reference to the TextView to set latitude
/* TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);
// Getting reference to the TextView to set longitude
TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);
// Setting the latitude
tvLat.setText("Latitude:" + latLng.latitude);
// Setting the longitude
tvLng.setText("Longitude:"+ latLng.longitude);*/
// Returning the view containing InfoWindow contents
return v;
}
private void render(Marker marker, View view) {
TextView place_distance = (TextView) view.findViewById(R.id.place_distance);
// Add the code to set the required values
// for each element in your custominfowindow layout file
}
public void setModels(List<YourModelclass> nearByModel) {
this.nearByModel = nearByModel;
}
}
public类YourCustomInfoWindowAdapter实现GoogleMap.InfoWindowAdapter{
私有最终视图mymarkerview;
私人语境;
私有列表模型;
私人位置标记;
公共YourCustomInfoWindowAdPath(上下文){
this.context=上下文;
currentMarker=新位置();
mymarkerview=((LayoutFlater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE))
.充气(R.layout.custominfowindow,空);
}
公共视图getInfoWindow(标记器){
渲染(marker,mymarkerview);
返回mymarkerview;
}
公共视图getInfoContents(标记器){
视图v=((LayoutInflater)context.getSystemService(context.LAYOUT\u充气器\u SERVICE)).inflate(R.LAYOUT.custominfowindow,null);
//从标记器中获取位置
LatLng LatLng=marker.getPosition();
//获取对TextView的引用以设置纬度
/*TextView tvLat=(TextView)v.findviewbyd(R.id.tv_lat);
//获取对TextView的引用以设置经度
TextView tvLng=(TextView)v.findViewById(R.id.tv_lng);
//设置纬度
tvLat.setText(“纬度:+latLng.Latitude”);
//设置经度
tvLng.setText(“经度:”+latLng.Longitude)*/
//返回包含InfoWindow内容的视图
返回v;
}
专用空心渲染(标记、视图){
TextView地点距离=(TextView)view.findViewById(R.id.place\u距离);
//添加代码以设置所需的值
//对于custominfowindow布局文件中的每个元素
}
公共void集合模型(列表nearByModel){
this.nearByModel=nearByModel;
}
}
首先,调用YourCustomInfoWindowAdpater yourInfo=new YourCustomInfoWindowAdpater(此)代码>
然后设置googleMap.setInfoWindowAdapter(yourInfo)代码>
一旦您获得数据,请致电CustomInfoWindowADPATER的setModels方法,通过该方法传递您的数据模型谢谢您的回答。我不明白setModels做什么?这是我应该放co和no2数据的地方吗?还有什么是位置距离?位置距离是信息窗口中的文本视图,显示相关文本。它在信息窗口的布局中。一旦您有了新数据,设置模型将用于更新您的数据,该模型中的数据数量等于infowindow with dataI的编号我已编辑了在地图活动中返回数据的方法,我将显示的数据是2个整数,co_mV和no2_mV。我将最多有6个标记存储在ArrayList中。如果数组的大小==6,我将删除最旧的标记。然后我将如何定义列表?然后在该方法中传递该arrayList,并将相关变量声明为arrayList,作为相应的赋值
public class YourCustomInfoWindowAdpater implements GoogleMap.InfoWindowAdapter {
private final View mymarkerview;
private Context context;
private List<YourModelClass> nearByModel;
private Location currentMarker;
public YourCustomInfoWindowAdpater(Context context) {
this.context = context;
currentMarker = new Location();
mymarkerview = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.custominfowindow, null);
}
public View getInfoWindow(Marker marker) {
render(marker, mymarkerview);
return mymarkerview;
}
public View getInfoContents(Marker marker) {
View v = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custominfowindow, null);
// Getting the position from the marker
LatLng latLng = marker.getPosition();
// Getting reference to the TextView to set latitude
/* TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);
// Getting reference to the TextView to set longitude
TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);
// Setting the latitude
tvLat.setText("Latitude:" + latLng.latitude);
// Setting the longitude
tvLng.setText("Longitude:"+ latLng.longitude);*/
// Returning the view containing InfoWindow contents
return v;
}
private void render(Marker marker, View view) {
TextView place_distance = (TextView) view.findViewById(R.id.place_distance);
// Add the code to set the required values
// for each element in your custominfowindow layout file
}
public void setModels(List<YourModelclass> nearByModel) {
this.nearByModel = nearByModel;
}
}