android MapView DrawLine

时间:2011-04-28 15:38:03

标签: android overlay android-mapview itemizedoverlay

我正在尝试在mapActivity上绘制一些叠加路径。

这是My ItemizedOverlay Class:

package com.state.park;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

public class Flytrap extends ItemizedOverlay<OverlayItem> {
private GeoPoint gp1, gp2;
private int color;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public Flytrap(Drawable defaultMarker){
    super(defaultMarker);
}

public Flytrap(GeoPoint gp1, GeoPoint gp2, int color, MapView mapView, Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
    this.gp1 = gp1;
    this.gp2 = gp2;
    this.color = color;
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {

    Projection projection = mapView.getProjection();
    Paint paint = new Paint();
    Point point = new Point();
    projection.toPixels(gp1, point);
    paint.setColor(color);
    Point point2 = new Point();
    projection.toPixels(gp2, point2);
    paint.setStrokeWidth(5);
    paint.setAlpha(120);
    canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
    super.draw(canvas, mapView, shadow);
}

public void addItem(OverlayItem item){
     mOverlays.add(item);
        populate();

}

@Override
protected OverlayItem createItem(int i) {
    // TODO Auto-generated method stub
    return mOverlays.get(i);
}

@Override
public int size() {
    // TODO Auto-generated method stub
    return 0;
}


 }

这就是我称之为的MapActivity。

ArrayList<Double> lon = new ArrayList<Double>();
ArrayList<Double> lat = new ArrayList<Double>();
ArrayList<GeoPoint> geopoints = new ArrayList<GeoPoint>();
List<Overlay> overlays;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.map);

    String flytrap = readRawTextFile(getBaseContext(), R.raw.flytrap);
    getlonlat(flytrap);

    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mc = mapView.getController();
    myLoc = new MyLocationOverlay(this, mapView);
    mapView.getOverlays().add(myLoc);
    locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locListener = new MyLocationListener();
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
            locListener);

    Drawable d = getResources().getDrawable(R.drawable.icon);

    for (int i = 1; i < geopoints.size(); i++) {

        mapView.getOverlays().add(new Flytrap(geopoints.get(i-1), geopoints.get(i), Color.BLUE, mapView, d ));
    }

    mapView.invalidate();
}

protected boolean isLocationDisplayed() {
    return myLoc.isMyLocationEnabled();
}

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

有关如何在地图上显示线条的任何建议。截至目前,它运行。但没有显示叠加。

谢谢。

1 个答案:

答案 0 :(得分:0)

而不是

mapView.invalidate();

mapView.postInvalidate();

此外,您的叠加层应扩展叠加,而不是 ItemizedOverlay ,因为这些是可点击的气球等。覆盖不应该在构造函数中需要MapView,也不能在draw方法中访问它。