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" +"
" + "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....
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…