mapOverlay项目背景没有出现在旋转的mapview中?

时间:2012-10-16 10:47:16

标签: android android-mapview

您好我的应用程序正在使用mapview旋转地图。这意味着将mapview添加到rotateview类并使用sensormanager。在mapview中,在点击特定图钉后加载了很多图钉,它显示了该位置的名称在使用BallonItemizedOverlay类的覆盖项上。Doenot shows overlay item background

但是当我删除了旋转类时,它显示如下shows overlay item background]

这是我的rotateview类`

     public class RotateView extends ViewGroup implements SensorListener 

    {

    private static final float SQ2 = 1.414213562373095f;
    private final SmoothCanvas mCanvas = new SmoothCanvas();
    private float mHeading = 0;
    boolean check=false;

    public RotateView(Context context) {
        super(context);
    }

    public void onSensorChanged(int sensor, float[] values) {
        //Log.d(TAG, "x: " + values[0] + "y: " + values[1] + "z: " + values[2]);
        synchronized (this) {
            mHeading = values[0];

            if(!check)
            {
                if(Math.round(values[0])>0 || Math.round(values[0])<=90)
                {         

                  onGpsUpdate();
                  direction="NE";
                  GetPlacesDoInBack doinback=new GetPlacesDoInBack();
                  doinback.execute();
                }

                if(Math.round(values[0])>90 || Math.round(values[0])<=180)
                {         
                  check=true;
                  onGpsUpdate();
                  direction="SE";
                  GetPlacesDoInBack doinback=new GetPlacesDoInBack();
                  doinback.execute();

                }
                if(Math.round(values[0])>180 || Math.round(values[0])<=270)
                {   
                  check=true;

                  onGpsUpdate();
                  direction="SW";
                  GetPlacesDoInBack doinback=new GetPlacesDoInBack();
                  doinback.execute();

                }
                if(Math.round(values[0])>270 || Math.round(values[0])<=360)
                {   
                  check=true;   
                  onGpsUpdate();
                  direction="NW";
                  GetPlacesDoInBack doinback=new GetPlacesDoInBack();
                  doinback.execute();

                }

                check=true;
            }
            else
            {
            if(Math.round(values[0])>0)
            {                                   
             if(Math.round(values[0])==45)
              {                           
                onGpsUpdate();
                direction="NE";
                GetPlacesDoInBack doinback=new GetPlacesDoInBack();
                doinback.execute();

              }

              if(Math.round(values[0])==135)
              {       
                  check=true;
                  onGpsUpdate();
                  direction="SE";
                  GetPlacesDoInBack doinback=new GetPlacesDoInBack();
                  doinback.execute();

              }
              if(Math.round(values[0])==225)
              { 
                  check=true;                
                  onGpsUpdate();
                  direction="SW";
                  GetPlacesDoInBack doinback=new GetPlacesDoInBack();
                  doinback.execute();

              }
              if(Math.round(values[0])==315)
              { 
                  check=true;   
                  onGpsUpdate();
                  direction="NW";
                  GetPlacesDoInBack doinback=new GetPlacesDoInBack();
                  doinback.execute();

              }
            }
            }
            double lat=Double.parseDouble(lati);
            double lag=Double.parseDouble(longi);

            Location zeroLocation = new Location("");
            zeroLocation.setLatitude(lat);
            zeroLocation.setLongitude(lag);

            Location testLocation = new Location("");

            testLocation.setLatitude(Double.parseDouble("27.74993670"));
            testLocation.setLongitude(Double.parseDouble("85.37316799"));

            final float[] results= new float[3];
            // The computed distance in meters is stored in results[0].
            // If results has length 2 or greater, the initial bearing is stored in results[1].
            // If results has length 3 or greater, the final bearing is stored in results[2].
            Location.distanceBetween(lat, lag, Double.parseDouble("27.74993670"), Double.parseDouble("85.37316799"), results);

            final float bearing = results[1];

            double lat1=Double.parseDouble(lati);
            double lon1=Double.parseDouble(longi);

            double lat2=Double.parseDouble("27.74993670");
            double lon2=Double.parseDouble("85.37316799");

            double d1= Math.toRadians(lat2-lat1);
            double d2= Math.toRadians(lon2-lon1);

            lat1=Math.toRadians(lat1);
            lat2=Math.toRadians(lat2);

            double y=Math.sin(d2) * Math.cos(lat2);
            double x=Math.cos(lat1)*Math.sin(lat2) -
            Math.sin(lat1)*Math.cos(lat2)*Math.cos(d2);

            double  brng = Math.toDegrees(Math.atan2(y, x));
            brng=360-Math.abs(brng);

            // Log.e("","****"+brng+"***"+mHeading+"***"+(brng-mHeading));
            brng=360-Math.abs(zeroLocation.bearingTo(testLocation));

            //Log.e("",""+zeroLocation.bearingTo(testLocation)+"****"+bearing+"***"+(brng-mHeading)); 
            invalidate();
        }
    }

    class GetPlacesDoInBack extends AsyncTask<URL, Integer, Long>
    {
        int i=0;

        @Override
        protected Long doInBackground(URL... arg0) 
        {
            lati="35.227087";longi="-80.843127";
            response=UrltoValue.getValuefromUrl(DataUrls.nearbylocationsurl+"?lat="+lati+"&long="+longi+"&direction="+direction+"&id="+scatid);

            Log.e("Check",UrltoValue.getValuefromUrl(DataUrls.nearbylocationsurl+"?lat="+lati+"&long="+longi+"&direction="+direction+"&id="+scatid));
            Log.e("getplaces response", response);

                try
                {
                    JSONArray jarray=new JSONArray(response);
                    placesname=new String[jarray.length()];
                    placeslat=new String[jarray.length()];
                    placeslon=new String[jarray.length()];
                    placesaddr=new String[jarray.length()];
                    for(int i=0;i<jarray.length();i++)
                    {
                        placesname[i]=jarray.getJSONObject(i).getString("name");
                        placeslat[i]=jarray.getJSONObject(i).getString("latitude");
                        placeslon[i]=jarray.getJSONObject(i).getString("longitude");
                        placesaddr[i]=jarray.getJSONObject(i).getString("address");

                        Log.e("placesname",placesname[i]);Log.e("placeslat",placeslat[i]);
                        Log.e("placeslon",placeslon[i]);Log.e("placesaddr",placesaddr[i]);
                    }
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            return null;

        }

        protected void onPostExecute(Long result) 
        {   
            home.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if(click==0)
                    {
                        click=1;
                        headertext.setText("Categories");
                    home.setBackgroundResource(R.drawable.homeicon);
                    maplayout.setVisibility(View.GONE);
                    vlayout.setVisibility(View.VISIBLE);
                    }
                    else
                    {
                         BalloonItemizedOverlay.check=0;
                            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(v.getWindowToken(),0);
                            Intent in=new Intent(DNCActivity.this,Main.class);
                            startActivity(in);
                            finish();   
                    }
                }
            });
            if(response.trim().equals("[]"))
            {

            }
            else
            {
                try
                {
                    for (Overlay overlay : mapOverlays) {
                        if (overlay instanceof BalloonItemizedOverlay<?> ) {
                            if (((BalloonItemizedOverlay<?>) overlay).balloonView != null)
                                ((BalloonItemizedOverlay<?>) overlay).balloonView.setVisibility(View.GONE);
                        }
                    }           
                    mapOverlays.clear();
                    if(!mapOverlays.isEmpty())
                    {
                        mapOverlays.remove(itemizedOverlay);
                        mapView.invalidate();           
                        mapView.getOverlays().remove(mapOverlays);
                        mapOverlays.clear();            
                    }
                     for(int j=0;j<placeslon.length;j++)
                     {
                        String templat=placeslat[j];
                        String templon=placeslon[j];
                        String title="Name: "+placesname[j];
                        String park="Address: "+placesaddr[j];
                        mapLoad(templat,templon,title);
                     }

                }
                catch(Exception e)
                {

                }
            }
        }
    }

    @Override
    protected void dispatchDraw(Canvas canvas) 
    {
        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.rotate(-mHeading, getWidth() * 0.5f, getHeight() * 0.5f);
        mCanvas.delegate = canvas;
        super.dispatchDraw(mCanvas);
        canvas.restore();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) 
    {
        final int width = getWidth();
        final int height = getHeight();
        final int count = getChildCount();
        for (int i = 0; i < count; i++) 
        {
            final View view = getChildAt(i);
            final int childWidth = view.getMeasuredWidth();
            final int childHeight = view.getMeasuredHeight();
            final int childLeft = (width - childWidth) / 2;
            final int childTop = (height - childHeight) / 2;
            view.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
    {
        int w = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        int h = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        int sizeSpec;
        if (w > h) 
        {
            sizeSpec = MeasureSpec.makeMeasureSpec((int) (w * SQ2), MeasureSpec.EXACTLY);
        }
        else 
        {
            sizeSpec = MeasureSpec.makeMeasureSpec((int) (h * SQ2), MeasureSpec.EXACTLY);
        }
        final int count = getChildCount();
        for (int i = 0; i < count; i++) 
        {
            getChildAt(i).measure(sizeSpec, sizeSpec);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    public void onAccuracyChanged(int sensor, int accuracy) {

    }
}

` 如何在旋转的mapview中显示叠加项目背景..  任何人都可以帮助我 提前谢谢..

