如何在IndoorAtlas Floorplan图像中显示当前位置?

时间:2015-05-30 06:59:45

标签: android android-layout indoor-positioning-system

我加载了室内地图集平面图,但我如何在该图像上显示我的位置.. 此图片也是静态的请帮助。

装载平面图的代码如下。

public void loadFloorPlanImage(FloorPlan floorPlan) 
{
    BitmapFactory.Options options = createBitmapOptions(floorPlan);
    FutureResult<Bitmap> result = mIndoorAtlas.fetchFloorPlanImage(floorPlan,options);
    result.setCallback(new ResultCallback<Bitmap>() {
        @Override
        public void onResult(final Bitmap result) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ImageView imageView = (ImageView)    findViewById(R.id.imageView);
                    imageView.setImageBitmap(result);
                }
        });
        }
    });
}

我得到的位置时间我只是在ImageView上标记我的代码的位置是..

public void onServiceUpdate(ServiceState state){

       double x1 = state.getMetricPoint().getX();
        double y1 = state.getMetricPoint().getY();
        float x = (float) x1;
        float y = (float) y1;

        //Paint paint = new Paint();
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);

        Bitmap workingBitmap = Bitmap.createBitmap(image);
        Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);

        Canvas canvas = new Canvas(mutableBitmap);
       // canvas.drawCircle(x, y, 100, paint);
        canvas.drawCircle(50, 50, 10, paint);

        imageView.setAdjustViewBounds(true);
        imageView.setImageBitmap(image);

}

但它不起作用。请帮忙谢谢

1 个答案:

答案 0 :(得分:0)

import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import com.indooratlas.android.CalibrationState;
import com.indooratlas.android.FloorPlan;
import com.indooratlas.android.FutureResult;
import com.indooratlas.android.IndoorAtlas;
import com.indooratlas.android.IndoorAtlasException;
import com.indooratlas.android.IndoorAtlasFactory;
import com.indooratlas.android.IndoorAtlasListener;
import com.indooratlas.android.ResultCallback;
import com.indooratlas.android.ServiceState;

/**
 * <p>
 * Activity to demonstrate basic use of IndoorAtlas SDK. If there are no public
 * maps around your location, you can create a map of your own and upload it to
 * IndoorAtlas servers. Read more on how to do this from:
 * http://developer.indooratlas.com.
 * </p>
 * <p/>
 * <p>
 * To run this demo, you will also need your applications API key/secret and
 * identifiers for the floor plan from http://developer.indooratlas.com.
 * </p>
 * <p/>
 */
public class MainActivity extends Activity implements IndoorAtlasListener {

    private static final String TAG = "MainActivity";

    // private ListView mLogView;
    // private LogAdapter mLogAdapter;

    private IndoorAtlas mIndoorAtlas;
    private boolean mIsPositioning;
    private StringBuilder mSharedBuilder = new StringBuilder();
    private String apiKey = "YOU API KEY";
    private String apiSecret = "YOUR SECRET KEY";
    private String floorPlanId = "CUSTOM FLOOR PLAN ID";
    private String venueId = "CUSTOM VENUE ID";
    private String floorId = "CUSTOM FLOOR ID";
    private FloorPlan mFloorPlan;
    private ImageView imageView1;
    private Bitmap bitmap;

