如何使用osmdroid / osmbonus在Android应用程序中打开街道地图上的用户移动时绘制路径?

时间:2016-01-29 07:48:40

标签: android openstreetmap osmdroid

This image shows the red dot which is location updation how can i join those points 我第一次在Android应用程序中使用OSM来显示地图。这是我的代码,它只是在你的位置每次更新时创建一个点我需要一条线(路径)从哪里开始直到我的目的地。我怎么能实现这一目标?运用 PathOverlay我创建了点如何加入这些点,以便它形成一条线?

public class AutoTracking extends Activity   {
    private MapView map;
    private MapController myMapController;
    LocationManager locationManager;
    int mIncr = 10;
    ArrayList<OverlayItem> overlayItemArray;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_tracking);
         map = (MapView) findViewById(R.id.map);
        BaseActivity base_obj;
        BaseActivity.UpdateTextViewTiltle("Tracking");
        Resources res=getResources();
        Drawable dr=res.getDrawable(R.drawable.ic_tracking);
        BaseActivity.UpdateIcon2(dr);
        map.setBuiltInZoomControls(true);
        myMapController = (MapController) map.getController();
        myMapController.setZoom(12);
        //--- Create Overlay
        overlayItemArray = new ArrayList<OverlayItem>();
        DefaultResourceProxyImpl defaultResourceProxyImpl
                = new DefaultResourceProxyImpl(this);
        MyItemizedIconOverlay myItemizedIconOverlay
                = new MyItemizedIconOverlay(
                overlayItemArray, null, defaultResourceProxyImpl);
        map.getOverlays().add(myItemizedIconOverlay);
        //---
        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        //for demo, getLastKnownLocation from GPS only, not from NETWORK
        Location lastLocation
                = locationManager.getLastKnownLocation(
                LocationManager.GPS_PROVIDER);
        if(lastLocation != null){
          //  updateLoc(lastLocation);
        }
        //Add Scale Bar
        ScaleBarOverlay myScaleBarOverlay = new ScaleBarOverlay(this);
        map.getOverlays().add(myScaleBarOverlay);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocationListener);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, myLocationListener);
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        locationManager.removeUpdates(myLocationListener);
    }
    private void updateLoc(Location loc){
        GeoPoint locGeoPoint = new GeoPoint(loc.getLatitude(), loc.getLongitude());
        polyline=new Polyline();
      //  GeoPoint gPt1 = new GeoPoint(locGeoPoint.getLatitudeE6()+ mIncr, locGeoPoint.getLongitudeE6());
       GeoPoint gPt2 = new GeoPoint(locGeoPoint.getLatitudeE6()+ mIncr, locGeoPoint.getLongitudeE6() + mIncr);
     //  GeoPoint gPt3 = new GeoPoint(locGeoPoint.getLatitudeE6(), locGeoPoint.getLongitudeE6() );
        myMapController.setCenter(locGeoPoint);
        PathOverlay myPath = new PathOverlay(Color.RED, this);
        myPath.addPoint(locGeoPoint);
     //  myPath.addPoint(gPt1);
      myPath.addPoint(gPt2);
      // myPath.addPoint(gPt3);
        myPath.addPoint(locGeoPoint);
        map.getOverlays().add(myPath);
        myMapController.setCenter(locGeoPoint);
        setOverlayLoc(loc);
        map.invalidate();
    }
    private void setOverlayLoc(Location overlayloc){
        GeoPoint overlocGeoPoint = new GeoPoint(overlayloc);
        //---
        overlayItemArray.clear();
        OverlayItem newMyLocationItem = new OverlayItem(
                "My Location", "My Location", overlocGeoPoint);
        overlayItemArray.add(newMyLocationItem);
        //---
    }
    private LocationListener myLocationListener
            = new LocationListener(){
        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            updateLoc(location);
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
        }
    };
    private class MyItemizedIconOverlay extends ItemizedIconOverlay<OverlayItem> {
        public MyItemizedIconOverlay(
                List<OverlayItem> pList,
                org.osmdroid.views.overlay.ItemizedIconOverlay.OnItemGestureListener<OverlayItem> pOnItemGestureListener,
                ResourceProxy pResourceProxy) {
            super(pList, pOnItemGestureListener, pResourceProxy);
            // TODO Auto-generated constructor stub
        }
        @Override
        public void draw(Canvas canvas, MapView mapview, boolean arg2) {
            // TODO Auto-generated method stub
            super.draw(canvas, mapview, arg2);
            if(!overlayItemArray.isEmpty()){
                //overlayItemArray have only ONE element only, so I hard code to get(0)
                GeoPoint in = (GeoPoint) overlayItemArray.get(0).getPoint();
                Point out = new Point();
                mapview.getProjection().toPixels(in, out);
                Bitmap bm = BitmapFactory.decodeResource(getResources(),
                        R.drawable.user);
                canvas.drawBitmap(bm,
                        out.x - bm.getWidth()/2,  //shift the bitmap center
                        out.y - bm.getHeight()/2,  //shift the bitmap center
                        null);
            }
        }
        @Override
        public boolean onSingleTapUp(MotionEvent event, MapView mapView) {
            // TODO Auto-generated method stub
            //return super.onSingleTapUp(event, mapView);
            return true;
        }
    }

}

0 个答案:

没有答案