简体   繁体   English

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

[英]How do I create a direction blue dot like Google Map using OSMDROID?

在此处输入图片说明

I am using open street Map tool of osmdroid我正在使用 osmdroid 的开放街道地图工具

How can I create a blue dot with direction pointing base on direction bearing?如何创建一个基于方向指向的蓝点?

I only find out how to add marker in OSMDROID, what is the blue dot call in osmdroid?我只知道如何在 OSMDROID 中添加标记,osmdroid 中的蓝点调用是什么?

I did this a few years ago, I don't know if Osmdroid has changed in the mean time.我几年前就这样做了,我不知道 Osmdroid 是否同时发生了变化。 I was overriding overlay and its draw method, inserting my bitmap.我正在覆盖overlay及其draw方法,插入我的位图。

As I said, this is old code, check with the current API before you use it.正如我所说,这是旧代码,请在使用之前检查当前的 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) {

}

} }

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

Step 2: download osmand and get icons from osmand;第二步:下载osmand并从osmand获取图标;

  • blue arrow (map_navigation_default)蓝色箭头(map_navigation_default)
  • or similar to the icon above (map_location_default)或类似于上面的图标(map_location_default)

Step 3: Change DirectedLocationOverlay icon:第 3 步:更改 DirectedLocationOverlay 图标:

    myLocationOverlay = new DirectedLocationOverlay(this);
  • first choice, Single bitmap;首选,单位图;

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

     // 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));
  • or third option, similar to the icon above;或第三个选项,类似于上面的图标;

     //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));
  • change icon;更改图标;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM