Google地图在当前位置和点之间绘制路线

时间:2014-01-18 15:52:59

标签: android google-maps-android-api-2

我正在尝试在当前位置和另一个点之间绘制路线。我编写的代码可以检查当前位置,并在地图上点击地图上的点。目前,该计划运作良好, 但我想在当前位置和新点之间绘制另一条路线(点女巫我在地图点击监听器上添加了地图)。 我的代码如下,有谁知道如何在我的代码中添加这个逻辑?

public class GPS extends Activity implements

    OnMyLocationChangeListener,OnMapClickListener,
    OnMapLongClickListener, OnMarkerDragListener {

final int RQS_GooglePlayServices = 1;
private GoogleMap myMap;
Circle myCircle;
Location myLocation;
TextView tvLocInfo, GPSLocation;
LatLng latLng;
boolean markerClicked;
ArrayList<LatLng> markerPoints;
Polygon polygon;

public Button btnline;
double Clicklatitude, Clicklongitude, latitude, longitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.gps);
    markerPoints=new ArrayList<LatLng>();
    btnline = (Button) findViewById(R.id.button1);
    tvLocInfo = (TextView) findViewById(R.id.GpsTxt);
    GPSLocation = (TextView) findViewById(R.id.GPSLocation);
    FragmentManager myFragmentManager = getFragmentManager();
    MapFragment myMapFragment = (MapFragment) myFragmentManager
            .findFragmentById(R.id.GpsMap);
    myMap = myMapFragment.getMap();

    myMap.setMyLocationEnabled(true);
    myMap.setOnMyLocationChangeListener(this);

    myMap.setOnMapClickListener(this);
    myMap.setOnMapLongClickListener(this);
    myMap.setOnMarkerDragListener(this);

    myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    myMap.setMyLocationEnabled(true);
    myMap.setOnMyLocationChangeListener(this);




    markerClicked = false;
    btnline.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {



        }
    });
}

@Override
protected void onResume() {

    super.onResume();

    int resultCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(getApplicationContext());

    if (resultCode == ConnectionResult.SUCCESS) {
        Toast.makeText(getApplicationContext(),
                "isGooglePlayServicesAvailable SUCCESS", Toast.LENGTH_LONG)
                .show();
    } else {
        GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                RQS_GooglePlayServices);
    }

}





@Override
public void onMyLocationChange(Location location) {

    latitude = location.getLatitude();
    // Getting longitude of the current location
    longitude = location.getLongitude();
    // Creating a LatLng object for the current location
    latLng = new LatLng(latitude, longitude);
    // Showing the current location in Google Map
    myMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    // Zoom in the Google Map

    GPSLocation.setText(latitude + " " + longitude);
    myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    LatLng locLatLng = new LatLng(location.getLatitude(),
            location.getLongitude());
    double accuracy = location.getAccuracy();

    if (myCircle == null) {
        CircleOptions circleOptions = new CircleOptions().center(locLatLng)
                // set center
                .radius(accuracy)
                // set radius in meters
                .fillColor(Color.RED).strokeColor(Color.BLACK)
                .strokeWidth(5);

        myCircle = myMap.addCircle(circleOptions);
    } else {
        myCircle.setCenter(locLatLng);
        myCircle.setRadius(accuracy);
    }
    myMap.animateCamera(CameraUpdateFactory.zoomTo(15));
    // myMap.animateCamera(CameraUpdateFactory.newLatLng(locLatLng));

}

@Override
public void onMarkerDrag(Marker arg0) {
    // TODO Auto-generated method stub

}
@Override
public void onMarkerDragEnd(Marker arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onMarkerDragStart(Marker arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onMapLongClick(LatLng point) {


}

@Override
public void onMapClick(LatLng point) {
    Clicklatitude = point.latitude;
    Clicklongitude = point.longitude;
    tvLocInfo.setText(Clicklatitude + " " + Clicklongitude);
    if (point != null)
        myMap.clear();
    myMap.addMarker(new MarkerOptions().position(point).draggable(true));

    markerClicked = false;

}

}

我希望得到这样的结果enter link description here

0 个答案:

没有答案