2012-04-09 6 views
0

Ich bin in der Lage, Pfad zwischen zwei Sätze von Geo-Koordinaten aus Referenz von
j2memaprouteprovider zeichnen.
Ich kann den aktuellen Standort Breite und Länge erhalten.
Wie implementiere ich diese Koordinaten in der folgenden Klasse, um den Pfad von der aktuellen Position zur Zielposition zu ziehen.Wie werden aktuelle Geokoordinaten zugewiesen, um den Pfad vom aktuellen Standort zum Zielort zu zeichnen?

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

Ich möchte hier in obigen Code aktuelle Position lattiude und Längenwert fromLat und fronLon Variablen zugewiesen werden:

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

Hilfe

!!

Antwort

1

Ganz einfach ---

Machen Sie Ihre Aktivität wie folgt aus: -

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. Klasse -----

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

Ich hoffe, es wird Ihnen helfen ...

0

Verwendung Folgenden wird Richtung auf nativen Kartenanwendung anzuzeigen,

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); 
+0

Ich möchte zusätzliche Informationen in Google Map anzeigen, die ich mit dem obigen Code nicht tun kann !! – captaindroid