0

我正在使用 Android 地图,我需要在其中添加指南针功能,即当我单击指南针按钮时,它会显示单独的指南针。任何人都可以指导我正确的方向或参考任何教程。我需要单独添加 Compass 提前感谢阅读和回复

4

2 回答 2

1

我的 Cass 中的场景是单独使用使用指南针点击指南针按钮时它会显示

  • 从当前位置(我的位置)到目标位置的距离
  • 罗盘
  • 指南针朝北方向所以为了实现所有这些,我使用了以下方法希望这将帮助任何与我有相同情况的人

我的班级的代码片段将获得全球两点之间的距离并将图像显示为指南针,头朝北

public class MapController extends MapActivity implements SensorEventListener {
    public static GeoPoint Mypoint, MapPoint;
    double km, meter;
    public static int kmInDec, meterInDec;
    private SensorManager mSensorManager;
    private Sensor mAccelerometer, mField;
    Location myLoc, mapLoc;
    double myLat, myLong, mapLat, mapLong;

    public static double lat, lng;
    public static double mylat = Double.parseDouble(Constants.DEVICE_OBJ.lat);//31.567615d;

    public static double mylon = Double.parseDouble(Constants.DEVICE_OBJ.lng);//74.360962d;
    com.google.android.maps.MapController mapController;
    public static boolean enabled = false;

    @Override
    protected void onCreate(Bundle icicle) {
        // TODO Auto-generated method stub
        super.onCreate(icicle);
        setContentView(R.layout.map_layout);
        Log.v(Constants.TAG, "In Map Controller");

        fetchUI();
    }

    public void fetchUI() {
        myLoc = new Location("Test");
        mapLoc = new Location("Test");
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mField = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        myLat = (31.567615 * 1e6) / 1E6;
        myLong = (74.360962d * 1e6) / 1E6;
        mapLat = (MapPoint.getLatitudeE6()) / 1E6;
        mapLong = (MapPoint.getLongitudeE6()) / 1E6;
        myLoc.setLatitude(31.567615);
        myLoc.setLongitude(74.360962);
        mapLoc.setLatitude(mapLat);
        mapLoc.setLongitude(mapLong);
    }


    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
        mSensorManager.registerListener(this, mField, SensorManager.SENSOR_DELAY_UI);
    }

    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
    }


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


    ///Method to calculate the Distance between 2 Geo Points aroud the Globe
    public double CalculationByDistance(GeoPoint StartP, GeoPoint EndP) {
        double lat1 = StartP.getLatitudeE6() / 1E6;
        double lat2 = EndP.getLatitudeE6() / 1E6;
        double lon1 = StartP.getLongitudeE6() / 1E6;
        double lon2 = EndP.getLongitudeE6() / 1E6;
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.asin(Math.sqrt(a));
        double valueResult = Radius * c;
        double km = valueResult / 1;
        DecimalFormat newFormat = new DecimalFormat("####");
        kmInDec = Integer.valueOf(newFormat.format(km));
        meter = valueResult % 1000;
        meterInDec = Integer.valueOf(newFormat.format(meter));
        Log.i("Radius Value", "" + valueResult + "  KM  " + kmInDec + " Meter   " + meterInDec);

        return Radius * c;
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        if (myLoc == null) return;

        float azimuth = event.values[0];
        float baseAzimuth = azimuth;

        GeomagneticField geoField = new GeomagneticField(Double
                .valueOf(myLoc.getLatitude()).floatValue(), Double
                .valueOf(myLoc.getLongitude()).floatValue(),
                Double.valueOf(myLoc.getAltitude()).floatValue(),
                System.currentTimeMillis());

        azimuth -= geoField.getDeclination(); // converts magnetic north into true north

        // Store the bearingTo in the bearTo variable
        float bearTo = myLoc.bearingTo(mapLoc);

        // If the bearTo is smaller than 0, add 360 to get the rotation clockwise.
        if (bearTo < 0) {
            bearTo = bearTo + 360;
        }

        //This is where we choose to point it
        float direction = bearTo - azimuth;

        // If the direction is smaller than 0, add 360 to get the rotation clockwise.
        if (direction < 0) {
            direction = direction + 360;
        }

        rotateImageView(compasView, R.drawable.app_icon, direction);

        //Set the field
        String bearingText = "N";

        if ((360 >= baseAzimuth && baseAzimuth >= 337.5) || (0 <= baseAzimuth && baseAzimuth <= 22.5))
            bearingText = "N";
        else if (baseAzimuth > 22.5 && baseAzimuth < 67.5) bearingText = "NE";
        else if (baseAzimuth >= 67.5 && baseAzimuth <= 112.5) bearingText = "E";
        else if (baseAzimuth > 112.5 && baseAzimuth < 157.5) bearingText = "SE";
        else if (baseAzimuth >= 157.5 && baseAzimuth <= 202.5) bearingText = "S";
        else if (baseAzimuth > 202.5 && baseAzimuth < 247.5) bearingText = "SW";
        else if (baseAzimuth >= 247.5 && baseAzimuth <= 292.5) bearingText = "W";
        else if (baseAzimuth > 292.5 && baseAzimuth < 337.5) bearingText = "NW";
        else bearingText = "?";

        Distance.setText(kmInDec + "  Km " + " " + meterInDec + " m");
    }
    ////This is the method to Show the Image as Compass

    private void rotateImageView(ImageView imageView, int drawable, float rotate) {

        // Decode the drawable into a bitmap
        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
                drawable);

        // Get the width/height of the drawable
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = bitmapOrg.getWidth(), height = bitmapOrg.getHeight();

        // Initialize a new Matrix
        Matrix matrix = new Matrix();

        // Decide on how much to rotate
        rotate = rotate % 360;

        // Actually rotate the image
        matrix.postRotate(rotate, width, height);

        // recreate the new Bitmap via a couple conditions
        Bitmap rotatedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);
        //BitmapDrawable bmd = new BitmapDrawable( rotatedBitmap );

        //imageView.setImageBitmap( rotatedBitmap );
        imageView.setImageDrawable(new BitmapDrawable(getResources(), rotatedBitmap));
        imageView.setScaleType(ScaleType.CENTER);
    }
}
于 2013-01-17T05:17:33.170 回答
0

使用本指南: 谷歌地图教程

搜索指南针,按下指南针(叠加)时尝试根据您的需要启动新活动

反正教程很全面,相信能解决你的大部分需求=)。

于 2013-01-16T11:32:53.790 回答