2017-04-12 44 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數據的數量 –

相關問題