2012-09-14 239 views
0

從解析一個JSON文件中的一個地理點時,我有一個MapView drawable看起來很正常,但是當我解析一個mapset的所有geopoints時,drawable圖標看起來很醜並且沒有分配?這會造成什麼?我也使用MapView Ballons Library。 MapView BallonsAndroid Mapview可繪製圖標毀容

毀容地圖Pin碼

public class ViewAll extends MapActivity 

{ static final String KEY_MAPNAME = "name"; 
static final String KEY_MAPLOCATION = "address"; 
static final String KEY_MAPLONG = "longitude"; 
static final String KEY_MAPLAT = "latitude"; 
static final String KEY_MAPSETMAPSET = ""; 

TapControlledMapView mapView; // use the custom TapControlledMapView 
List<Overlay> mapOverlays; 
Drawable drawable; 

SimpleItemizedOverlay itemizedOverlay; 



; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.maps); 

Intent in = getIntent(); 
    String mapset = in.getStringExtra(KEY_MAPSETMAPSET); 

    mapView = (TapControlledMapView) findViewById(R.id.mapview); 
    mapView.setBuiltInZoomControls(true); 
    mapView.setSatellite(false); 
    // 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(); 

    // first overlay 
    drawable = getResources().getDrawable(R.drawable.marker2); 



    itemizedOverlay = new SimpleItemizedOverlay(drawable, mapView); 
    // set iOS behavior attributes for overlay 
    itemizedOverlay.setShowClose(false); 
    itemizedOverlay.setShowDisclosure(true); 
    itemizedOverlay.setSnapToCenter(false); 

try { 

      JSONObject jOBj; 
      JSONArray jsonArray; 
      JSONArray jarray=new JSONArray(mapset); 

      for(int j=0;j<jarray.length();j++){ 
       jOBj=jarray.getJSONObject(j); 
       jsonArray = jOBj.getJSONArray("locations"); 
        for(int i=0;i < jsonArray.length();i++){      



        JSONObject maps = jsonArray.getJSONObject(i); 

        String.valueOf(i); 



        String latitude = maps.getString("latitude"); 
        String longitude = maps.getString("longitude"); 
        String name = maps.getString("name"); 
        String address = maps.getString("address"); 

        double lat = Double.parseDouble(latitude); 
        double lng = Double.parseDouble(longitude); 
        GeoPoint point = new GeoPoint(
          (int) (lat * 1E6), 
          (int) (lng * 1E6)); 
        OverlayItem overlayItem = new OverlayItem(point, name, 
          address); 
        itemizedOverlay.addOverlay(overlayItem); 



        mapOverlays.add(itemizedOverlay); 

        // second overlay 


        if (savedInstanceState == null) { 

         final MapController mc = mapView.getController(); 
         mc.animateTo(point); 
         mc.setZoom(2); 

        } else { 

         // example restoring focused state of overlays 
         int focused; 
         focused = savedInstanceState.getInt("focused_1", -1); 
         if (focused >= 0) { 
          itemizedOverlay.setFocus(itemizedOverlay.getItem(focused)); 
         } 

         } 


      RadioGroup radGrp = (RadioGroup) findViewById(R.id.map_type); 
      int checkedRadioButtonID = radGrp.getCheckedRadioButtonId(); 
      radGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
       public void onCheckedChanged(RadioGroup arg0, int id) { 
       switch (id) { 

       case R.id.maps: 
        mapView.setSatellite(false); 
        break; 
       case R.id.satellite: 
        mapView.setSatellite(true); 
        break; 

       default: 
        mapView.setSatellite(false); 
        break; 
       } 
       } 
      }); } 













       } 




}catch(JSONException e)  { 
    Log.e("log_tag", "Error parsing data "+e.toString()); 
} 
} 


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

法線貼圖PIN碼

public class Maps extends MapActivity 



{ 
static final String KEY_MAPSETMAPSET = ""; 
static final String KEY_MAPNAME = "name"; 
static final String KEY_MAPLOCATION = "address"; 
static final String KEY_MAPLONG = "longitude"; 
static final String KEY_MAPLAT = "latitude"; 


TapControlledMapView mapView; // use the custom TapControlledMapView 
List<Overlay> mapOverlays; 
Drawable drawable; 
Drawable drawable2; 
SimpleItemizedOverlay itemizedOverlay; 
SimpleItemizedOverlay itemizedOverlay2; 




@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.maps); 

Intent in = getIntent(); 
    String name = in.getStringExtra(KEY_MAPNAME); 
    String address = in.getStringExtra(KEY_MAPLOCATION); 
    String longitude = in.getStringExtra(KEY_MAPLONG); 
    String latitude = in.getStringExtra(KEY_MAPLAT); 





    mapView = (TapControlledMapView) findViewById(R.id.mapview); 
     mapView.setBuiltInZoomControls(true); 
     mapView.setSatellite(false); 
     // 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(); 

     // first overlay 
     drawable = getResources().getDrawable(R.drawable.marker2); 



     itemizedOverlay = new SimpleItemizedOverlay(drawable, mapView); 
     // set iOS behavior attributes for overlay 
     itemizedOverlay.setShowClose(false); 
     itemizedOverlay.setShowDisclosure(true); 
     itemizedOverlay.setSnapToCenter(false); 
     double lat = Double.parseDouble(latitude); 
     double lng = Double.parseDouble(longitude); 

     GeoPoint point = new GeoPoint(
       (int) (lat * 1E6), 
       (int) (lng * 1E6)); 
     OverlayItem overlayItem = new OverlayItem(point, name, 
       address); 
     itemizedOverlay.addOverlay(overlayItem); 



     mapOverlays.add(itemizedOverlay); 

     // second overlay 


     if (savedInstanceState == null) { 

      final MapController mc = mapView.getController(); 
      mc.animateTo(point); 
      mc.setZoom(16); 

     } else { 

      // example restoring focused state of overlays 
      int focused; 
      focused = savedInstanceState.getInt("focused_1", -1); 
      if (focused >= 0) { 
       itemizedOverlay.setFocus(itemizedOverlay.getItem(focused)); 
      } 

      } 


RadioGroup radGrp = (RadioGroup) findViewById(R.id.map_type); 
int checkedRadioButtonID = radGrp.getCheckedRadioButtonId(); 
radGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
    public void onCheckedChanged(RadioGroup arg0, int id) { 
    switch (id) { 

    case R.id.maps: 
     mapView.setSatellite(false); 
     break; 
    case R.id.satellite: 
     mapView.setSatellite(true); 
     break; 

    default: 
     mapView.setSatellite(false); 
     break; 
    } 
    } 
}); } 







    @Override 
    protected boolean isRouteDisplayed() { 
     return false; 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 

     // example saving focused state of overlays 
     if (itemizedOverlay.getFocus() != null) outState.putInt("focused_1", itemizedOverlay.getLastFocusedIndex()); 
     super.onSaveInstanceState(outState); 

    } 



    } 

Normal Map Pin Disfigured Map Pin

+1

嗯很難說,試着移動這條線'mapOverlays.add(itemizedOverlay);'出for循環。 – dmon

回答

1

您反覆添加itemizedOve每次向它添加一個項目時,都會拖拽到mapview疊加層。

招行:

mapOverlays.add(itemizedOverlay); 

,只是它已被創建之後,你開始嘗試catch語句之前,應該很好地工作。