0

場所を表示するためにJSONデータをマップビューに取得するのではなく、空白のマップを取得して、JSONデータをマップビューにフェッチしようとするプログラムを作成していますが、アプリを実行するたびにJSONデータなしでマップを取得するので、問題と私が行う必要がある変更は何ですか。ここにコードを配置しています。参照してください:-

public class MapView extends MapActivity {

public GeoPoint point;
TapControlledMapView mapView; // use the custom TapControlledMapView
List<Overlay> mapOverlays;
Drawable drawable;
SimpleItemizedOverlay itemizedOverlay;

@Override
public void onCreate(final Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_view);

    mapView = (TapControlledMapView) findViewById(R.id.viewmap);
    mapView.setBuiltInZoomControls(true);
    mapView.setSatellite(false);

//   new DownloadWebPageTask().execute();


    // dismiss balloon upon single tap of MapView (iOS behavior) 
    mapView.setOnSingleTapListener(new OnSingleTapListener() {      


        public boolean onSingleTap(MotionEvent e) {
            itemizedOverlay.hideAllBalloons();
            return true;

        }
    });

    mapOverlays = mapView.getOverlays();        
    drawable = getResources().getDrawable(R.drawable.ic_launcher);
    itemizedOverlay = new SimpleItemizedOverlay(drawable, mapView);         
    itemizedOverlay.setShowClose(false);
    itemizedOverlay.setShowDisclosure(true);
    itemizedOverlay.setSnapToCenter(false);



    class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {

            HttpClient client = new DefaultHttpClient();
            // Perform a GET request for a JSON list
            HttpUriRequest request = new HttpGet("https://dl.***.com/maps.json");
            // Get the response that sends back
            HttpResponse response = null;
            try {
                response = client.execute(request);
            } catch (ClientProtocolException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            // Convert this response into a readable string
            String jsonString = null;
            try {
                jsonString = StreamUtils.convertToString(response.getEntity().getContent());
            } catch (IllegalStateException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            // Create a JSON object that we can use from the String
            JSONObject json = null;
            try {
                json = new JSONObject(jsonString);
            } catch (JSONException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }


            try{
                JSONArray jsonArray = json.getJSONArray("maps");
                Log.e("log_tag", "Opening JSON Array ");
                for(int i=0;i < jsonArray.length();i++){                      
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String latitude =  jsonObject.getString("latitude");
                    String longitude =  jsonObject.getString("longitude");
                    String title =  jsonObject.getString("title");
                    String country = jsonObject.getString("country");
                    double lat = Double.parseDouble(latitude);
                    double lng = Double.parseDouble(longitude);
                    Log.e("log_tag", "ADDING GEOPOINT"+title); 
                    point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
                    OverlayItem overlayItem = new OverlayItem
                        (point, title, country);
                    itemizedOverlay.addOverlay(overlayItem);
               }
           }catch(JSONException e)        {
               Log.e("log_tag", "Error parsing data "+e.toString());
           } 


           return jsonString;
       }

           @Override
        protected void onPostExecute(String result) {
                    itemizedOverlay.populateNow(); 

           mapOverlays.add(itemizedOverlay);
           if (savedInstanceState == null) {
               MapController controller = mapView.getController();
               controller.setCenter(point);
               controller.setZoom(7);
           } else {
               // example restoring focused state of overlays
               int focused;
               focused = savedInstanceState.getInt("focused_1", -1);
               if (focused >= 0) {
                   itemizedOverlay.setFocus(itemizedOverlay.getItem(focused));
               }
           }
              }

    }

    }
@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

    }

JSON データ

{
"maps": [
{

"title": "Place One",
"latitude" : "46.483742",
"longitude" : "7.663157",
"country": "Switzerland"
},
{
"title" : "Place Two",
"latitude" : "59.25235",
"longitude" : "18.465536",
"country" : "Sweden"
},
{
"title" : "Place Three",
"latitude" : "56.404182",
"longitude" : "-3.818855",
"country" : "Scotland"
}
]
}
4

1 に答える 1

0

SimpleItemizedOverlay をどこで取得しているのかはわかりませんが、ItemizedOverlay を拡張するカスタム実装であると想定しています。もしそうなら、そのソースも提供していただけますか?

SimpleItemizedOverlay ソースがなければ、私の提案は次のようになります。

  • コンストラクター内で呼び出しpopulate()ていることを確認します。itemizedOverlay
  • インスタンス化された直後にメソッドに移動mapOverlays.add(itemizedOverlay);します。現在の位置で、その後の ASyncTask の実行時にオーバーレイが再追加されます。onCreateitemizedOverlay
  • onPostExecute呼び出し内でitemizedOverlay.populate()mapView.postInvalidate()強制的に更新する

それ以外の場合は、SimpleItemizedOverlay にデバッグ メッセージを追加して、JSON データが正しく追加されていることを確認しますが、そのソースがなければ例を提供できません。

于 2012-10-27T18:55:06.987 に答える