2011-05-31 2 views
1

dans mon application j'ai besoin de dessiner des lignes sur la carte pendant que je marche, courir ou conduire. Pour obtenir l'emplacement, j'utilise la méthode OnLocationChanged de la classe LocattionListener. Dans la méthode OnLocationChanged, j'appelle la classe de dessin au trait. la classe est appelée imprimée dans le journal mais je n'obtiens pas de lignes sur ma carte. Aidez-moi, s'il vous plaît. Si vous avez de l'expérience à ce sujet ou des idées s'il vous plaît partager avec moi.comment dessiner la ligne sur la carte en android

code:

public void onCreate(Bundle savedInstanceState) 
    { 
     ....... 
     myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,new myLocationListener()); 
    } 
    ...... 
    class myLocationListener implements LocationListener { 
     public void onLocationChanged(Location loc) { 
        Log.e("status","begin"); 
     Toast.makeText(getBaseContext(),"onStatusChanged - called",Toast.LENGTH_SHORT).show(); 
     Log.e("MAP","onStatusChanged - called"); 
     LocationManager myManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     if(myManager != null){ 
      String param = (String)myManager.getProviders(true).get(0); 
      Location loc1 = myManager.getLastKnownLocation(param); 
      if(loc1 != null){ 
       latPointDst = loc1.getLatitude(); 
       lngPointDst = loc1.getLongitude(); 
       tolat=latPointDst; 
       tolng=latPointDst; 
       GeoPoint tmp2= new GeoPoint((int) (tolat * 1E6), (int) (tolng * 1E6)); 
       drawingmethod(tmp2);      
      } 
      else 
        Log.e("Err-2","Error: Location is null"); 
     } 
     else 
      Log.e("Err-2","Error: Location Manager is null");    
     Log.e("status","end"); 
    } 
    }  
} 

public void drawingmethod(GeoPoint g2) { 
    geo.add(g2); 
    mc = mapView.getController(); 
    mapOverlays = mapView.getOverlays();   
    projection = mapView.getProjection(); 
    Iterator<GeoPoint> itr = geo.listIterator(); 
    while(itr.hasNext()){ 
     if(itr.hasNext()){ 
      p=itr.next(); 
     } 
     if(itr.hasNext()){ 
      p1=itr.next(); 
     } 
     mapOverlays.add(new MyOverlay(p.getLatitudeE6(),p.getLongitudeE6(),p1.getLatitudeE6(),p1.getLongitudeE6())); 
    } 
} 

// classe de dessin de ligne

public class MyOverlay extends Overlay { 
    private GeoPoint gp1; 
    private GeoPoint gp2; 

    public MyOverlay(int fromlatE6,int fromlonE6,int tolatE6,int tologE6) {   

     int flat=0,flog=0,tlat=0,tlog=0; 
     flat=fromlatE6; 
     flog=fromlonE6; 
     tlat=tolatE6; 
     tlog=tologE6;      
     gp1 = new GeoPoint(flat,flog); 
     gp2 = new GeoPoint(tlat,tlog); 
    } 

    @Override 
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow,long when) { 
     Projection projection = mapView.getProjection(); 
     if (shadow == false) { 
      Paint paint = new Paint(); 
      paint.setAntiAlias(true); 
      Point point = new Point(); 
      projection.toPixels(gp1, point); 
      paint.setColor(Color.RED); 
      Point point2 = new Point(); 
      projection.toPixels(gp2, point2); 
      paint.setStrokeWidth(3); 
      Log.e("location change","drawing"); 
      canvas.drawLine((float) point.x, (float) point.y, (float) point2.x,(float) point2.y, paint); 
      Log.e("map","draw2"); 
     } 
     return super.draw(canvas, mapView, shadow, when); 
    } 
    @Override 
    public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
     // TODO Auto-generated method stub 
     super.draw(canvas, mapView, shadow); 
     Log.e("map","draw1"); 
    } 
} 

Répondre

2

vous devriez obtenir la liste des coordonnées entre la source et la destination en appelant google api.

alors vous pouvez tracer la ligne. Refer this

Questions connexes