Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

java - Android/Google Maps: How to get different info windows for different markers?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...