2012-10-27 57 views
0

没有得到JSON数据到MapView的展示位置,但得到的空白地图, 我正在在我试图获取JSON数据到MapView的一个节目,但每当我跑我的应用程序越来越地图没有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" 
} 
] 
} 
+0

正在显示您的地图吗?总之没有得到JSON数据 –

+0

可以查看地图,但不能位置 – Stanley

+0

请人告诉我 – Stanley

回答

0

我不确定你在哪里SimpleItemizedOverlay,但我假设它是一个扩展了ItemizedOverlay的自定义实现。如果是这样,你能否提供该来源?

没有SimpleItemizedOverlay源我的建议是:

  • 验证你在你的itemizedOverlay它的构造函数中调用populate()
  • itemizedOverlay被实例化后立即将mapOverlays.add(itemizedOverlay);移动到您的onCreate方法中。在当前位置,重叠将在随后的ASyncTask运行中重新添加。
  • onPostExecute呼叫itemizedOverlay.populate()然后mapView.postInvalidate()强制刷新

否则我想补充调试消息SimpleItemizedOverlay来验证是否被正确添加的JSON数据,但是不来源,我不能提供一个例子。

+0

LWCoder,我们可以谈谈....吗? – Stanley