2012-10-16 47 views
2

嗨在我的應用程序工作在mapview,這是旋轉的地圖。,這意味着添加mapview到rotateview類,也使用sensormanager。在mapview我點擊特定的引腳後加載了很多引腳,它顯示了該位置的名稱對於覆蓋項目正在使用BallonItemizedOverlay類.. Doenot shows overlay item backgroundmapOverlay項目背景未出現在旋轉的mapview中?

但是,當我刪除了旋轉類它顯示像下面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) { 

    } 
} 

` 如何顯示在rotateed的MapView覆蓋項目背景.. 任何一個可以幫我請
在此先感謝..

回答

0

雖然與rotateview你必須通過覆蓋dispatchTouchEvent()

旋轉也隨之MapView的觸摸事件旋轉的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; 
    }