Android / Google地图:如何为不同的标记获取不同的信息窗口?

时间:2017-04-12 12:53:38

标签: java android google-maps onclicklistener infowindow

这是我第一次使用Google Maps,如果这是一个初学者问题,那就很抱歉。我正在开发一个连接便携式传感器的应用程序。传感器每2分钟发送一次污染数据。每次我获得新数据时,都会在谷歌地图上放置一个标记,我想在标记的信息窗口中显示数据,以便您可以根据自己的位置了解污染情况。

我的问题是,截至目前,我的所有标记信息窗口都使用最新数据进行更新。我需要这样做,以便每个标记都有自己独立的信息窗口和唯一的数据。

我认为我需要以某种方式实现OnInfoWindowClickListener,并且我需要每个标记的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));
    }
}

编辑我想要展示的数据是&#34; co_mV&#34;和&#34; no2_mV&#34;。

        @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 个答案:

答案 0 :(得分:0)

像这样创建一个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;
}
}

首先致电YourCustomInfoWindowAdpater yourInfo=new YourCustomInfoWindowAdpater(this);

然后设置googleMap.setInfoWindowAdapter(yourInfo);

并且一旦你获得数据调用你的custominfowindowadpater的setModels方法,其中传递你的数据模型