2012-05-02 43 views
0

当前位置图钉大小我有这样的方法来设置当前位置的图钉地图:安卓:根据精度

private void createPushPinOnMap(Canvas canvas, MapView mapView, BOMPushpin pushpin) { 
    Location location = currentBestLocation; 
    float accuracy = location.getAccuracy(); 

    Point pointPts = mapView.getProjection().toPixels(new GeoPoint(pushpin.getLatitude(), 
      pushpin.getLongtitude()), 
      null); 
    Bitmap bmp1 = BitmapFactory.decodeResource(
      context.getResources(), R.drawable.pushpin);    
    canvas.drawBitmap(bmp1, pointPts.x-10, pointPts.y-34, null); 

} 

但我需要更改此代码,所以图钉大小会莫名其妙地等于精度(像在maps.google.com位置图钉中)。

回答

0

为什么没有人告诉我,androids google api有一个名为MyLocation的类,它使得所有的东西都成为可能。

0

以下是我在GTL应用程序中使用的示例代码,稍微区别一点,我使用圆形而不是图钉。如果准确度发生变化,您需要将其设置在lastAccuracy中。 GPSSettings.IS_SHOW_ACCURACY_MARKER是真实的。

下面是代码

public class AccuracyOverlay extends Overlay { 
    private GeoPoint geoPoint; 
    private boolean isNeedClearCanvas = false; 
    private float lastAccuracy; 

    public void clearCanvas(Canvas canvas) { 
     canvas.drawColor(Color.WHITE); 
    } 

    private void drawAccuracyCircle(Canvas canvas, MapView mapView, GeoPoint geoPoint) { 
      // convert point to pixels 
     Point screenPoint = new Point(); 
     mapView.getProjection().toPixels(geoPoint, screenPoint); 

     Paint circlePaint = new Paint(); 
     circlePaint.setAntiAlias(true); 
     circlePaint.setStrokeWidth(2.0f); 
     circlePaint.setColor(0xff6666ff); 
     circlePaint.setStyle(Paint.Style.FILL_AND_STROKE); 
     circlePaint.setAlpha(64); 

     double lat = geoPoint.getLatitudeE6(); 
     int radius = (int) mapView.getProjection().metersToEquatorPixels(lastAccuracy); 
     canvas.drawCircle(screenPoint.x, screenPoint.y, radius, circlePaint); 

    } 

    public int metersToRadius(double latitude) { 
     return (int) (mapView.getProjection().metersToEquatorPixels(lastAccuracy) * (1/ Math.cos(Math.toRadians(latitude)))); 
    }  

    public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
     if (GPSSettings.IS_SHOW_ACCURACY_MARKER) { 
      if (!shadow && getLastLocation() != null) { 
       geoPoint = getLastLocation(); 
      } 

      if (geoPoint != null) { 
       if (GPSSettings.IS_SHOW_ACCURACY_MARKER && (lastAccuracy > 0)) { 
        drawAccuracyCircle(canvas, mapView, geoPoint); 
       } 
      } 
      if (isNeedClearCanvas) { 
       boolean wasOn = GPSSettings.IS_SHOW_ACCURACY_MARKER; 
       try { 
        try { 
         if (wasOn) 
          GPSSettings.IS_SHOW_ACCURACY_MARKER = false; 
         geoPoint = null; 
         canvas.drawColor(Color.WHITE); 
        } 
        finally { 
         if (wasOn) { 
          GPSSettings.IS_SHOW_ACCURACY_MARKER = true; 
         } 
        } 
       } 
       finally { 
        isNeedClearCanvas = false; 
       } 
      } 
     } 
    } 

    public AccuracyOverlay() { 
     super(); 
    } 


    public void clearMarks() { 
     this.isNeedClearCanvas = true; 
    } 

    public void setLastAccuracy(float lastAccuracy) { 
     this.lastAccuracy = lastAccuracy; 
    } 
}