使用类MyLocationOverlay时无法获取geopoint

时间:2012-02-07 16:30:05

标签: android google-maps mylocationoverlay

我无法标记当前位置...应用程序始终获取消息“无法确定位置”。 我在三星galaxy mini上运行应用程序,我打开GPS :(

Class FixedMyLocationOverlay

  

public class FixedMyLocationOverlay扩展MyLocationOverlay {         private boolean bugged = false;         私人Drawable drawable;         私人油漆精度;         私人点中心;         私人Point离开;         private int width;         private int height;

  public FixedMyLocationOverlay(Context context, MapView mapView) {
      super(context, mapView);
  }

  @Override
  protected void drawMyLocation(Canvas canvas, MapView mapView,
          Location lastFix, GeoPoint myLocation, long when) {
      if(!bugged) {
          try {
              super.drawMyLocation(canvas, mapView, lastFix, myLocation, when);
          } catch (Exception e) {
              // we found a buggy phone, draw the location icons ourselves
              bugged = true;
          }
      }

      if(bugged) {
          if(drawable == null) {

              accuracyPaint = new Paint();
              accuracyPaint.setAntiAlias(true);
              accuracyPaint.setStrokeWidth(2.0f);

              drawable = mapView.getContext().getResources().getDrawable(R.drawable.here);
              width = drawable.getIntrinsicWidth();
              height = drawable.getIntrinsicHeight();
              center = new Point();
              left = new Point();
          }

          Projection projection = mapView.getProjection();
          double latitude = lastFix.getLatitude();
          double longitude = lastFix.getLongitude();
          float accuracy = lastFix.getAccuracy();

          float[] result = new float[1];

          Location.distanceBetween(latitude, longitude, latitude, longitude + 1, result);
          float longitudeLineDistance = result[0];

          GeoPoint leftGeo = new GeoPoint((int)(latitude*1e6), (int)((longitude-accuracy/longitudeLineDistance)*1e6));
          projection.toPixels(leftGeo, left);
          projection.toPixels(myLocation, center);
          int radius = center.x - left.x;

          accuracyPaint.setColor(0xff6666ff);
          accuracyPaint.setStyle(Style.STROKE);
          canvas.drawCircle(center.x, center.y, radius, accuracyPaint);

          accuracyPaint.setColor(0x186666ff);
          accuracyPaint.setStyle(Style.FILL);
          canvas.drawCircle(center.x, center.y, radius, accuracyPaint);

          drawable.setBounds(center.x - width/2, center.y - height/2, center.x + width/2, center.y + height/2);
          drawable.draw(canvas);
      }
  }
}

代码活动

  

public class atm_atmogan扩展了MapActivity {

  private MapView mapView;
  private MyLocationOverlay myLocationOverlay;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.atm_atmogan); 


      mapView = (MapView) findViewById(R.id.map_view);
      mapView.setBuiltInZoomControls(true);
      myLocationOverlay = new FixedMyLocationOverlay(this, mapView);

      mapView.getOverlays().add(myLocationOverlay);
      mapView.postInvalidate();
      zoomToMyLocation();
    }

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

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


  private void zoomToMyLocation() {
      GeoPoint myLocationGeoPoint = myLocationOverlay.getMyLocation();
      if(myLocationGeoPoint != null) {
          mapView.getController().animateTo(myLocationGeoPoint);
          mapView.getController().setZoom(18);
      }
      else {
          Toast.makeText(this, "Cannot determine location", Toast.LENGTH_SHORT).show();
      }
  }

  @Override
  protected boolean isRouteDisplayed() {
      return false;
  }
}

1 个答案:

答案 0 :(得分:0)

您需要使用位置管理器类/接口获取您的实际位置。

本教程将介绍您需要了解的有关http://www.vogella.de/articles/AndroidLocationAPI/article.html

的所有信息

希望这会有所帮助