2011-04-28 75 views
1

我想在我的mapActivity上繪製一些覆蓋路徑。android mapview drawline

這是我的ItemizedOverlay類:

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; 
    } 

如何顯示在地圖上的線條任何建議。現在它運行。但不顯示重疊。

謝謝。

回答

0

而不是

mapView.invalidate(); 

嘗試

mapView.postInvalidate(); 

而且你應該覆蓋延伸覆蓋代替ItemizedOverlay那些是可點擊氣球等。疊加層不應該需要構造函數中的MapView,也可以使用draw方法訪問。