Android Mapview可绘制图标变形

时间:2012-09-14 01:20:02

标签: android android-mapview drawable

我从JSON文件解析一个地理点时有一个MapView,drawable看起来很正常但是当我解析地图集的所有地理位置时,可绘制的图标看起来很丑陋和不合适?会造成什么?我也在使用MapView Ballons Library。 MapView Ballons

已毁坏的地图引脚代码

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;
}}

法线贴图代码

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 个答案:

答案 0 :(得分:1)

每次向其添加项目时,都会反复将itemizedOverlay添加到mapview叠加层。

移动线:

mapOverlays.add(itemizedOverlay); 

在创建它之后,在启动Try Catch语句之前,它应该可以正常工作。