如何从一个GPS位置绘制一条线到另一个?

时间:2012-05-30 01:49:18

标签: android android-layout android-mapview android-maps

我有两个GPS坐标,这是我下面的代码,我正在尝试用线连接它们。我找到了这个帖子:Connect points on map with lines但我不认为我完全理解它。有人能更清楚地解释一下吗? (理想情况下,我希望每当我点击一个标记时,就会有一条线连接到它。)

这是我的代码:

package draw.live;

import java.util.ArrayList;
import java.util.List;

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.Overlay;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import android.graphics.Point;
import android.os.Bundle;
import android.view.MotionEvent;

import android.widget.Button;

public class start extends MapActivity{
    // MapActivity variables
    MapController mCont;
    GeoPoint geo;
    MapView mapV;
    Button Draw, Stop;

    /* Called when the activity is first created. references my start.xml*/
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start);

        /*
         * Allows zoom controls on the screen along with
         * zoom functionality.
         */
        mapV = (MapView) findViewById(R.id.mapview);
        mapV.displayZoomControls(true);
        mapV.setBuiltInZoomControls(true);

        //longitude and latitude
        double lat = 32.87728;
        double lng = -117.22961;

        //Are equation to micro Degree's    
        geo = new GeoPoint ((int) (lat *1E6), (int) (lng * 1E6 ));

        //Gets are map and sets a Fixed Zoom 15
        mCont = mapV.getController();
        mCont.animateTo(geo);
        mCont.setZoom(15);

        //Add a location marker
        mCont.animateTo(geo);
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapV.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);        

        mapV.invalidate();
    }

    public class MapOverlay extends com.google.android.maps.Overlay
    {        
        /*
         * Class for adding Marker Overlay for every time it is clicked.
         */

       public class MarkerOverlay extends Overlay{
            private GeoPoint geo; 
            public MarkerOverlay(GeoPoint p){
                this.geo = p;
            }

            //Creates my canvas to draw on my Map
            @Override
            public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when){

               Paint paint;
               super.draw(canvas, mapView, shadow);                   

               //Translate the GeoPoint to screen pixels
               Point screenPts = new Point();
               mapView.getProjection().toPixels(geo, screenPts);                      

               //Adds a Marker on the map
               Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);            
               canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);                    
               return true;    
            }
        }

      public boolean onTouchEvent (MotionEvent event, MapView mapView) 
      {   
         /*
          * Translate to Geo Pixels, Along with everytime you click adds marker.
          */
         Point screenPts = new Point();
         mapView.getProjection().toPixels(geo, screenPts);
          if (event.getAction() == 1) {                
              GeoPoint geo = mapView.getProjection().fromPixels(
                      (int) event.getX(),
                      (int) event.getY());
                      /*Toast.makeText(getBaseContext(), 
                          geo.getLatitudeE6() / 1E6 + "," + 
                          geo.getLongitudeE6() /1E6 , 
                          Toast.LENGTH_SHORT).show(); */                      
                      mapView.getOverlays().add(new MarkerOverlay(geo));
                      mapView.invalidate();

          }                            
          return false;
      }
    } 

    //Not used yet....
    /*public void onClick(View v) {

        //Check's to see if button 3 is clicked
       if (v.getId() == R.id.button3){


        }
        // Checks to see if button4 button has been clicked
        if (v.getId() == R.id.button4){
        }
    }*/

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

1 个答案:

答案 0 :(得分:1)

您应该检查此项目:https://github.com/frogermcs/RoutePathExample

我认为这可能对你有所帮助。