请告诉我,我有很多小时无法理解。
我有一张地图。我想按照你的方式画。
问题是所有的例子都是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);
}
}