GoogleMaps v2 - 将叠加添加到Activity或FragmentActivity

时间:2013-02-14 13:47:17

标签: android google-maps google-maps-api-2 google-maps-android-api-2

请告诉我,我有很多时间无法理解。

我有一张地图。我想按照你的方式画画。

问题是所有的例子都是mapView,我有一个片段。

我不明白我该如何绘制路线?

如何创建活动?

所以

public class TravelMapActivity extends Activity implements OnClickListener {
    private GoogleMap map;
    private ToggleButton travelOnOffButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.travel_map_activity);
        init();
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        travelOnOffButton = (ToggleButton) findViewById(R.id.travel_start_stop);
        travelOnOffButton.setOnClickListener(this);
    }

左右?

public class TravelMapActivity extends FragmentActivity implements OnClickListener {
    private GoogleMap map;
    private ToggleButton travelOnOffButton;
    private SupportMapFragment mMapFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mMapFragment = new SupportMapFragment();
        try {
            MapsInitializer.initialize(this);
        } catch (GooglePlayServicesNotAvailableException e) {
            // TODO handle error
            e.printStackTrace();
        }
        getSupportFragmentManager().beginTransaction().add(android.R.id.content, mMapFragment).commit();

        travelOnOffButton = (ToggleButton) findViewById(R.id.travel_start_stop);
        travelOnOffButton.setOnClickListener(this);
    }

在这种情况下,它已经在线setOnClickListener - 抛出NullPointerException。

作为一个例子。

class RouteOverlay extends Overlay {

    private Projection projection;

    public RouteOverlay() {
    }

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

        Paint mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(2);

        GeoPoint gP1 = new GeoPoint(19240000, -99120000);
        GeoPoint gP2 = new GeoPoint(37423157, -122085008);

        Point p1 = new Point();
        Point p2 = new Point();
        Path path = new Path();

        projection.toPixels(gP1, p1);
        projection.toPixels(gP2, p2);

        path.moveTo(p2.x, p2.y);
        path.lineTo(p1.x, p1.y);

        canvas.drawPath(path, mPaint);
    }
}

1 个答案:

答案 0 :(得分:0)

要在地图V2上绘制路线,请add a Polyline to the GoogleMap

PolylineOptions line=
      new PolylineOptions().add(new LatLng(40.70686417491799,
                                           -74.01572942733765),
                                new LatLng(40.76866299974387,
                                           -73.98268461227417),
                                new LatLng(40.765136435316755,
                                           -73.97989511489868),
                                new LatLng(40.748963847316034,
                                           -73.96807193756104))
                           .width(5).color(Color.RED);

  map.addPolyline(line);

(代码取自this sample project