1

我在我的应用程序中使用 OSM 地图。我正在为 osmdroid 使用 3.0.8 jar 文件。我在地图上绘制自定义标记时遇到问题,它会在 2-3 秒后绘制。我盯着它找到了一个使用库 3.0.5 osmdroid 的解决方案。当我尝试时,它给出了android依赖项失败的错误。所以请给我方法,我可以解决这个问题。

这是我绘制自定义标记的代码。

public class Map extends Activity {
GoogleMap gMap;
static int loginCheck = 0;
ConnectionDetector conDec;
ArrayList<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
SharedPreferences prefs;
LatLng latLng;
MarkerOptions markerOptions;
LinearLayout botlay;
EditText desc;
MyItemizedOverlay myItemizedOverlay = null;
MyLocationOverlay myLocationOverlay = null;
ArrayList<OverlayItem> anotherOverlayItemArray, overlayItemArray;
GeoPoint p, loc, currentLocationPixels, t;
GeoPoint myPoint1;
Projection proj;
GPSTracker gps;
double my_Latitude, my_Longitude;
private MapView mapView;
private MapController myMapController;
public int count = 0;
EditText ed1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map1);

    ed1 = (EditText) findViewById(R.id.descr);

    getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

    mapView = (MapView) findViewById(R.id.mapViewosm);
    mapView.setBuiltInZoomControls(true);
    mapView.setMultiTouchControls(true);

    myMapController = mapView.getController();
    myMapController.setZoom(15);

    ScaleBarOverlay myScaleBarOverlay = new ScaleBarOverlay(this);
    mapView.getOverlays().add(myScaleBarOverlay);

    Drawable marker = getResources().getDrawable(
            android.R.drawable.star_big_on);
    int markerWidth = marker.getIntrinsicWidth();
    int markerHeight = marker.getIntrinsicHeight();
    marker.setBounds(0, markerHeight, markerWidth, 0);

    ResourceProxy resourceProxy = new DefaultResourceProxyImpl(
            getApplicationContext());

    myItemizedOverlay = new MyItemizedOverlay(marker, resourceProxy);

    // add overlay for current location..RAJ
    MyCurrentItemizedOverlay myCurrentLocationOverlay = new MyCurrentItemizedOverlay(
            marker, resourceProxy);
    mapView.getOverlays().add(myCurrentLocationOverlay);

    mapView.getOverlays().add(myItemizedOverlay);

    // mapView.postInvalidate();
    gps = new GPSTracker(Map.this);

    // check if GPS enabled
    if (gps.canGetLocation()) {

        while (gps.getLatitude() == 0.0 || gps.getLongitude() == 0.0) {
            gps.canGetLocation();
        }

        my_Latitude = gps.getLatitude();
        my_Longitude = gps.getLongitude();
        // \n is for new line
        // Toast.makeText(getApplicationContext(),
        // "Your Location is - \nLat: " + my_Latitude + "\nLong: " +
        // my_Longitude, Toast.LENGTH_LONG).show();
    } else {
        // can't get location
        // GPS or Network is not enabled
        // Ask user to enable GPS/network in settings
        gps.showSettingsAlert();
    }
    // set the current loaction for clicked location..
    p = new GeoPoint((int) (my_Latitude * 1E6), (int) (my_Longitude * 1E6));
    // add overlay for current location..RAJ
    currentLocationPixels = new GeoPoint((int) (my_Latitude * 1E6),
            (int) (my_Longitude * 1E6));
    // its mine..
    Toast.makeText(
            getApplicationContext(),
            "Your Location is - \nLat: " + my_Latitude + "\nLong: "
                    + my_Longitude, Toast.LENGTH_LONG).show();
    // ConvertPointToLocation(currentLocationPixels);

    myLocationOverlay = new MyLocationOverlay(this, mapView);
    // mapView.getOverlays().add(myLocationOverlay);
    // myLocationOverlay.enableMyLocation();
    // myLocationOverlay.getMyLocation();

    mapView.invalidate();

    // String coordinates[] = {
    // myLocationOverlay.getMyLocation().getLatitudeE6()+"",
    // myLocationOverlay.getMyLocation().getLatitudeE6()+"" };

    // double lat = (double)
    // myLocationOverlay.getMyLocation().getLatitudeE6();
    // double lng = (double)
    // myLocationOverlay.getMyLocation().getLongitudeE6();
    // //
    // //
    // p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));

    // mapView.postInvalidate();

    // double lat = (double) loc.getLatitudeE6();
    // double lng = (double) loc.getLongitudeE6();

    // addLocation(my_Latitude, my_Longitude);

    // myLocationOverlay.runOnFirstFix(new Runnable() {
    // public void run() {
    // mapView.getController().animateTo(
    // myLocationOverlay.getMyLocation());
    // }
    // });
    // mapView.getOverlays().add(touchOverlay);

}

