osmdroid,在mapview中设置自定义图标

时间:2015-09-19 14:24:33

标签: android osmdroid

我的应用正在使用本地服务,该服务提供不断变化的位置。 这些位置将添加到我的ArrayList:

static List<Location> weg = new ArrayList<Location>();

WegAnsehenActivity现在显示连接点的路径。
我的目标:我想改变最后一个当前点的路径上的图标 完成:我想更改最后一个位置的绿色箭头
我的问题:我怎么能做到这一点?

import org.osmdroid.api.IMapController;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.PathOverlay;
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay;
.
.
.
public class WegAnsehenActivity extends Activity {
  MapView mapView;
  IMapController mapController;
  MyLocationNewOverlay o;
  Paint paint;
  PathOverlay overlay;
  //filled by the local service, method onLocationChanged
  static List<Location> weg = new ArrayList<Location>();

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mapview);
    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setClickable(true);
    mapView.setBuiltInZoomControls(true);
    mapView.setTileSource(TileSourceFactory.MAPQUESTOSM);
    mapController = mapView.getController();
    //false keeps the mapView from loading online tiles using network connection!!!
    mapView.setUseDataConnection(false);//true);
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(7);
    paint.setColor(Color.BLUE);
    overlay = new PathOverlay(Color.BLUE, this);
    overlay.setPaint(paint);
    o = new MyLocationNewOverlay(getApplicationContext(), mapView);
  }
  @Override
  protected void onResume() {
    super.onResume();
    mapController.setZoom(16);
    if (weg.size() >= 1) {
      for (int i = 0; i < weg.size(); i++) {
        if (i==weg.size()-1) {
          mapController.setCenter(new GeoPoint(weg.get(i).getLatitude(), weg.get(i).getLongitude()));
        }
        GeoPoint point = new GeoPoint(weg.get(i));
        overlay.addPoint(point);
      }
      mapView.getOverlayManager().add(overlay);
      o.enableMyLocation(); //shows a green arrow I want to replace
      mapView.getOverlays().add(o);
      mapView.postInvalidate();
    }
  }
}

请帮忙!

关心Wicki

2 个答案:

答案 0 :(得分:0)

你可以做的是子类MyLocationNewOverlay,并在你的构造函数中设置你自己的位图(mPersonBitmap和mDirectionArrowBitmap)。

或者您可以尝试使用此构造函数:

public MyLocationNewOverlay(IMyLocationProvider myLocationProvider, 
    MapView mapView, ResourceProxy resourceProxy)

并定义您自己的&#34; ResourceProxy&#34;。

答案 1 :(得分:0)

感谢您的建议。 现在我找到了解决方案 我已经注释掉了“o.enableMyLocation();”指令,它负责标准图标 它取代了最后4条指令:

mapView.getOverlayManager().add(overlay);
// o.enableMyLocation(); //shows the green-arrow-marker (my own position)
mapView.getOverlays().add(o);
setMarker(); //set an own marker
mapView.postInvalidate();
.
.
void setMarker() {
  Drawable marker = getResources().getDrawable(R.drawable.icon);
  ItemizedIconOverlay<OverlayItem> overlay = new ItemizedIconOverlay<OverlayItem>(
          new ArrayList<OverlayItem>(), marker, null,
          new DefaultResourceProxyImpl(this));
  // gc: last GeoPoint
  OverlayItem item = new OverlayItem(null, null, gc);
  overlay.addItem(item);
  mapView.getOverlays().add(overlay);
}

关心Wicki