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