【问题标题】:osmdroid, set a custom icon in a mapviewosmdroid,在地图视图中设置自定义图标
【发布时间】:2016-09-25 17:20:16
【问题描述】:

我的应用正在使用本地服务,该服务提供不断变化的位置。 这些位置已添加到我的 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();
    }
  }
}

请帮忙!

问候维姬

【问题讨论】:

  • 根据我的经验,osmdroid 不允许您更改现有点的图标。唯一的解决办法就是删除再删除再添加

标签: android osmdroid


【解决方案1】:

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

或者你可以尝试使用这个构造函数:

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

并定义您自己的“ResourceProxy”。

【讨论】:

    【解决方案2】:

    感谢您的建议。 现在我找到了解决办法。
    我已经注释掉了负责标准图标的指令“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);
    }
    

    问候维姬

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-19
      • 1970-01-01
      • 2011-10-15
      • 2012-05-10
      • 1970-01-01
      相关资源
      最近更新 更多