如何在android中的地图上画线

发布于 2024-11-10 17:57:19 字数 3537 浏览 4 评论 0原文

在我的应用程序中,我需要在走路、跑步或开车时在地图上画线。为了获取位置,我使用 LocationListener 类的 OnLocationChanged 方法。在 OnLocationChanged 方法中我调用线条绘制类。该类被称为它打印在日志中,但我在地图上没有看到任何线条。请帮我。如果您有这方面的经验或想法,请与我分享。

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

// 画线类

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

in my app i need to draw lines on map while i am walking,running or driving. for get location i use OnLocationChanged method of LocattionListener class. in OnLocationChanged method i call line drawing class. the class is called it printed in log but i do not get no lines on my map. please help me. if you have experience on this or ideas please share with me.

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

// line drawing class

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚雾 2024-11-17 17:57:19

您应该通过调用 google api 来获取源和目的地之间的坐标列表。

然后你就可以画线了。 参考此内容

you should get list of coordinates between source and destination by calling google api.

then you can draw the line. Refer this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文