Android / Google地图:如何为不同的标记获取不同的信息窗口? [英] Android/Google Maps: How to get different info windows for different markers?

查看:88
本文介绍了Android / Google地图:如何为不同的标记获取不同的信息窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用 Google地图进行工作,所以如果这是一个初学者问题,那么很抱歉。我正在研究连接到便携式传感器的应用程序。传感器每2分钟发送一次污染数据。每当我获得新的数据时,标记会放在谷歌地图上,我希望在标记的信息窗口中显示数据,以便根据您的位置了解污染情况。



我的问题是,截至目前,我所有的标记信息窗口都会更新最新的数据。我需要做到这一点,以便每个标记都有自己独立的信息窗口和独特的数据。



我认为我需要以某种方式实现 OnInfoWindowClickListener ,并且我需要每个标记的ID。我试图在其他线程中查看答案,到目前为止我还没有理解我应该如何解决这个问题。



欣赏我可以获得的任何帮助



这是我的代码。

  public class MapsActivity扩展FragmentActivity实现OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {

....


/ *这里我们创建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。 ();
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;


});

}

....

@覆盖
public void onLocationChanged(位置位置){
$ b $如果(!initiateApp){
if(location.distanceTo(mLastLocation)< 20){
markerArrayList.get(markerArrayList.size() - 1).remove();
markerArrayList.remove(markerArrayList.size() - 1);
Toast.makeText(
getApplicationContext(),
阅读以接近上次阅读,替换上次阅读,Toast.LENGTH_SHORT).show(); ();
}
}

if(markerArrayList.size()== 3){
markerArrayList.get(0).remove();
markerArrayList.remove(0);
}

//放置当前位置标记
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()));


//移动地图相机

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));
}
}

编辑数据I想要显示的是co_mV和no2_mV。

  @Override 
protected void onProgressUpdate(String ...值){
super.onProgressUpdate(values);


尝试{
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();

//从服务器收到的响应
Log.d(CO2,值[0]);
long timeStamp = System.currentTimeMillis()/ 1000L;
time = new java.util.Date((long)timeStamp * 1000);




//在此处理服务器响应....

}

}


解决方案

创建一个像这样的infowWindowAdapter

  public class YourCustomInfoWindowAdpater实现GoogleMap.InfoWindowAdapter {
private final查看mymarkerview;
私有上下文上下文;
私人列表< YourModelClass> nearByModel;
私人位置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查看getInfoWindow(标记标记){
render(marker,mymarkerview);
返回mymarkerview;
}

查看v =((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))。inflate(R.layout.custominfowindow,空值);

//从标记中获取位置
LatLng latLng = marker.getPosition();
//获取对TextView的引用以设置纬度
/ * TextView tvLat =(TextView)v.findViewById(R.id.tv_lat);

//获取引用TextView设置经度
TextView tvLng =(TextView)v.findViewById(R.id.tv_lng);

//设置纬度
tvLat.setText(Latitude:+ latLng.latitude);

//设置经度
tvLng.setText(Longitude:+ latLng.longitude); * /

//返回包含InfoWindow内容的视图
return v;


private void render(Marker marker,View view){
TextView place_distance =(TextView)view.findViewById(R.id.place_distance);

//添加代码以设置所需值
//对于您的custominfowindow布局文件中的每个元素
}

public void setModels(List< ; YourModelclass> nearByModel){
this.nearByModel = nearByModel;


首先调用 YourCustomInfoWindowAdpater yourInfo = new YourCustomInfoWindowAdpater(this);



然后设置 googleMap.setInfoWindowAdapter(yourInfo);



只要您获得数据调用setModels方法的yourcustominfowindowadpater,其中传递您的数据模型

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....

    }

}

解决方案

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

这篇关于Android / Google地图:如何为不同的标记获取不同的信息窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