1

我正在Android平板电脑上开发一个应用程序,我有一个新要求,我需要找出一个点到另一个选定点之间的距离,谁能帮我实现这个

我发现在谷歌地图中,​​我们在实验室菜单中有一个名为“测量”的选项,我需要同样的要求,谁能告诉我如何在谷歌地图中调用/实现以编程方式测量的功能。

请检查下图以获得更好的理解。

等待您的宝贵意见...在此处输入图像描述

我正在使用下面的代码来显示当前的 gps 位置现在我需要知道如何指向其他位置并画线然后测量区域请检查我的代码并建议我

public class Tracking extends MapActivity implements LocationListener {

   LocationManager locman;
   LocationListener loclis;
   Location Location;
   private MapView map;

   List<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>();
   private MapController controller;
   String provider = LocationManager.GPS_PROVIDER;
   double lat;
   double lon;


   @Override
      public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         initMapView();
         initMyLocation();
         locman = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
         //locman.requestLocationUpdates(provider,60000, 100,loclis);
         //Location = locman.getLastKnownLocation(provider);

      }
   /** Find and initialize the map view. */
      private void initMapView() {
         map = (MapView) findViewById(R.id.mapView);
         controller = map.getController();
         map.setSatellite(false);
         map.setBuiltInZoomControls(true);
      }

      /** Find Current Position on Map. */
      private void initMyLocation() {
         final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
         overlay.enableMyLocation();
         overlay.enableCompass(); // does not work in emulator
         overlay.runOnFirstFix(new Runnable() {
            public void run() {
               // Zoom in to current location
               controller.setZoom(24);
               controller.animateTo(overlay.getMyLocation());
            }
         });
         map.getOverlays().add(overlay);
      }

   @Override
   public void onLocationChanged(Location location) {
      if (Location != null){
         lat = Location.getLatitude();
          lon = Location.getLongitude();
          GeoPoint New_geopoint = new GeoPoint((int)(lat*1e6),(int)(lon*1e6));
          controller.animateTo(New_geopoint);
          geoPointsArray.add(New_geopoint);


      }

   }
   public double getDistance(double lat1, double lon1, double lat2, double lon2) {
        double latA = Math.toRadians(lat1);
        double lonA = Math.toRadians(lon1);
        double latB = Math.toRadians(lat2);
        double lonB = Math.toRadians(lon2);
        double cosAng = (Math.cos(latA) * Math.cos(latB) * Math.cos(lonB-lonA)) +
                        (Math.sin(latA) * Math.sin(latB));
        double ang = Math.acos(cosAng);
        double dist = ang *6371;
        return dist;
    }

   class MyOverlay extends Overlay{
       public MyOverlay(){
       }   
       public void draw(Canvas canvas, MapView mapv, boolean shadow){
       super.draw(canvas, mapv, shadow);

        Projection projection = map.getProjection();
        Path p = new Path();
        for (int i = 0; i < geoPointsArray.size(); i++) {
        if (i == geoPointsArray.size() - 1) {
            break;
        }
        Point from = new Point();
        Point to = new Point();
        projection.toPixels(geoPointsArray.get(i), from);
        projection.toPixels(geoPointsArray.get(i + 1), to);
        p.moveTo(from.x, from.y);
        p.lineTo(to.x, to.y);
        }
        Paint mPaint = new Paint();
        mPaint.setStyle(Style.STROKE);
        mPaint.setColor(0xFFFF0000);
        mPaint.setAntiAlias(true);
        canvas.drawLine(0, 4,5,50, mPaint);
        canvas.drawPath(p, mPaint);
        super.draw(canvas, map, shadow);
    }   
}

   @Override
   public void onProviderDisabled(String provider) {
      // TODO Auto-generated method stub

   }
   @Override
   public void onProviderEnabled(String provider) {
      // TODO Auto-generated method stub

   }

   @Override
   protected boolean isRouteDisplayed() {
      // TODO Auto-generated method stub
      return false;
   }

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}}
4

2 回答 2

1

此链接可能会对您有所帮助

使用 gps 获取一个人走过的距离

GPS - 在跑步和步行时获取距离、时间

请通过链接。它详细解释

于 2012-05-11T06:10:10.987 回答
1

如果您想在两个位置之间绘制路径,则可以参考以下链接

J2ME/Android/BlackBerry - 行车路线,两个地点之间的路线

您也可以从这里获取源代码。

于 2012-05-12T09:10:25.227 回答