0

我搜索了两个小时,但只找到了画线,我想得到这个表格的方向点击这里!我有十个点,当我单击该点时,我想显示从我所在位置到该点的方向。我的代码看起来:

package de.vogella.android.locationapi.maps;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

   import com.google.android.maps.GeoPoint;
   import com.google.android.maps.MapActivity;
   import com.google.android.maps.MapController;
   import com.google.android.maps.MapView;
   import com.google.android.maps.MyLocationOverlay;
   import com.google.android.maps.OverlayItem;
   import com.google.android.maps.Projection;

    public class ShowMapActivity extends MapActivity {

private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private MyOverlays itemizedoverlay,itemizedoverlay_central;
private MyLocationOverlay myLocationOverlay;
private String nameOfOffice_AZ[];
private String addressOfOffice_AZ[];
private String phoneOfOffice_AZ[];

public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.main); // bind the layout to the activity

    // Configure the Map
    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    Projection projection = mapView.getProjection();

    nameOfOffice_AZ= getResources().getStringArray(R.array.NamesOFObject_AZ);
    addressOfOffice_AZ = getResources().getStringArray(R.array.AddressOfOffice_AZ);
    phoneOfOffice_AZ = getResources().getStringArray(R.array.PhoneOfOffice_AZ);


    mapController = mapView.getController();
    mapController.setZoom(16); // Zoon 1 is world view
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
            0, new GeoUpdateHandler());

    myLocationOverlay = new MyLocationOverlay(this, mapView);
    mapView.getOverlays().add(myLocationOverlay);

    myLocationOverlay.runOnFirstFix(new Runnable() {
        public void run() {
            mapView.getController().animateTo(myLocationOverlay.getMyLocation());
        }
    });

    Drawable drawable = this.getResources().getDrawable(R.drawable.office_icon);
    Drawable drawable_central = this.getResources().getDrawable(R.drawable.central_office);



    itemizedoverlay = new MyOverlays(this, drawable);
    itemizedoverlay_central = new MyOverlays(this, drawable_central);

    createMarker();
}

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

public class GeoUpdateHandler implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {
        int lat = (int) (location.getLatitude() * 1E6);
        int lng = (int) (location.getLongitude() * 1E6);
        GeoPoint point = new GeoPoint(lat, lng);
        createMarker();
        mapController.animateTo(point); // mapController.setCenter(point);

    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

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

private void createMarker() {
    //CENTRAL OFFICE
    GeoPoint p_central = new GeoPoint((int)(40.38292884084829 * 1e6),(int)(49.84223246574 * 1e6));
    OverlayItem overlayitem = new OverlayItem(p_central, nameOfOffice_AZ[0] ,addressOfOffice_AZ[0]+"\n"+phoneOfOffice_AZ[0] );
    itemizedoverlay_central.addOverlay(overlayitem);
    mapView.getOverlays().add(itemizedoverlay_central);
    //END CENTRALL OFFICE


    GeoPoint p = new GeoPoint((int)(40.376455916943236 * 1e6),(int)(49.84803676605224 * 1e6));
    overlayitem = new OverlayItem(p, nameOfOffice_AZ[0] ,addressOfOffice_AZ[0]+"\n"+phoneOfOffice_AZ[0] );
    itemizedoverlay.addOverlay(overlayitem);

        mapView.getOverlays().add(itemizedoverlay);

}

@Override
protected void onResume() {
    super.onResume();
    myLocationOverlay.enableMyLocation();
    myLocationOverlay.enableCompass();
}

@Override
protected void onPause() {
    super.onPause();
    myLocationOverlay.disableMyLocation();
    myLocationOverlay.disableCompass();
}

和 MyOverlays.java

package de.vogella.android.locationapi.maps;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
 import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

 public class MyOverlays extends ItemizedOverlay<OverlayItem> {

private static int maxNum = 5;
private OverlayItem overlays[] = new OverlayItem[maxNum];
private int index = 0;
private boolean full = false;
private Context mContext;
private OverlayItem previousoverlay;

public MyOverlays(Context context, Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
    this.mContext = context;
}

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





public void addOverlay(OverlayItem overlay) {
    mOverlays.add(overlay);
    populate();
}
public void name()  {

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

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

@Override
protected boolean onTap(int i) {
    //when you tap on the marker this will show the informations provided by you when    you create in the 
    //main class the OverlayItem
   OverlayItem item = mOverlays.get(i);

    AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
    dialog.setInverseBackgroundForced(true);
    dialog.setTitle(item.getTitle());
    dialog.setMessage(item.getSnippet());

    dialog.setCancelable(true);
    dialog.setPositiveButton("Go To", new OkOnClickListener());
   /// dialog.setNegativeButton("No, no", new CancelOnClickListener());
    dialog.show();
//  nese bax = new nese();
//  bax.oldu(mContext);
    return true;
}
private final class CancelOnClickListener implements
DialogInterface.OnClickListener {
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(mContext, "You clicked yes", Toast.LENGTH_LONG)
                .show();
    }
    }

    private final class OkOnClickListener implements
    DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
    //Toast.makeText(mContext, "You clicked no", Toast.LENGTH_LONG).show();
}

}


    public class RoutePath extends MapActivity { 
/** Called when the activity is first created. */ 

MapView mapView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

MapView mapView = (MapView) findViewById(R.id.mapview); 
double src_lat = 25.04202; // the testing source 
double src_long = 121.534761; 
double dest_lat = 25.05202; // the testing destination 
double dest_long = 121.554761; 
GeoPoint srcGeoPoint = new GeoPoint((int) (src_lat * 1E6), 
(int) (src_long * 1E6)); 
GeoPoint destGeoPoint = new GeoPoint((int) (dest_lat * 1E6), 
(int) (dest_long * 1E6)); 

DrawPath(srcGeoPoint, destGeoPoint, Color.GREEN, mapView); 

mapView.getController().animateTo(srcGeoPoint); 
mapView.getController().setZoom(15); 

} 

@Override 
protected boolean isRouteDisplayed() { 
// TODO Auto-generated method stub 
return false; 
} 

private void DrawPath(GeoPoint src, GeoPoint dest, int color, 
MapView mMapView01) { 

// code in section 2.2 

} 

}

}

4

2 回答 2

2

减少搜索的努力..

不管怎样,看看谷歌行车路线 - 地图视图覆盖了谷歌方向 API

并使用意图

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("http://maps.google.com/maps?saddr=xx.xx,xx.xx&daddr=xx.xx,xx.xx"));
startActivity(intent);
于 2012-07-16T12:42:51.493 回答
1

这篇博文可以作为一个很好的起点:Android 驾驶方向 Route-Path

对我来说工作得很好。

于 2012-07-16T12:42:28.113 回答