calculate distance between 2 points

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

calculate distance between 2 points

Postby gloomyson » Tue Jan 18, 2011 9:28 am

I want to create 2 geopoints and calculate distance between them. I'm very new in android program . What should I have to do next. here's the code:
Code: Select all
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class MapViewTest extends MapActivity {
   /** Called when the activity is first created. */

   MapView mapView;
   TextView positionTextView;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      mapView = (MapView) findViewById(R.id.map);
      positionTextView = (TextView) findViewById(R.id.positionTextView);
      mapView.setBuiltInZoomControls(true);
      MapController mc = mapView.getController();
      mc.setZoom(15);

      final GeoPoint p = new GeoPoint((int) (34.983338 * 1E6),
            (int) (138.406852 * 1E6));
      final GeoPoint q = new GeoPoint((int) (34.972531 * 1E6),
            (int) (138.38906 * 1E6));

      class MapOverlay extends com.google.android.maps.Overlay {
         @Override
         public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
               long when) {
            super.draw(canvas, mapView, shadow);

            // ---translate the GeoPoint to screen pixels---
            Point screenPts1 = new Point();
            mapView.getProjection().toPixels(p, screenPts1);

            Point screenPts2 = new Point();
            mapView.getProjection().toPixels(q, screenPts2);

            // ---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                  R.drawable.marker);
            canvas.drawBitmap(bmp, screenPts1.x - 15, screenPts1.y - 27,
                  null);
            canvas.drawBitmap(bmp, screenPts2.x - 15, screenPts2.y - 27,
                  null);
            return true;

         }
      }

      MapOverlay mapOverlay = new MapOverlay();
      List<Overlay> listOfOverlays = mapView.getOverlays();
      listOfOverlays.clear();
      listOfOverlays.add(mapOverlay);

      StringBuilder sb = new StringBuilder();
      mapView.getMapCenter();
      sb.append("経度=");
      sb.append(((double) p.getLatitudeE6()) / 1E6);
      sb.append("     ");
      sb.append("緯度=");
      sb.append(((double) p.getLongitudeE6()) / 1E6);
      sb.append("\n");
      sb.append("経度=");
      sb.append(((double) q.getLatitudeE6()) / 1E6);
      sb.append("     ");
      sb.append("緯度=");
      sb.append(((double) q.getLongitudeE6()) / 1E6);
      sb.append("\n");
      
      positionTextView.setText(sb.toString());
      // mapView.setTraffic(true);

      mapView.invalidate();
      
   }
}
gloomyson
Freshman
Freshman
 
Posts: 2
Joined: Tue Jan 18, 2011 9:16 am

Top

Re: calculate distance between 2 points

Postby gloomyson » Tue Jan 25, 2011 7:07 am

help me plzzzz :cry: . Now I had 2 geopoints and their markers on map. I want to calculate distance between them. Thank you
gloomyson
Freshman
Freshman
 
Posts: 2
Joined: Tue Jan 18, 2011 9:16 am

Re: calculate distance between 2 points

Postby puxdroid » Sat Feb 12, 2011 12:53 am

Hi!

Use this code:

Code: Select all

public static double EARTH_RADIUS_KM = 6384;// km

public static double calculateDistanceMeters(double aLong, double aLat,
         double bLong, double bLat) {

      double d2r = (Math.PI / 180);

      double dLat = (bLat - aLat) * d2r;
      double dLon = (bLong - aLong) * d2r;
      double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
            + Math.cos(aLat * d2r) * Math.cos(bLat * d2r)
            * Math.sin(dLon / 2) * Math.sin(dLon / 2);
      double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

      return EARTH_RADIUS_KM * c * 1000;

   }


Probably this web page will help you for other questions like that ;)

http://www.movable-type.co.uk/scripts/latlong.html
puxdroid
Developer
Developer
 
Posts: 49
Joined: Wed Nov 25, 2009 11:57 am

Re: calculate distance between 2 points

Postby JoxTraex » Sat Feb 19, 2011 12:49 am

Thanks for the link puxdroid.
JoxTraex
Developer
Developer
 
Posts: 25
Joined: Wed Jan 26, 2011 12:29 am

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 6 guests