2013-10-25 67 views
1

我在我的應用程序中使用OSM貼圖。我爲osmdroid使用3.0.8 jar文件。我有一個問題,當我在地圖上繪製自定義標記時,它會在2-3秒後繪製。我瞪着它找到一個使用庫3.0.5 osmdroid的解決方案。當我嘗試它給出的錯誤,Android依賴關係失敗。所以請給我方式,我可以解決這個問題。OpenStreetMap上的標記繪製緩慢

這裏是我繪製自定義標記的代碼。

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

}

+0

從您的libs文件夾中刪除3.0.8 jar。將3.0.5 jar複製到libs文件夾。清理你的項目。 – ramaral

+1

我試過了。但它不適用於我:( –

+1

在項目屬性刪除任何引用3.0.8 jar – ramaral

回答

3

的第一件事,我會嘗試是停止分配位圖的每個繪製循環。您的draw()方法應該儘可能快,並且在該方法中分配對象肯定會減慢速度 - 尤其是在垃圾收集器必須運行的情況下。您應該在構造函數中創建所有這些位圖並將它們存儲在變量中。此外,如果您可以避開它,建議您不要在每個繪製週期內調用像素(),只有在位置發生變化並存儲它(並在縮放級別更改時更新它)時才調用它。

查看示例應用程序OpenStreetMapViewer,瞭解如何使用osmdroid的一些精心製作的示例。