问题
This is my first time working with Google Maps
so sorry if this is a beginner problem. I am working on an app that connects to a portable sensor. The sensor sends pollution data every 2 minutes. Every time I get new data, a marker is placed on google maps and I want to show the data in the info window of the markers so that you can see how the pollution differs depending on where you are.
My problem is that as of now, all my markers info windows are updated with the newest data. I need to make it so that every marker has their own separate info window with unique data.
I think that I need to implement OnInfoWindowClickListener
somehow and also that I need the ID of every marker. I have tried to look at answers in other threads and so far I have not understood how I should solve this problem.
Appreciate any help I can get
This is my code right now.
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));
}
}
EDIT The data I want to show is "co_mV" and "no2_mV".
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
try {
JSONObject parser = new JSONObject(values[0]);
JSONObject d = parser.getJSONObject("d");
co_mV = d.getInt("co_mV");
no2_mV = d.getInt("no2_mV");
} catch (JSONException e) {
e.printStackTrace();
}
newData();
//response received from server
Log.d("CO2", values[0]);
long timeStamp = System.currentTimeMillis() / 1000L;
time=new java.util.Date((long)timeStamp*1000);
//process server response here....
}
}
回答1:
create a infowWindowAdapter like this
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;
}
}
first of all call YourCustomInfoWindowAdpater yourInfo=new YourCustomInfoWindowAdpater(this);
then set googleMap.setInfoWindowAdapter(yourInfo);
and as soon as you get data call setModels method of yourcustominfowindowadpater in which pass your data model
来源:https://stackoverflow.com/questions/43370131/android-google-maps-how-to-get-different-info-windows-for-different-markers