2

I am developing anAndrpoid app with ArcGIS Runtime SDK .

I want to show set the map orientation always to north direction.

I am trying to implemet this on onclick event of an image button.

My try

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.LocationDisplay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.panenviron.pocarcmap.sketching.SketchGraphicsOverlay;
import com.panenviron.pocarcmap.sketching.SketchGraphicsOverlayEventListener;



public class MainActivity extends AppCompatActivity {

    private MapView mMapView;
    private SketchGraphicsOverlay mSketchGraphicsOverlay;
    private ImageButton mPointButton, mPolylineButton, mPolygonButton, mUndoButton, mRedoButton, mClearButton,_ImgBtnCompass;
    private ImageView _showMyLoc;
    private LocationDisplay mLocationDisplay;
    private int requestCode = 2;
    String[] reqPermissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission
            .ACCESS_COARSE_LOCATION};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // inflate MapView from layout
        mMapView = (MapView) findViewById(R.id.mapView);
        // create a map with the Basemap Type topographic
        //ArcGISMap mMap = new ArcGISMap(Basemap.Type.LIGHT_GRAY_CANVAS,lat, long, zoom);
        ArcGISMap mMap = new ArcGISMap(Basemap.Type.LIGHT_GRAY_CANVAS, 8.494911, 76.956755, 10);
        // set the map to be displayed in this view
        mMapView.setMap(mMap);
        // Create a new SketchGraphicsOverlay with a new listener
        mSketchGraphicsOverlay = new SketchGraphicsOverlay(mMapView, new MySketchGraphicsOverlayEventListener());

        // Get references to all of the bottom action bar bottoms for highlighting and disabling/enabling
        mPointButton = (ImageButton)findViewById(R.id.pointButton);
        mPointButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showMyLoc();
            }
        });
        _ImgBtnCompass = (ImageButton)findViewById(R.id.ImgBtnCompass);
        _ImgBtnCompass.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mLocationDisplay = mMapView.getLocationDisplay();
                mLocationDisplay.setAutoPanMode(LocationDisplay.AutoPanMode.COMPASS_NAVIGATION);
            }
        });
        //mPolylineButton = (ImageButton)findViewById(R.id.polylineButton);
        mPolygonButton = (ImageButton)findViewById(R.id.polygonButton);

        // Disable the undo, redo, and clear button to start with
        mUndoButton = (ImageButton)findViewById(R.id.undoButton);
        mUndoButton.setClickable(false);
        mUndoButton.setEnabled(false);

        mRedoButton = (ImageButton)findViewById(R.id.redoButton);
        mRedoButton.setClickable(false);
        mRedoButton.setEnabled(false);

        mClearButton = (ImageButton)findViewById(R.id.clearButton);
        mClearButton.setClickable(false);
        mClearButton.setEnabled(false);



        mLocationDisplay = mMapView.getLocationDisplay();
        mLocationDisplay.addDataSourceStatusChangedListener(new LocationDisplay.DataSourceStatusChangedListener() {
            @Override
            public void onStatusChanged(LocationDisplay.DataSourceStatusChangedEvent dataSourceStatusChangedEvent) {

                // If LocationDisplay started OK, then continue.
                if (dataSourceStatusChangedEvent.isStarted())
                    return;

                // No error is reported, then continue.
                if (dataSourceStatusChangedEvent.getError() == null)
                    return;

                // If an error is found, handle the failure to start.
                // Check permissions to see if failure may be due to lack of permissions.
                boolean permissionCheck1 = ContextCompat.checkSelfPermission(MainActivity.this, reqPermissions[0]) ==
                        PackageManager.PERMISSION_GRANTED;
                boolean permissionCheck2 = ContextCompat.checkSelfPermission(MainActivity.this, reqPermissions[1]) ==
                        PackageManager.PERMISSION_GRANTED;

                if (!(permissionCheck1 && permissionCheck2)) {
                    // If permissions are not already granted, request permission from the user.
                    ActivityCompat.requestPermissions(MainActivity.this, reqPermissions, requestCode);
                } else {
                    // Report other unknown failure types to the user - for example, location services may not
                    // be enabled on the device.
                    String message = String.format("Error in DataSourceStatusChangedListener: %s", dataSourceStatusChangedEvent
                            .getSource().getLocationDataSource().getError().getMessage());
                    Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();

                    // Update UI to reflect that the location display did not actually start

                }
            }
        });
        showMyLoc();

    }
    public void showMyLoc(){
        mLocationDisplay.setAutoPanMode(LocationDisplay.AutoPanMode.RECENTER);
        if (!mLocationDisplay.isStarted())
            mLocationDisplay.startAsync();
    }
    @Override
    protected void onPause(){
        mMapView.pause();
        super.onPause();
    }

    @Override
    protected void onResume(){
        super.onResume();
        mMapView.resume();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    /**
     * When the point button is clicked, show it as selected and enable point drawing mode.
     *
     * @param v the button view
     */
    public void pointClick(View v) {
        if (!v.isSelected()) {
            v.setSelected(true);
            mSketchGraphicsOverlay.setDrawingMode(SketchGraphicsOverlay.DrawingMode.POINT);
        } else {
            mSketchGraphicsOverlay.setDrawingMode(SketchGraphicsOverlay.DrawingMode.NONE);
        }
    }

    /**
     * When the polyline button is clicked, show it as selected and enable polyline drawing mode.
     *
     * @param v the button view
     */
    public void polylineClick(View v) {
        if (!v.isSelected()) {
            v.setSelected(true);
            mSketchGraphicsOverlay.setDrawingMode(SketchGraphicsOverlay.DrawingMode.POLYLINE);
        } else {
            mSketchGraphicsOverlay.setDrawingMode(SketchGraphicsOverlay.DrawingMode.NONE);
        }
    }

    /**
     * When the polygon button is clicked, show it as selected and enable polygon drawing mode.
     *
     * @param v the button view
     */
    public void polygonClick(View v) {
        if (!v.isSelected()) {
            v.setSelected(true);
            mSketchGraphicsOverlay.setDrawingMode(SketchGraphicsOverlay.DrawingMode.POLYGON);
        } else {
            mSketchGraphicsOverlay.setDrawingMode(SketchGraphicsOverlay.DrawingMode.NONE);
        }
    }

    /**
     * When the undo button is clicked, undo the last event on the SketchGraphicsOverlay.
     *
     * @param v the button view
     */
    public void undoClick(View v) {
        mSketchGraphicsOverlay.undo();
    }

    /**
     * When the redo button is clicked, redo the last undone event on the SketchGraphicsOverlay.
     *
     * @param v the button view
     */
    public void redoClick(View v) {
        mSketchGraphicsOverlay.redo();
    }

    /**
     * When the clear button is clicked, clear all graphics on the SketchGraphicsOverlay.
     *
     * @param v the button view
     */
    public void clearClick(View v) {
        mSketchGraphicsOverlay.clear();
    }

    /**
     * Event listener for the SketchGraphicsOverlay that listens for state changes on the undo, redo, and
     * clear capabilities, as well as finished drawings, to control the enabled/disabled/selected state
     * of the various buttons.
     */
    private class MySketchGraphicsOverlayEventListener implements SketchGraphicsOverlayEventListener {

        @Override
        public void onUndoStateChanged(boolean undoEnabled) {
            // Set the undo button's enabled/disabled state based on the event boolean
            mUndoButton.setEnabled(undoEnabled);
            mUndoButton.setClickable(undoEnabled);
        }

        @Override
        public void onRedoStateChanged(boolean redoEnabled) {
            // Set the redo button's enabled/disabled state based on the event boolean
            mRedoButton.setEnabled(redoEnabled);
            mRedoButton.setClickable(redoEnabled);
        }

        @Override
        public void onClearStateChanged(boolean clearEnabled) {
            // Set the clear button's enabled/disabled state based on the event boolean
            mClearButton.setEnabled(clearEnabled);
            mClearButton.setClickable(clearEnabled);
        }

        @Override
        public void onDrawingFinished() {
            // Reset the selected state of the drawing buttons when a drawing is finished
           // mPointButton.setSelected(false);
            showMyLoc();
            //mPolylineButton.setSelected(false);
            mPolygonButton.setSelected(false);
        }
    }
    /*@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_compass:
                listenerOnMapPinch.resetCompass();

            default:
                break;
        }
        // TODO Auto-generated method stub

    }*/
}

Here the display of map and other functionalities are working perfectly.

Please suggest some way for this.I have googled this issue but im not able to find the solution for this.

1

You set the rotation of the map to North with mMapView.setViewpointRotationAsync(0); Take a look at the map rotation sample for an example of using a SeekBar to set the rotation angle. Then you can look at our maps-app to see a working example of a compass button/icon that responds to taps and re-orients the MapView to North.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.