1 个答案:

答案 0 :(得分:0)

使用rotateview旋转mapview时,您必须通过覆盖dispatchTouchEvent()

来旋转触摸事件以及mapview

请参阅以下代码

 @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        float[] coords = new float[] {
                event.getX(), event.getY()
        };
        adjustCoords(coords, -mHeading);
        MotionEvent evt = MotionEvent.obtain(event.getDownTime(), event.getEventTime(), event
                .getAction(), coords[0], coords[1], event.getPressure(), event.getSize(), event
                .getMetaState(), event.getXPrecision(), event.getYPrecision(),    event.getDeviceId(),
                event.getEdgeFlags());
        return super.dispatchTouchEvent(evt);
    }
    protected void adjustCoords(float[] coords, float deg) {
        float x = coords[0];
        float y = coords[1];
        int centerX = getWidth() / 2;
        int centerY = getHeight();
        // convert to radians
        float rad = (float) ((deg * Math.PI) / 180F);
        float s = (float) Math.sin(rad);
        float c = (float) Math.cos(rad);
        // translate point back to origin:
        x -= centerX;
        y -= centerY;
        // apply rotation
        float tmpX = x * c - y * s;
        float tmpY = x * s + y * c;
        x = tmpX;
        y = tmpY;
        // translate point back:
        x += centerX;
        y += centerY;
        coords[0] = x;
        coords[1] = y;
    }