    private Bitmap copy;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_layout);
        imageView1 = (ImageView) findViewById(R.id.imageView1);
        // mLogView = (ListView) findViewById(R.id.list);
        // mLogAdapter = new LogAdapter(this);
        // mLogView.setAdapter(mLogAdapter);

        initIndoorAtlas();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        tearDown();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_clear_log:
            // mLogAdapter.clear();
            return true;
        case R.id.action_toggle_positioning:
            togglePositioning();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    private void tearDown() {
        if (mIndoorAtlas != null) {
            mIndoorAtlas.tearDown();
        }
    }

    private void stopPositioning() {
        mIsPositioning = false;
        if (mIndoorAtlas != null) {
            log("Stop positioning");
            mIndoorAtlas.stopPositioning();
        }
    }

    private void startPositioning() {
        if (mIndoorAtlas != null) {
            log(String
                    .format("startPositioning, venueId: %s, floorId: %s, floorPlanId: %s",
                            venueId, floorId, floorPlanId));
            try {
                mIndoorAtlas.startPositioning(venueId, floorId, floorPlanId);
                mIsPositioning = true;
            } catch (IndoorAtlasException e) {
                log("startPositioning failed: " + e);
            }
        } else {
            log("calibration not ready, cannot start positioning");
        }
    }

    private void togglePositioning() {
        if (mIsPositioning) {
            stopPositioning();
        } else {
            startPositioning();
        }
    }

    private void initIndoorAtlas() {

        try {
            log("Connecting with IndoorAtlas, apiKey: " + apiKey);
            // obtain instance to positioning service, note that calibrating
            // might begin instantly
            mIndoorAtlas = IndoorAtlasFactory.createIndoorAtlas(
                    getApplicationContext(), this, // IndoorAtlasListener
                    apiKey, apiSecret);
            log("IndoorAtlas instance created");
            FutureResult<FloorPlan> result = mIndoorAtlas
                    .fetchFloorPlan(floorPlanId);
            result.setCallback(new ResultCallback<FloorPlan>() {

                @Override
                public void onResult(final FloorPlan result) {
                    mFloorPlan = result;
                    loadFloorPlanImage(result);
                }

                @Override
                public void onApplicationError(IndoorAtlasException arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onSystemError(IOException arg0) {
                    // TODO Auto-generated method stub

                }
                // handle error conditions too
            });
            try {
                Log.d("abcd", "helo14");
                mIndoorAtlas.startPositioning(venueId, floorId, floorPlanId);
                Log.d("abcd", "helo1");
            } catch (IndoorAtlasException e) {
                // TODO Auto-generated catch block
                Log.d("abcd", "helo");
                e.printStackTrace();
            }
            togglePositioning();
        } catch (IndoorAtlasException ex) {
            Log.e("IndoorAtlas", "init failed", ex);
            log("init IndoorAtlas failed, " + ex.toString());
        }

    }

    private void updateImageViewInUiThread(final Bitmap result) {
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                bitmap = result;
                imageView1.setImageBitmap(result);
            }
        });
    }

    void loadFloorPlanImage(FloorPlan floorPlan) {
        BitmapFactory.Options options = createBitmapOptions(floorPlan);
        FutureResult<Bitmap> result = mIndoorAtlas.fetchFloorPlanImage(
                floorPlan, options);
        result.setCallback(new ResultCallback<Bitmap>() {
            @Override
            public void onResult(final Bitmap result) {
                // now you have floor plan bitmap, do something with it
                updateImageViewInUiThread(result);
            }

            @Override
            public void onApplicationError(IndoorAtlasException arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onSystemError(IOException arg0) {
                // TODO Auto-generated method stub

            }
            // handle error conditions too
        });
    }

    private void log(final String msg) {
        Log.d(TAG, msg);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // mLogAdapter.add(msg);
                // mLogAdapter.notifyDataSetChanged();
            }
        });
    }

    /* IndoorAtlasListener interface */

    /**
     * This is where you will handle location updates.
     */
    @Override
    public void onServiceUpdate(ServiceState state) {
        Log.d("abcd", "testttt");
        final Bitmap icon = BitmapFactory.decodeResource(
                getApplicationContext().getResources(), R.drawable.loc_img);
        // TODO Auto-generated method stub
        final int i = state.getImagePoint().getI();
        final int j = state.getImagePoint().getJ(); 
        Log.d("abcd", "" + i + "  " + j);   
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (bitmap != null) {
                    Bitmap copy2 = bitmap.copy(bitmap.getConfig(), true);
                    Canvas canvas = new Canvas(copy2);
                    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
                    canvas.drawBitmap(icon, i, j, paint);
                    imageView1.setImageBitmap(copy2);
                }           
            }
        });
        mSharedBuilder.setLength(0);
        mSharedBuilder.append("Location: ").append("\n\troundtrip : ")
                .append(state.getRoundtrip()).append("ms").append("\n\tlat : ")
                .append(state.getGeoPoint().getLatitude()).append("\n\tlon : ")
                .append(state.getGeoPoint().getLongitude())
                .append("\n\tX [meter] : ")
                .append(state.getMetricPoint().getX())
                .append("\n\tY [meter] : ")
                .append(state.getMetricPoint().getY())
                .append("\n\tI [pixel] : ")
                .append(state.getImagePoint().getI())
                .append("\n\tJ [pixel] : ")
                .append(state.getImagePoint().getJ()).append("\n\theading : ")
                .append(state.getHeadingDegrees()).append("\n\tuncertainty: ")
                .append(state.getUncertainty());
        log(mSharedBuilder.toString());
    }

    private BitmapFactory.Options createBitmapOptions(FloorPlan floorPlan) {
        BitmapFactory.Options options = null;
        int reqWidth = 2048;
        int reqHeight = 2048;
        final int width = (int) floorPlan.dimensions[0];
        final int height = (int) floorPlan.dimensions[1];
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;            
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
        if (options != null) {
            options.inSampleSize = inSampleSize;
        }
        return options;

    }

    private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
        Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(),
                bmp1.getHeight(), bmp1.getConfig());
        Canvas canvas = new Canvas(bmOverlay);
        canvas.drawBitmap(bmp1, new Matrix(), null);
        canvas.drawBitmap(bmp2, new Matrix(), null);
        return bmOverlay;
    }

    @Override
    public void onServiceFailure(int errorCode, String reason) {
        log("onServiceFailure: reason : " + reason);
    }

    @Override
    public void onServiceInitializing() {
        log("onServiceInitializing");
    }

    @Override
    public void onServiceInitialized() {
        log("onServiceInitialized");
    }

    @Override
    public void onInitializationFailed(final String reason) {
        log("onInitializationFailed: " + reason);
    }

    @Override
    public void onServiceStopped() {
        log("onServiceStopped");
    }

    @Override
    public void onCalibrationStatus(CalibrationState calibrationState) {
        log("onCalibrationStatus, percentage: "
                + calibrationState.getPercentage());
    }

    /**
     * Notification that calibration has reached level of quality that provides
     * best possible positioning accuracy.
     */
    @Override
    public void onCalibrationReady() {
        log("onCalibrationReady");
    }

    @Override
    public void onNetworkChangeComplete(boolean success) {
    }

    /**
     * @deprecated this callback is deprecated as of version 1.4
     */
    @Override
    public void onCalibrationInvalid() {
    }

    /**
     * @deprecated this callback is deprecated as of version 1.4
     */
    @Override
    public void onCalibrationFailed(String reason) {
    }     
}

此代码可以帮助您..并参考此link