2017-04-12 90 views
-1

这是我第一次与Google Maps合作,所以很抱歉,如果这是一个初学者的问题。我正在研究连接到便携式传感器的应用程序。传感器每2分钟发送一次污染数据。每次获取新数据时,都会在谷歌地图上放置一个标记,我想在标记的信息窗口中显示数据,以便您可以根据自己的位置了解污染情况。Android/Google地图:如何获取不同标记的不同信息窗口?

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

我认为我需要以某种方式实现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)); 
    } 
} 

编辑我要显示的数据是 “co_mV” 和 “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.... 

    } 

} 

回答

0

创建这样

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

,一旦你得到yourcustominfowindowadpater的数据呼叫setModels方法,其中传递的infowWindowAdapter您的数据模型

+0

谢谢你的回答。我不明白setModels是做什么的?那我应该把co和no2数据放在哪里? – Kspr

+0

什么是place_distance? – Kspr

+0

place_distance是信息窗口中的文本视图,用于在信息窗口的布局中显示相关的text.its。并且一旦有新数据,集模型将用于更新您的数据,该模型中的数据数量等于infowindow数据的数量 –

相关问题