2016-09-01 4 views
1

JSONレスポンスでWeb APIから受け取った緯度と経度に応じてマーカーをプロットします。MarkerカスタムInfoWindowのタイトルとスニペット以上

問題は、各マーカーにID、コード、電話番号、および名前などの独自のデータセットがあることです。このデータがカスタムInfoWindowAdapterを使用して各マーカーのInfoWindowに表示されたいのですが、JSONで受け取った関連データを表示できません。

これまで私がこれまでに試したことは次のとおりです。このコードでは、すべてのマーカースニペットは、同じデータが含まれている、と私は各マーカーについてこのスニペットに異なるデータを追加したい:

private class GetFixture extends AsyncTask<Void, Void, Void> { 


    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @Override 
    protected Void doInBackground(Void... arg) { 


     ServiceHandler serviceClient = new ServiceHandler(); 
     Log.d("url: ", "> " + URL_ITEMS); 
     String json = serviceClient.makeServiceCall(URL_ITEMS, ServiceHandler.GET); 
     // print the json response in the log 
     Log.d("Get match fixture resps", "> " + json); 
     if (json != null) { 
      try { 
       Log.d("try", "in the try"); 
       JSONArray jsonArray = new JSONArray(json); 
       Log.d("jsonObject", "new json Object"); 
       // Getting JSON Array node 
       for (int i = 0; i <= jsonArray.length(); i++) { 

        JSONObject c = jsonArray.getJSONObject(i); 
        Double matchId = Double.parseDouble(c.getString(TAG_MATCHID)); 
        Log.d("matchId", String.valueOf(matchId)); 
        Double teamA = Double.valueOf(c.getString(TAG_TEAMA)); 
        Log.d("teamA", String.valueOf(teamA)); 
        teamB = c.getString(TAG_TEAMB); 
        Log.d("teamB", teamB); 
        // hashmap for single match 

        HashMap<String, String> matchFixture = new HashMap<String, String>(); 
        HashMap<String, String> cc = new HashMap<String, String>(); 

        // adding each child node to HashMap key => value 
        matchFixture.put(TAG_MATCHID, String.valueOf(matchId)); 
        matchFixture.put(TAG_TEAMA, String.valueOf(teamA)); 
        areahash.put(TAG_TEAMB, teamB); 

        matchFixtureList.add(new LatLng(matchId, teamA)); 
        cc.put(TAG_TEAMB,teamB); 
        area.add(teamB); 
       } 
      } catch (JSONException e) { 
       Log.d("catch", "in the catch"); 
       e.printStackTrace(); 
      } 
     } else { 
      Log.e("JSON Data", "Didn't receive any data from server!"); 
     } 
     return null; 
    } 


    @Override 
    protected void onPostExecute(Void result) { 

     int x; 
     //for (LatLng point : matchFixtureList) { 
     //for (String a:area) 
     for (i = 0; i < matchFixtureList.size(); i++) { 

      mMap.addMarker(new MarkerOptions().position(matchFixtureList.get(i)).title(area.get(i))); 
      mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(matchFixtureList.get(i), 15)); 
      mMap.animateCamera(CameraUpdateFactory.zoomIn()); 



      // Zoom out to zoom level 10, animating with a duration of 2 seconds. 
      mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null); 
      if (mMap != null) { 
       mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { 
        @Override 
        public View getInfoWindow(Marker marker) { 
         return null; 
        } 

        @Override 
        public View getInfoContents(Marker marker) { 
         View v = getLayoutInflater().inflate(R.layout.check, null); 
         TextView ashid = (TextView) v.findViewById(R.id.ash_id); 
         TextView ashcode = (TextView) v.findViewById(R.id.ash_code); 
         TextView Cellno = (TextView) v.findViewById(R.id.cell_id); 
         TextView Ashname = (TextView) v.findViewById(R.id.ash_name); 

         TextView NIC = (TextView) v.findViewById(R.id.nic_id); 
         TextView tvlat = (TextView) v.findViewById(R.id.lat_id); 
         TextView tvLng = (TextView) v.findViewById(R.id.lonti_id); 
         TextView Zmid = (TextView) v.findViewById(R.id.ZM_id); 




         LatLng ll = marker.getPosition(); 
         ashid.setText(marker.getTitle()); 
         tvlat.setText(""+ll.latitude); 
         tvLng.setText("" + ll.longitude); 

         Zmid.setText("" + ll.longitude); 
         NIC.setText(""+ll.latitude); 
         Ashname.setText("" + ll.longitude); 
         Cellno.setText(""+ll.latitude); 
         ashid.setText("" + ll.longitude); 
         ashcode.setText(teamB); 

         ashcode.setText(marker.getSnippet()); 

         return v; 
        } 
       }); 


      } 

      // mMap.addMarker(new MarkerOptions().position(pakistan).title("Marker in pakistn")); 
      //mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
      // mMap.animateCamera(CameraUpdateFactory.zoomIn()); 
      // Zoom out to zoom level 10, animating with a duration of 2 seconds. 
      // mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null); 
      //mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(matchFixtureList(i))); 
      //mMap.animateCamera(CameraUpdateFactory.zoomTo(5)); 


     } 
    } 

