在谷歌地图,android上绘制一条路线

时间:2012-05-27 20:20:44

标签: android google-maps gps routes

我想在Google地图上绘制一条路线,使用GPS改变我的位置。随着我的位置发生变化(创建新的地理位置时),点在谷歌地图上移动,但我无法在地图上绘制线条。

请帮助绘制谷歌地图上的路线。以下是我的代码

`

LocationManager locman;
LocationListener loclis;
Location location;
private MapView map;

List<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>();
private MapController controller;
String provider = LocationManager.GPS_PROVIDER;
double lat;
double lon;


@Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      initMapView();
      initMyLocation();
      locman = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
      //locman.requestLocationUpdates(provider,60000, 100,loclis);
      //Location = locman.getLastKnownLocation(provider);

   }
/** Find and initialize the map view. */
   private void initMapView() {
      map = (MapView) findViewById(R.id.mapView);
      controller = map.getController();
      map.setSatellite(false);
      map.setBuiltInZoomControls(true);
   }

   /** Find Current Position on Map. */
   private void initMyLocation() {
      final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
      overlay.enableMyLocation();
      overlay.enableCompass(); // does not work in emulator
      overlay.runOnFirstFix(new Runnable() {
         public void run() {
            // Zoom in to current location
            controller.setZoom(16);
            controller.animateTo(overlay.getMyLocation());
         }
      });
      map.getOverlays().add(overlay);
   }

public void onLocationChanged(Location location) {
    if (location != null){
        lat = location.getLatitude();
        lon = location.getLongitude();
        GeoPoint New_geopoint = new GeoPoint((int)(lat*1e6),(int)(lon*1e6));
        controller.animateTo(New_geopoint);

    }

}
class MyOverlay extends Overlay{
    public MyOverlay(){
    }   
    public void draw(Canvas canvas, MapView mapv, boolean shadow){
    super.draw(canvas, mapv, shadow);
    Paint paint;
    paint = new Paint();
    paint.setColor(Color.GREEN);

    paint.setAntiAlias(true);
    paint.setStyle(Style.STROKE);
    paint.setStrokeWidth(3);

        Projection projection = map.getProjection();
        Path p = new Path();
        for (int i = 0; i < geoPointsArray.size(); i++) {
            if (i == geoPointsArray.size() - 1) {
                break;
            }
        Point from = new Point();
        Point to = new Point();
        projection.toPixels(geoPointsArray.get(i), from);
        projection.toPixels(geoPointsArray.get(i + 1), to);
        p.moveTo(from.x, from.y);

        canvas.drawLine(from.x, from.y, to.x, to.y, paint);
            }
       }
 }

    `

0 个答案:

没有答案