0

我能够从
j2memaprouteprovider的参考中绘制两组地理坐标之间的路径。
我能够获得当前位置的纬度和经度。
我如何在以下类中实现这些坐标以获取从当前位置到目标位置的路径。

MapRouteActivity.java

public class MapRouteActivity extends MapActivity {

LinearLayout linearLayout;
MapView mapView;
private Road mRoad;
static Context context = null;
String GPSPROVIDER = LocationManager.GPS_PROVIDER;
private static final long MIN_GEOGRAPHIC_POOLING_TIME_PERIOD = 10000;
private static final float MIN_GEOGRAPHIC_POOLING_DISTANCE = (float) 5.0;
public LocationManager gpsLocationManager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    context = this;

    /* Get a listener for GPS */
    LocationListener gpsLocationListener = null;

    gpsLocationListener = new GpsLocationListener(this);

    /* Start Location Service for GPS */
    gpsLocationManager = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);

    /* Register GPS listener with Location Manager */
    gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            MIN_GEOGRAPHIC_POOLING_TIME_PERIOD,
            MIN_GEOGRAPHIC_POOLING_DISTANCE, gpsLocationListener);
    if (gpsLocationManager == null) {
        gpsLocationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER,
                MIN_GEOGRAPHIC_POOLING_TIME_PERIOD,
                MIN_GEOGRAPHIC_POOLING_DISTANCE, gpsLocationListener);
    }

    boolean isAvailableGps = gpsLocationManager
            .isProviderEnabled(GPSPROVIDER);

    if (isAvailableGps) {

        Location loc = gpsLocationManager.getLastKnownLocation("gps");

        if (loc != null) {

            double lattitude = loc.getLatitude();
            double longitude = loc.getLongitude();

            Toast.makeText(
                    MapRouteActivity.this,
                    "Longitude iss  " + lattitude + "   Latitude iss   "
                            + longitude, Toast.LENGTH_LONG).show();

        }
    }
    new Thread() {
        @Override
        public void run() {
            double fromLat = 49.85, fromLon = 24.016667, toLat = 50.45, toLon = 30.523333;
            String url = RoadProvider
                    .getUrl(fromLat, fromLon, toLat, toLon);
            InputStream is = getConnection(url);
            mRoad = RoadProvider.getRoute(is);
            mHandler.sendEmptyMessage(0);
        }
    }.start();

}

Handler mHandler = new Handler() {
    public void handleMessage(android.os.Message msg) {
        TextView textView = (TextView) findViewById(R.id.description);
        textView.setText(mRoad.mName + " " + mRoad.mDescription);
        MapOverlay mapOverlay = new MapOverlay(mRoad, mapView);
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
        mapView.invalidate();
    };
};