// private void addLocation(double lat, double lng) {
// // ---Add a location marker---
//
// p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
//
// Drawable marker = getResources().getDrawable(
// android.R.drawable.star_big_on);
// int markerWidth = marker.getIntrinsicWidth();
// int markerHeight = marker.getIntrinsicHeight();
// marker.setBounds(0, markerHeight, markerWidth, 0);
//
// ResourceProxy resourceProxy = new DefaultResourceProxyImpl(
// getApplicationContext());
//
// myItemizedOverlay = new MyItemizedOverlay(marker, resourceProxy);
// mapView.getOverlays().add(myItemizedOverlay);
//
// List<Overlay> listOfOverlays = mapView.getOverlays();
// listOfOverlays.clear();
// listOfOverlays.add(myItemizedOverlay);
// mapView.invalidate();
// }

@Override
protected void onResume() {
    super.onResume();
    myLocationOverlay.enableMyLocation();
    myLocationOverlay.enableFollowLocation();
}

@Override
protected void onPause() {
    super.onPause();
    myLocationOverlay.disableMyLocation();
    myLocationOverlay.disableFollowLocation();
}

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();

    public MyItemizedOverlay(Drawable pDefaultMarker,
            ResourceProxy pResourceProxy) {
        super(pDefaultMarker, pResourceProxy);
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean arg2) {
        super.draw(canvas, mapView, arg2);

        // ---translate the GeoPoint to screen pixels---
        Point screenPts = new Point();
        mapView.getProjection().toPixels(p, screenPts);
        // ---add the marker---
        if (count == 1) {
            int caller = getIntent().getIntExtra("button", 0);
            switch (caller) {
            case R.id.btMap:
                Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                        R.drawable.pin_annotation_darkblue);
                canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
                break;
            case R.id.imageButton1:
                Bitmap bmp1 = BitmapFactory.decodeResource(getResources(),
                        R.drawable.pin_annotation_green);
                canvas.drawBitmap(bmp1, screenPts.x, screenPts.y - 50, null);
                break;
            case R.id.imageButton2:
                Bitmap bmp2 = BitmapFactory.decodeResource(getResources(),
                        R.drawable.pin_annotation_bue);
                canvas.drawBitmap(bmp2, screenPts.x, screenPts.y - 50, null);
                break;
            case R.id.imageButton3:
                Bitmap bmp3 = BitmapFactory.decodeResource(getResources(),
                        R.drawable.pin_annotation_light);
                canvas.drawBitmap(bmp3, screenPts.x, screenPts.y - 50, null);
                break;
            case R.id.imageButton4:
                Bitmap bmp4 = BitmapFactory.decodeResource(getResources(),
                        R.drawable.pin_annotation_purple);
                canvas.drawBitmap(bmp4, screenPts.x, screenPts.y - 50, null);
                break;
            case R.id.imageButton5:
                Bitmap bmp5 = BitmapFactory.decodeResource(getResources(),
                        R.drawable.pin_annotation_red);
                canvas.drawBitmap(bmp5, screenPts.x, screenPts.y - 50, null);
                break;
            case R.id.imageButton6:
                Bitmap bmp6 = BitmapFactory.decodeResource(getResources(),
                        R.drawable.pin_annotation_yellow);
                canvas.drawBitmap(bmp6, screenPts.x, screenPts.y - 50, null);
                break;
            }
        }

        // Bitmap bmp = BitmapFactory.decodeResource(getResources(),
        // R.drawable.pin_annotation_green);
        // if (count == 1) {
        // canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
        // }
    }

    public void addItem(GeoPoint p, String title, String snippet) {
        OverlayItem newItem = new OverlayItem(title, snippet, p);
        overlayItemList.add(newItem);
        populate();
    }

    @Override
    public boolean onSnapToItem(int arg0, int arg1, Point arg2,
            IMapView arg3) {
        return false;
    }

    @Override
    protected OverlayItem createItem(int arg0) {
        return overlayItemList.get(arg0);
    }

    @Override
    public int size() {
        return overlayItemList.size();
    }

    @Override
    public boolean onLongPress(MotionEvent e, MapView mapView) {

        proj = mapView.getProjection();
        loc = (GeoPoint) proj.fromPixels((int) e.getX(), (int) e.getY());
        ConvertPointToLocation(loc);
        return true;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e, MapView mapView) {
        count = 1;
        Projection proj = mapView.getProjection();
        p = (GeoPoint) proj.fromPixels((int) e.getX(), (int) e.getY());
        // ConvertPointToLocation(p);
        return true;
    }

}

