2017-04-12 9 views
-1

Google Mapsで初めて作業していますので、初心者の問題であれば申し訳ありません。私はポータブルセンサーに接続するアプリケーションに取り組んでいます。センサーは2分ごとに汚染データを送信します。新しいデータが得られるたびに、マーカーがGoogleマップに配置され、マーカーの情報ウィンドウにデータを表示して、どこで汚染が異なるかを確認します。Android/Googleマップ:さまざまなマーカーに異なる情報ウィンドウを表示するにはどうすればよいですか?

私の問題は、今のところ、すべてのマーカー情報ウィンドウが最新のデータで更新されていることです。すべてのマーカーに固有のデータを持つ独自の情報ウィンドウがあるようにする必要があります。

何とかOnInfoWindowClickListenerを実装する必要があると思いますし、すべてのマーカーのIDが必要です。私は他のスレッドで答えを見ようとしていますが、これまでどのようにこの問題を解決すべきか分かりませんでした。

は私が

は、これが今の私のコードで取得することができます任意の助けに感謝します。私が表示したいデータが "co_mV" と "no2_mV" である

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。

 @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

すべてのコールYourCustomInfoWindowAdpater yourInfo=new YourCustomInfoWindowAdpater(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; 
} 
} 

ようinfowWindowAdapterを作成し、その後、すぐにあなたが合格したyourcustominfowindowadpaterのデータ呼setModels方法を得るようgoogleMap.setInfoWindowAdapter(yourInfo);

を設定し、あなたのデータモデル

+0

あなたの答えをありがとう。私はsetModelが何をしているのか理解していないのですか?私はcoとno2のデータを置くべき場所ですか? – Kspr

+0

また、place_distanceとは何ですか? – Kspr

+0

place_distanceは情報ウィンドウのレイアウトに関連するtext.itsを表示する情報ウィンドウのテキストビューです。そして、セットされたモデルは、あなたが新しいデータを持っているとすぐにあなたのデータを更新するために使用されます。そのモデルのデータ数は、データが入っているインフォウィンドウの番号に等しくなります。 –

関連する問題