private InputStream getConnection(String url) {
    InputStream is = null;
    try {
        URLConnection conn = new URL(url).openConnection();
        is = conn.getInputStream();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return is;
}

@Override
protected boolean isRouteDisplayed() {
    return false;
}

public static class GpsLocationListener implements LocationListener {
    private ImageView mCurrentPointer;

    public GpsLocationListener(Context context) {

    }

    public void onLocationChanged(Location loc) {

        if (loc != null) {

            double latitude = loc.getLatitude();
            double longitude = loc.getLongitude();
            GeoPoint point = new GeoPoint((int) (latitude * 1E6),
                    (int) (longitude * 1E6));
            Toast.makeText(
                    context,
                    "Longitude is  " + longitude + "   Latitude is   "
                            + latitude, Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

}
}

class MapOverlay extends com.google.android.maps.Overlay {
Road mRoad;
ArrayList<GeoPoint> mPoints;

public MapOverlay(Road road, MapView mv) {
    mRoad = road;
    if (road.mRoute.length > 0) {
        mPoints = new ArrayList<GeoPoint>();
        for (int i = 0; i < road.mRoute.length; i++) {
            mPoints.add(new GeoPoint((int) (road.mRoute[i][1] * 1000000),
                    (int) (road.mRoute[i][0] * 1000000)));
        }
        int moveToLat = (mPoints.get(0).getLatitudeE6() + (mPoints.get(
                mPoints.size() - 1).getLatitudeE6() - mPoints.get(0)
                .getLatitudeE6()) / 2);
        int moveToLong = (mPoints.get(0).getLongitudeE6() + (mPoints.get(
                mPoints.size() - 1).getLongitudeE6() - mPoints.get(0)
                .getLongitudeE6()) / 2);
        GeoPoint moveTo = new GeoPoint(moveToLat, moveToLong);

        MapController mapController = mv.getController();
        mapController.animateTo(moveTo);
        mapController.setZoom(7);
    }
    }

@Override
public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
    super.draw(canvas, mv, shadow);
    drawPath(mv, canvas);
    return true;
}

public void drawPath(MapView mv, Canvas canvas) {
    int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
    Paint paint = new Paint();
    paint.setColor(Color.GREEN);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(3);
    for (int i = 0; i < mPoints.size(); i++) {
        Point point = new Point();
        mv.getProjection().toPixels(mPoints.get(i), point);
        x2 = point.x;
        y2 = point.y;
        if (i > 0) {
            canvas.drawLine(x1, y1, x2, y2, paint);
        }
        x1 = x2;
        y1 = y2;
    }
}
}

我想在上面的代码中将当前位置的纬度和经度值分配给 fromLat 和 fronLon 变量:

new Thread() {
        @Override
        public void run() {
            double fromLat = 49.85, fromLon = 24.016667, toLat = 50.45, toLon = 30.523333;
            String url = RoadProvider
                    .getUrl(fromLat, fromLon, toLat, toLon);
            InputStream is = getConnection(url);
            mRoad = RoadProvider.getRoute(is);
            mHandler.sendEmptyMessage(0);
        }
    }.start();

帮助 !!

4

2 回答 2

1

非常简单---

使您的活动像这样:--

 public class GoogleMapLocationActivity extends MapActivity {

 private LocationManager myLocationManager;
 private LocationListener myLocationListener;
 private TextView myLongitude, myLatitude;
 private MapView myMapView;
 private MapController myMapController;
 LinearLayout zoomLayout;
 GeoPoint myLastPosition;
 AddItemizedOverlay mapOvlay;

 private void CenterLocatio(GeoPoint centerGeoPoint)
 {
  myMapController.animateTo(centerGeoPoint);

  List<Overlay> mapOverlays = myMapView.getOverlays();
  Drawable drawable = this.getResources().getDrawable(R.drawable.map_point);

  if(myLastPosition != null){
  mapOvlay = new AddItemizedOverlay(myLastPosition ,centerGeoPoint,drawable );
  OverlayItem overlayitem = new OverlayItem(centerGeoPoint,"","" );
  mapOvlay.addOverlay(overlayitem);
  mapOverlays.add(mapOvlay);
  }
      myLastPosition = centerGeoPoint;

};

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  myMapView = (MapView)findViewById(R.id.mapview);
         myLastPosition = null;
     myMapController = myMapView.getController();
     myMapController.setZoom(18); //Fixed Zoom Level

  myLocationManager = (LocationManager)getSystemService(
    Context.LOCATION_SERVICE);

  myLocationListener = new MyLocationListener();

  myLocationManager.requestLocationUpdates(
    LocationManager.GPS_PROVIDER,
    0,
    0,
    myLocationListener);

 private class MyLocationListener implements LocationListener{

      public void onLocationChanged(Location argLocation) {
        GeoPoint myGeoPoint = new GeoPoint(
        (int)(argLocation.getLatitude()*1000000),
         (int)(argLocation.getLongitude()*1000000));

     GeoPoint newGeoPoint = new  GeoPoint(myGeoPoint.getLatitudeE6() ,myGeoPoint.getLongitudeE6());
   CenterLocatio(newGeoPoint);
  }

  public void onProviderDisabled(String provider) {
      Toast.makeText(getBaseContext(),"GPS Disabled" ,Toast.LENGTH_SHORT).show();
  }

  public void onProviderEnabled(String provider) {
      Toast.makeText(getBaseContext(),"GPS Enabled" ,Toast.LENGTH_SHORT).show();
  }

  public void onStatusChanged(String provider,
    int status, Bundle extras) {
      Toast.makeText(getBaseContext(),"GPS Unavailable" ,Toast.LENGTH_SHORT).show();
  }
 }

 @Override
 protected boolean isRouteDisplayed() {
  return false;
 };

}

AddItemizedOverlay.class-----

public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {

       private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

       private Context context;
       private GeoPoint mGpt1;
       private GeoPoint mGpt2;

       public AddItemizedOverlay(GeoPoint gp1,GeoPoint gp2, Drawable defaultMarker) {

            super(boundCenterBottom(defaultMarker));
             mGpt1 = gp1;
             mGpt2 = gp2;
        }


       public AddItemizedOverlay(Drawable defaultMarker) {
            super(boundCenterBottom(defaultMarker));
       }

       public AddItemizedOverlay(Drawable defaultMarker, Context context) {
            this(defaultMarker);
            this.context = context;
       }

       @Override
       protected OverlayItem createItem(int i) {
          return mapOverlays.get(i);
       }

       @Override
       public int size() {
          return mapOverlays.size();
       }

       @Override
       protected boolean onTap(int index) {
          Log.e("Tap", "Tap Performed");
          return true;
       }

       public void addOverlay(OverlayItem overlay) {
          mapOverlays.add(overlay);
           this.populate();
       }

       public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
                long when) {
             super.draw(canvas, mapView, shadow);
             Paint paint;
             paint = new Paint();
             paint.setColor(Color.RED);
             paint.setAntiAlias(true);
             paint.setStyle(Style.STROKE);
             paint.setStrokeWidth(2);
             Point pt1 = new Point();
             Point pt2 = new Point();
             Projection projection = mapView.getProjection();
             projection.toPixels(mGpt1, pt1);
             projection.toPixels(mGpt2, pt2);
             canvas.drawLine(pt1.x, pt1.y, pt2.x, pt2.y, paint);
             return true;
          }
    }

希望它会帮助你...

于 2012-04-10T09:03:47.340 回答
0

使用以下,它将在本地地图应用程序上显示方向,

 final Intent intent = new Intent(Intent.ACTION_VIEW,
    Uri.parse(
            "http://maps.google.com/maps?" +
            "saddr="+YOUR_START_LONGITUDE+","+YOUR_START_LATITUDE+"&daddr="YOUR_END_LONGITUDE+","+YOUR_END_LATITUDE));
         intent.setClassName(
          "com.google.android.apps.maps",
          "com.google.android.maps.MapsActivity");
   startActivity(intent);
于 2012-04-09T07:00:23.707 回答