public class MyCurrentItemizedOverlay extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();

    public MyCurrentItemizedOverlay(Drawable pDefaultMarker,
            ResourceProxy pResourceProxy) {
        super(pDefaultMarker, pResourceProxy);
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean arg2) {
        super.draw(canvas, mapView, arg2);

        // ---translate the GeoPoint to screen pixels---
        Point screenPts = new Point();
        mapView.getProjection().toPixels(currentLocationPixels, screenPts);

        // ---add the marker---
        Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.pin_annotation_current_location);

        canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);

    }

    public void addItem(GeoPoint p, String title, String snippet) {
        OverlayItem newItem = new OverlayItem(title, snippet, p);
        overlayItemList.add(newItem);
        populate();
    }

    @Override
    public boolean onSnapToItem(int arg0, int arg1, Point arg2,
            IMapView arg3) {
        return false;
    }

    @Override
    protected OverlayItem createItem(int arg0) {
        return overlayItemList.get(arg0);
    }

    @Override
    public int size() {
        return overlayItemList.size();
    }

}

// Method for convert the lat & long into Address
public String ConvertPointToLocation(GeoPoint point) {
    String address = "";
    Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
    try {
        List<Address> addresses = geoCoder.getFromLocation(
                point.getLatitudeE6() / 1E6, point.getLongitudeE6() / 1E6,
                1);

        if (addresses.size() > 0) {
            for (int index = 0; index < addresses.get(0)
                    .getMaxAddressLineIndex(); index++)
                address += addresses.get(0).getAddressLine(index) + " ";
        }
        Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT)
                .show();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return address;
}

public void btHome(View v) {
    startActivity(new Intent(Map.this, JamInfo.class));
}

public void btMap(View v) {

}

public void btReport(View v) {
    startActivity(new Intent(Map.this, Report.class));
}

public void btSetting(View v) {
    startActivity(new Intent(Map.this, Setting.class));
}

}

4

1 回答 1

3

我要尝试的第一件事是停止在每个绘制周期分配位图。您的 draw() 方法应该尽可能快,并且在该方法中分配对象肯定会减慢速度 - 特别是如果垃圾收集器必须运行。您应该在构造函数中创建所有这些位图并将它们存储在变量中。此外,如果可以避免,建议您不要在每个绘制周期调用 toPixels() - 仅在位置更改并存储它时调用它(并在缩放级别更改时更新它)。

查看示例应用程序OpenStreetMapViewer,了解如何使用 osmdroid 的一些精心设计的示例。

于 2013-10-25T13:30:50.687 回答