对Android中的Overlay draw的理解

Linux大全评论306 views阅读模式

修改同学的一个程序,真是让我抓狂,重载的draw只在程序启动的时候调用了,本来依照自己的经验,应该是可以很快定位的,可是调了半天,就是开始没感觉了。开始以为,直接new 一个Overlay就会执行的,不过这个想法很快被自己否决了。真的怪自己思路不够严谨,然后之前对draw的机制不是很清楚。然后就先找出api研究下

  1. public void draw(Android.graphics.Canvas canvas,  
  2.                  MapView mapView,  
  3.                  boolean shadow)  
  4. Draw the overlay over the map. This will be called on all active overlays with shadow=true, to lay down the shadow layer, and then again on all overlays with shadow=false. By default, draws nothing.  
  5. Parameters:  
  6. canvas - The Canvas upon which to draw. Note that this may already have a transformation applied, so be sure to leave it the way you found it.  
  7. mapView - the MapView that requested the draw. Use MapView.getProjection() to convert between on-screen pixels and latitude/longitude pairs.  
  8. shadow - If true, draw the shadow layer. If false, draw the overlay contents.  

这个是api里的说明,这下我注意到了mapView相到必定是当overlay的对象添加到mapview是才执行的。带着这个思路,我很快发现继承的Overlay的确有大问题。他尽然采用了一个自己定义的set方法,目的是给Overlay对象赋值。悲剧啊。不过下面的调试还是有问题的,其实不想也知道了,那就是那个draw只是跑了一遍,很显然这里不是主要问题。下面就是

  1. @Override  
  2. public void onLocationChanged(Location location) {  
  3.     // TODO Auto-generated method stub   
  4.   
  5.     System.out.println("3");  
  6.     if (null!=location) {  
  7.         pointNum++;  
  8.         Log.e("newpath""添加00");  
  9.         myPath=new PathOverlay();  
  10.           
  11.         List<Overlay> pointOverlays=  
  12.             mMap.getOverlays();//.add(myPath);   
  13.         pointOverlays.clear();  
  14.         pointOverlays.add(myPath);  
  15.         addPointFromLac(location);  
  16.                        mMapOverlay.clear();  
  17.         updateWithNewLocation(location,point);  
  18.     }     
  19.           
  20. }  

  1. List<Overlay> mMapOverlay;  //代码中这个是全局变量   
  2. mMapOverlay=mMap.getOverlays();//这是一段写在oncreate中的代码  

问题就在这两段代码中,这样的结果很显然,在onLocationChanged中,mMap.getOverlays()的数据被无辜的清空了,唉,

  1. @Override  
  2. public void onLocationChanged(Location location) {  
  3.     // TODO Auto-generated method stub  
  4.   
  5.       
  6.     if (null!=location) {  
  7.         pointNum++;  
  8.     myPath=new PathOverlay();  
  9.           
  10.         List<Overlay> pointOverlays=  
  11.             mMap.getOverlays();//  
  12.         pointOverlays.clear();  
  13.         pointOverlays.add(myPath);  
  14.         addPointFromLac(location);  
  15.         updateWithNewLocation(location,point);  
  16.     }     
  17.           
  18. }  
  1. onLocationChanged  
  1. onLocationChanged  

企鹅博客
  • 本文由 发表于 2020年6月26日 16:15:24
  • 转载请务必保留本文链接:https://www.qieseo.com/171376.html

发表评论