如何使用OSMDROID创建像Google Map这样的方向蓝点?

时间:2018-01-28 10:12:20

标签: java openstreetmap osmdroid

enter image description here

我正在使用osmdroid的开放街道地图工具

如何创建一个蓝点,其方向指向方向轴承?

我只知道如何在OSMDROID中添加标记,osmdroid中的蓝点调用是什么?

2 个答案:

答案 0 :(得分:0)

几年前我做过这个,我不知道Osmdroid的平均时间是否有变化。我正在覆盖overlay及其draw方法,插入我的位图。

正如我所说,这是旧代码,请在使用前检查当前的API。

public class MyLocationOverlay extends Overlay implements MyLocationListener {

private static final String TAG = MyLocationOverlay.class.getSimpleName();

private BoundedMapView mapView;
private Point point = new Point();
private Bitmap myLocationMarker;
private Bitmap haloMarker;
private Location myLocation;
private Bitmap bm;
private MyLocationManager locationManager;

public MyLocationOverlay(Drawable pDefaultMarker, ResourceProxy pResourceProxy,
        Context context, MapView mapView, MyLocationManager locationManager,
        BitmapUtils bitmapUtils) {
    super(pResourceProxy);
    this.mapView = (BoundedMapView) mapView;

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inMutable = true;

    myLocationMarker = bitmapUtils.rotateIcon(BitmapFactory.decodeResource(
            context.getResources(), R.drawable.trackingdot, options));
    haloMarker = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.trackingdothalo);
    myLocation = locationManager.getMyLocation();
    this.locationManager = locationManager;
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {

    /*
     * draw my location
     */
    if (myLocation != null) {
        try {
            point = mapView.getProjection().toPixels(
                    new GeoPoint(myLocation.getLatitude(), myLocation.getLongitude()), point);
            canvas.drawBitmap(myLocationMarker, point.x - (myLocationMarker.getWidth() / 2),
                    point.y - (myLocationMarker.getWidth() / 2), null);

            int size = (int) mapView.getProjection().metersToEquatorPixels(
                    myLocation.getAccuracy());

            if (bm != null) {
                bm.recycle();
            }
            if (size > 40) {
                bm = Bitmap.createScaledBitmap(haloMarker, size, size, false);
                canvas.drawBitmap(bm, point.x - (bm.getWidth() / 2), point.y
                        - (bm.getHeight() / 2), null);
            }
        } catch (OutOfMemoryError e) {
            Log.d(TAG, e.getMessage() == null ? "OutOfMemoryError" : e.getMessage());
        } catch (IllegalArgumentException e) {
            Log.d(TAG, e.getMessage() == null ? "IllegalArgumentException" : e.getMessage());
        }

    }
}

@Override
public void onLocationChanged(Location location) {
    myLocation = location;
    mapView.postInvalidate();
}

@Override
public void onProviderDisabled(String provider) {
    if (LocationManager.GPS_PROVIDER.equalsIgnoreCase(provider)) {
        myLocation = null;
    }
}

@Override
public void onProviderEnabled(String provider) {
    if (LocationManager.GPS_PROVIDER.equalsIgnoreCase(provider)) {
        myLocation = locationManager.getMyLocation();
    }
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    if (LocationManager.GPS_PROVIDER.equalsIgnoreCase(provider)
            && status == LocationProvider.AVAILABLE) {
        myLocation = locationManager.getMyLocation();
    }
}

@Override
public void onExitEnterPark(InParkStatus inParkStatus) {

}

}

答案 1 :(得分:0)

第一步:安装 osmonuspack (https://github.com/MKergall/osmbonuspack/wiki/Tutorial_0)

第2步:下载osmand并从osmand获取图标;

  • 蓝色箭头(map_navigation_default)
  • 或类似于上面的图标 (map_location_default)

第 3 步:更改 DirectedLocationOverlay 图标:

    myLocationOverlay = new DirectedLocationOverlay(this);
  • 首选,单位图;

      // 1-Single Bitmap
      Drawable d = ResourcesCompat.getDrawable(getResources(), R.drawable.direction_arrow, null);
      Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
    
  • 或第二个选项,蓝色箭头;

      // 2-Single LayerDrawable - Blue Arrow
      LayerDrawable ld = (LayerDrawable)getResources().getDrawable(R.drawable.map_navigation_default);
      final int width = ld.getIntrinsicWidth();
      final int height = ld.getIntrinsicHeight();
      final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
      ld.setBounds(0, 0, width, height);
      ld.draw(new Canvas(bitmap));
    
  • 或第三个选项,类似于上面的图标;

       //3- Double layerdrawable
      Drawable bottom = ContextCompat.getDrawable(this, R.drawable.map_location_default_view_angle);
      Drawable top = ContextCompat.getDrawable(this, R.drawable.map_location_default);
      final LayerDrawable ld = new LayerDrawable(new Drawable[]{bottom, top});
      final int width = ld.getIntrinsicWidth();
      final int height = ld.getIntrinsicHeight();
      final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
      ld.setBounds(0, 0, width, height);
      ld.draw(new Canvas(bitmap));
    
  • 更改图标;

      myLocationOverlay.setDirectionArrow(bitmap);
      map.getOverlays().add(myLocationOverlay);