2

I am using OSM maps in my App. I am using 3.0.8 jar file for osmdroid. i have an issue with when i draw custom marker on Map it draw after 2-3 seconds. I goggled it find a solution that use library 3.0.5 osmdroid. when i tried it gives error that android dependencies failed. so please give me way that can i solve this problem.

here is my code for draw a custom marker.

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));
}

}

3
  • Delete 3.0.8 jar from your libs folder. Copy 3.0.5 jar to libs folder. Clean your project.
    – ramaral
    Oct 25, 2013 at 12:54
  • 1
    i tried it. but it not works for me :( Oct 25, 2013 at 12:57
  • 1
    At Project properties delete any references to 3.0.8 jar
    – ramaral
    Oct 25, 2013 at 13:03

1 Answer 1

3

The first thing I would try to is to stop allocating Bitmaps every draw cycle. Your draw() methods should be as fast as possible and allocating objects in that method will surely slow things down - especially if the garbage collector has to run. You should create all those Bitmaps in the constructor and store them in variables. Also it is recommended that you don't call toPixels() every draw cycle if you can avoid it - call it only when the location changes and store it (and update it if the zoom level changes).

Take a look at the sample application OpenStreetMapViewer for some well-crafted examples of how to use osmdroid.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.