1

我一直试图让这个应用程序绘制一条连接从用户当前位置收集的 geoPoints 数组的线。尝试填充阵列以便可以连接它们时,我被卡住了。

代码:

public class MainActivity extends MapActivity implements LocationListener {

private MyLocationOverlay myLocationOverlay;
private MapController mMapController;
private MapView mapview;
private MapController controller;

Location Location;
ArrayList<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>();
String provider = LocationManager.GPS_PROVIDER;
double lat;
double lon;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
    boolean enabled = service.isProviderEnabled(provider); 

    // Check if enabled and if not send user to the GPS settings
    if (!enabled) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
    }

    //Creates new MapView
    MapView mapview = (MapView) findViewById(R.id.mapView);
    //Sets up Zoom buttons on screen
    mapview.setBuiltInZoomControls(true);
    //Sets to Satellite View on Map
    mapview.setSatellite(true);
    myLocationOverlay = new MyLocationOverlay(this, mapview);
    myLocationOverlay.enableMyLocation();
    myLocationOverlay.enableCompass();
    mapview.getOverlays().add(myLocationOverlay);
    mapview.invalidate();
    mMapController = mapview.getController();
    //Sets Zoom level
    mMapController.setZoom(20);
    //Moves map To current location.
    myLocationOverlay.runOnFirstFix(new Runnable() {
        @Override
        public void run() {
            mMapController.animateTo(myLocationOverlay.getMyLocation());
        }
    }); 
}

class MyOverlay extends Overlay{
    public MyOverlay(){ 
    }   

    public void draw(Canvas canvas, MapView mapv, boolean shadow){
    super.draw(canvas, mapv, shadow);

    Paint mPaint = new Paint();
    Path p = new Path();
    Projection projection = mapview.getProjection();

    mPaint.setStyle(Style.FILL_AND_STROKE);
    mPaint.setColor(Color.RED);

    mPaint.setDither(true);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(2);


    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);
        p.lineTo(to.x, to.y);
   }

}

}




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


@Override
protected void onPause() {
    super.onPause();
    myLocationOverlay.disableMyLocation();
}

@Override
protected void onResume() {
    super.onResume();
    myLocationOverlay.enableMyLocation();
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

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

    }

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}


}

任何帮助都会很棒。谢谢

4

0 回答 0