image of snippet

答えて

1

あなただけのタイトルよりも多くのデータポイントを格納する必要がある場合と、スニペットでは、これを解決する最善の方法は、それぞれのMarkerのデータを格納するためにラッパークラスを使用し、InfoWindowAdapterで取得できるようにマーカーIDをキーとしてHashMapに格納されたデータをキーとして保持することです。

まず、(あなたは、必要に応じてより多くの情報を含めるには、この上で展開することができます)各Markerに対応した情報のためのホルダークラスを作成します。

public class MarkerHolder { 
    public String mAshId; 
    public String mAshCode; 
    public String mCellId; 
    public String mAshName; 

    public MarkerHolder(String ashId, String ashCode, String cellId, String ashName) { 
     mAshId = ashId; 
     mAshCode = ashCode; 
     mCellId = cellId; 
     mAshName = ashName; 
    } 
} 

その後に各マーカーIDをマッピングしますHashMap<String, MarkerHolder>を作成各Markerのためのデータ、及びその活動のインスタンス変数にする:あなたは、各マーカーを追加すると、THIに関連するすべてのデータを格納するMarkerHolderオブジェクトと同様にHashMapにエントリを追加し

HashMap<String, MarkerHolder> markerHolderMap = new HashMap<String, MarkerHolder>(); 

sのマーカー:私は確認してください私のすべてのコードの過去を少し私を理解してあなたの先生に感謝

mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { 

    @Override 
    public View getInfoWindow(Marker arg0) { 
     return null; 
    } 

    @Override 
    public View getInfoContents(Marker arg0) { 

     View v = getLayoutInflater().inflate(R.layout.check, null); 
     TextView ashid = (TextView) v.findViewById(R.id.ash_id); 
     TextView ashcode = (TextView) v.findViewById(R.id.ash_code); 
     TextView cellNo = (TextView) v.findViewById(R.id.cell_id); 
     TextView ashName = (TextView) v.findViewById(R.id.ash_name); 

     //Get the MarkerHolder for this Marker: 
     MarkerHolder markerHolder = markerHolderMap.get(arg0.getId()); 

     //Use the data to populate the layout: 
     ashid.setText(markerHolder.mAshId); 
     ashcode.setText(markerHolder.mAshCode); 
     cellNo.setText(markerHolder.mCellId); 
     ashName.setText(markerHolder.mAshName); 

     //........... 

     return v; 
    } 
}); 
+0

Marker marker = mMap.addMarker(new MarkerOptions().position(matchFixtureList.get(i)).title(area.get(i)).snippet("snipp works")); // Assuming these parameters come from your JSON: MarkerHolder mHolder = new MarkerHolder(ashId, ashCode, cellId, ashName); // Then add this holder reference to the HashMap markerHolderMap.put(marker.getId(), mHolder); 

その後、InfoWindowAdapterで、HashMapから入手したMarkerHolderから取得した情報を使用しますこれは私がこれらのことをするのを助けます。私の割り当てを教えてください。今日は最後の日付です –

+0

@naveedabbas私はこの例をあなたのコードに非常に近づけましたが、私はまた、この回答を可能な限り一般的なものにしておき、他の人も助けていきたいと思います。問題を解決するのに十分な情報がありますが、この例を拡張してJSONのすべてのデータを表示するだけです。 –

+0

@naveedabbasまた、もう1つ。ループ内で 'mMap.setInfoWindowAdapter()'を呼び出さないでください。一度呼び出されるだけです。 –

関連する問題