問題描述
我想創建一個類似于 Google 地圖的功能.每當有人開始步行/開車時,我都想更新我的位置標記.我正在考慮使用 LocationListner 的 onLocationChange 方法,但我不確定.我也喜歡在谷歌地圖上繪制更新位置的路徑.
I want to create a functionality like Google Maps. I want to update my location marker whenever a person start walking/driving. I am thinking of using onLocationChange method of LocationListner but I am not sure. I also like to draw the path with updating location on the Google Maps.
歡迎所有建議.
ItemizedOverlay:-
ItemizedOverlay:-
public class LocationOverlay extends ItemizedOverlay<OverlayItem>{
private List<OverlayItem> myOverlays;
Path path;
static int cnt = -1;
public LocationOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
System.out.println("Normal...");
myOverlays = new ArrayList<OverlayItem>();
}
public void addOverlay(OverlayItem overlay) {
System.out.println("Add Overlay called");
myOverlays.add(overlay);
populate();
cnt++;
}
@Override
protected OverlayItem createItem(int i) {
return myOverlays.get(i);
}
public void removeItem(int i) {
myOverlays.remove(i);
populate();
}
@Override
public int size() {
return myOverlays.size();
}
@Override
public void draw(Canvas canvas, final MapView mapView, boolean shadow) {
if (shadow) {
paint.setDither(true);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(4);
path = new Path();
Point point1 = new Point();
Projection projection = mapView.getProjection();
if (myOverlays.size() > 1) {
System.out.println(">>>>>>>>>>>");
for (int i = 0, j=i; i < myOverlays.size(); i++) {
Point point1 = new Point();
Point point2 = new Point();
projection.toPixels(myOverlays.get(i).getPoint(), point1);
System.out.println(">>>>>>>>>>>");
projection.toPixels(myOverlays.get(j++).getPoint(), point2);
System.out.println("Point == " + j);
path.moveTo(point2.x, point2.y);
path.lineTo(point1.x, point1.y);
canvas.drawPath(path, paint);
super.draw(canvas, mapView, shadow);
}
}
}
canvas.drawPath(path, paint);
super.draw(canvas, mapView, shadow);
}
}
注意 - 對于單個 locationChanged,draw 方法會被多次調用.
-
每當發生任何位置更改事件觸發時,我都可以繪制路線,但我在那里遇到了一個小問題.它也每次都從根目錄繪制路徑.我希望它從最后一個位置開始繪制路徑(即從最后一個位置連接到下一個位置).我附上一張圖片以便更好地理解.
I am able to draw the route whenever there is any location changed event fire but I am facing a small problem there. Its also drawing path from root every time. I want that from last place it should start drawing the path (i.e connecting from last to next location). I am attaching one image for better understanding.
從圖中可以看出,起點也與終點相連.我不想要這個.對此有任何想法.
From the image you can see that the starting point is also connected to last point. I don't want this. Any idea on this.
推薦答案
使用 MyLocationOverlay.您需要做的就是將此疊加層添加到您的 MapView.比如:
Current location can be easily drawn with MyLocationOverlay. What you need to do - is to add this overlay to your MapView. Something like:
mapView.addOverlay(new MyLocationOverlay(context, mapView));
默認位置點會自動繪制,一移動就會更新
The default location dot will be drawn automatically and will be updated as soon as you move
路線有點棘手,但仍然沒有火箭科學.您需要做的是擴展 Overlay 類并實現 draw()
邏輯.您需要跟蹤您旅行的 GeoPoints 并將它們投影到地圖上(在您的疊加層的 draw()
中,您可以向 mapView 詢問 Projection
實例 - getProjection()
.您可以使用此投影將 GeoPoints 映射到您的地圖坐標).一旦您擁有 GeoPoints 列表 - 您可以創建一個 Path 和在畫布上繪制此路徑
With route it is a bit trickier, but still there is no rocket science in there. What you need to do - is to extend Overlay class and implement draw()
logic. You need to keep track of GeoPoints you travelled and project them on a map (in your overlay's draw()
you can ask your mapView for a Projection
instance - getProjection()
. You can use this projection to map GeoPoints to your map coordinates).
As soon as you have list of GeoPoints - you can create a Path and draw this Path on a Canvas
如何在地圖上繪制路徑的簡化示例(我的項目中使用了一些自定義類,但你應該明白):
The simplified example how to draw path on a map (there are some custom classes I use in my project, but you should get an idea):
Path path;
@Override
public void draw(Canvas canvas, final MapView mapView, boolean shadow)
{
Projection proj = mapView.getProjection();
updatePath(mapView,proj);
canvas.drawPath(path, mPaint);
}
protected void updatePath(final MapView mapView, Projection proj)
{
path = new Path();
proj.toPixels(mPoints.get(0).point, p);
path.moveTo(p.x, p.y);
for(RouteEntity.RoutePoint rp : mPoints)
{
proj.toPixels(rp.point, mPoint);
path.lineTo(mPoint.x, mPoint.y);
}
}
這篇關于在 Android 中步行/駕駛時更新位置標記并在 Google 地圖上繪制路徑的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!