Select to view content in your preferred language

Not displaying polyline on the map

5798
5
03-11-2015 08:38 PM
thilibu
Deactivated User

Hello every one. I'm new to ArcGIS and I want to add path(polyline) on the map after  giving coordinates where to draw. I follow the guide Display Information -> add graphics and text. But not displaying the path and even not occur any error.

MapView mMapView = null;

    // The basemap switching menu items.

    MenuItem mStreetsMenuItem = null;

    MenuItem mTopoMenuItem = null;

    MenuItem mGrayMenuItem = null;

    MenuItem mOceansMenuItem = null;

    // Create MapOptions for each type of basemap.

    final MapOptions mTopoBasemap = new MapOptions(MapType.TOPO);

    final MapOptions mStreetsBasemap = new MapOptions(MapType.STREETS);

    final MapOptions mGrayBasemap = new MapOptions(MapType.GRAY);

    final MapOptions mOceansBasemap = new MapOptions(MapType.OCEANS);

    ArcGISTiledMapServiceLayer tms = new ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");

    // The current map extent, use to set the extent of the map after switching basemaps.

    Polygon mCurrentMapExtent = null;

    GraphicsLayer mLocationLayer;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_gis2);

        // Retrieve the map and initial extent from XML layout

        mMapView = (MapView) findViewById(R.id.map);

        mLocationLayer = new GraphicsLayer();

        mMapView.addLayer(tms);

        // Set the Esri logo to be visible, and enable map to wrap around date line.

        mMapView.setEsriLogoVisible(false);

        mMapView.enableWrapAround(true);

       SimpleLineSymbol lineSymbol = new SimpleLineSymbol(Color.GREEN, 3, SimpleLineSymbol.STYLE.DASH);

     Polyline lineGeometry = new Polyline();

     lineGeometry.startPath(-302557, 7570663);

     lineGeometry.lineTo(-302959, 7570868);

     lineGeometry.lineTo(-303042, 7571220);

     lineGeometry.lineTo(-302700, 7571803);

     lineGeometry.lineTo(-304043, 7576654);

     lineGeometry.lineTo(-300544, 7585289);

     lineGeometry.lineTo(-294365, 7592435);

     lineGeometry.lineTo(-290122, 7594445);

     lineGeometry.lineTo(-285283, 7595488); 

     Graphic lineGraphic = new Graphic(lineGeometry, lineSymbol);

     mLocationLayer.addGraphic(lineGraphic);

        // Set a listener for map status changes; this will be called when switching basemaps.

        mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {

            private static final long serialVersionUID = 1L;

            @Override

            public void onStatusChanged(Object source, STATUS status) {

                // Set the map extent once the map has been initialized, and the basemap is added

                // or changed; this will be indicated by the layer initialization of the basemap layer. As there is only

                // a single layer, there is no need to check the source object.

                if ((source == mMapView) && (status == STATUS.INITIALIZED)) {

                    boolean mIsMapLoaded = true;

                }

            }

        });

    }

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items from the Menu XML to the action bar, if present.

        getMenuInflater().inflate(R.menu.menu_gis2, menu);

        // Get the basemap switching menu items.

        mStreetsMenuItem = menu.getItem(0);

        mTopoMenuItem = menu.getItem(1);

        mGrayMenuItem = menu.getItem(2);

        mOceansMenuItem = menu.getItem(3);

        // Also set the topo basemap menu item to be checked, as this is the default.

        mTopoMenuItem.setChecked(true);

        return true;

    }

    @Override

    public boolean onOptionsItemSelected(MenuItem item) {

        // Save the current extent of the map before changing the map.

        // mCurrentMapExtent = mMapView.getExtent();

        // Handle menu item selection.

        switch (item.getItemId()) {

            case R.id.World_Street_Map:

                mMapView.setMapOptions(mStreetsBasemap);

                mStreetsMenuItem.setChecked(true);

                return true;

            case R.id.World_Topo:

                mMapView.setMapOptions(mTopoBasemap);

                mTopoMenuItem.setChecked(true);

                return true;

            case R.id.Gray:

                mMapView.setMapOptions(mGrayBasemap);

                mGrayMenuItem.setChecked(true);

                return true;

            case R.id.Ocean_Basemap:

                mMapView.setMapOptions(mOceansBasemap);

                mOceansMenuItem.setChecked(true);

                return true;

            default:

                return super.onOptionsItemSelected(item);

        }

    }

    @Override

    protected void onPause() {

        super.onPause();

        mMapView.pause();

    }

    @Override

    protected void onResume() {

        super.onResume();

        mMapView.unpause();

   }

Please help me on this. Thank you

0 Kudos
5 Replies
ÅsaWegelius
Deactivated User

I had the same problem (actually today like you ) and it occurs when the spatial references are not the same. This is how I solved it:

1. Make a field for the maps spatial reference

private SpatialReference mapSpatialReference;

2. Save the maps spatial reference in onCreateView. You need to do it this way to be sure the map is fully initialized before you try to get the value.

        mapView = (MapView) view.findViewById(R.id.map);

        mapView.setOnStatusChangedListener(new OnStatusChangedListener() {

            private static final long serialVersionUID = 1L;

            public void onStatusChanged(Object source, STATUS status) {

                if (OnStatusChangedListener.STATUS.INITIALIZED == status && source == mapView) {

                    mapSpatialReference = mapView.getSpatialReference();

                }

            }

        });

3. Check if the spatial references match and use a GeometryEngine to fix it if not.

        if(route != null) {

            locationDisplayManager.stop();

            for (Graphic graphic : route.getGraphics()) {

                if (route.getSpatialReference().equals(mapSpatialReference)) {

                    SimpleLineSymbol lineSymbol = new SimpleLineSymbol(R.color.orange, 5, SimpleLineSymbol.STYLE.SOLID);

                    Polyline poly = (Polyline)graphic.getGeometry();

                    Graphic routeGraphic = new Graphic(poly, lineSymbol);

                    myGraphicsLayer.addGraphic(routeGraphic);

                    mapView.setExtent(routeGraphic.getGeometry(), 15);

                }

                else{

                    SimpleLineSymbol lineSymbol = new SimpleLineSymbol(R.color.orange, 5, SimpleLineSymbol.STYLE.SOLID);

                    GeometryEngine engine = new GeometryEngine();

                    Polyline poly = (Polyline)engine.project(graphic.getGeometry(), route.getSpatialReference(), mapSpatialReference);

                    Graphic routeGraphic = new Graphic(poly, lineSymbol);

                    myGraphicsLayer.addGraphic(routeGraphic);

                    mapView.setExtent(routeGraphic.getGeometry(), 15);

                }

            }

Hope it helps.

Now I only have to find out why my polyline is black instead of orange and why the setExtent doesn't work. The  latter is probobly also related to the spatial reference.

thilibu
Deactivated User

thnx a lot.. Here route means object from Route or another?

ÅsaWegelius
Deactivated User

I just copied from a project I'm working on. The Route is a GPFeatureRecordSetLayer I get back from a service.

ÅsaWegelius
Deactivated User

I try show in your code instead:

    MapView mMapView = null;

    // The basemap switching menu items.

    MenuItem mStreetsMenuItem = null;

    MenuItem mTopoMenuItem = null;

    MenuItem mGrayMenuItem = null;

    MenuItem mOceansMenuItem = null;

    // Create MapOptions for each type of basemap.

    final MapOptions mTopoBasemap = new MapOptions(MapOptions.MapType.TOPO);

    final MapOptions mStreetsBasemap = new MapOptions(MapOptions.MapType.STREETS);

    final MapOptions mGrayBasemap = new MapOptions(MapOptions.MapType.GRAY);

    final MapOptions mOceansBasemap = new MapOptions(MapOptions.MapType.OCEANS);

// Here is the field for saving the maps spatial reference

    private SpatialReference mapSpatialReference;

    ArcGISTiledMapServiceLayer tms = new ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");

    // The current map extent, use to set the extent of the map after switching basemaps.

    Polygon mCurrentMapExtent = null;

    GraphicsLayer mLocationLayer;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_gis2);

        // Retrieve the map and initial extent from XML layout

        mMapView = (MapView) findViewById(R.id.map);

        mLocationLayer = new GraphicsLayer();

        mMapView.addLayer(tms);

        // Set the Esri logo to be visible, and enable map to wrap around date line.

        mMapView.setEsriLogoVisible(false);

        mMapView.enableWrapAround(true);

        // Set a listener for map status changes; this will be called when switching basemaps.

        mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {

            private static final long serialVersionUID = 1L;

            @Override

            public void onStatusChanged(Object source, STATUS status) {

                // Set the map extent once the map has been initialized, and the basemap is added

                // or changed; this will be indicated by the layer initialization of the basemap layer. As there is only

                // a single layer, there is no need to check the source object.

                if ((source == mMapView) && (status == STATUS.INITIALIZED)) {

                    boolean mIsMapLoaded = true;

                    mapSpatialReference = mMapView.getSpatialReference();

                    // if you need to check the maps spatial reference before you draw you do like this to ensure

                    //you don't draw before the map is initialized

                    writePolyline();

                }

            }

        });

    }

    public void writePolyline(){

        SimpleLineSymbol lineSymbol = new SimpleLineSymbol(Color.GREEN, 3, SimpleLineSymbol.STYLE.DASH);

        Polyline lineGeometry = new Polyline();

        lineGeometry.startPath(-302557, 7570663);

        lineGeometry.lineTo(-302959, 7570868);

        lineGeometry.lineTo(-303042, 7571220);

        lineGeometry.lineTo(-302700, 7571803);

        lineGeometry.lineTo(-304043, 7576654);

        lineGeometry.lineTo(-300544, 7585289);

        lineGeometry.lineTo(-294365, 7592435);

        lineGeometry.lineTo(-290122, 7594445);

        lineGeometry.lineTo(-285283, 7595488);

        GeometryEngine engine = new GeometryEngine();

        Graphic lineGraphic = new Graphic(lineGeometry, lineSymbol);

// here I get the maps corners to show that the polyline is way outside its boundaries so you either use points inside

// or finds the spatial reference from the map you got the points from and use an engine as I do below to projet it to the correct one

        Polygon mapExtent = mMapView.getExtent();

        System.out.println("mapViews extent " + mapExtent.toString() + " no of points " + mapExtent.getPointCount());

        for(int i = 0; i < mapExtent.getPointCount(); i++){

            System.out.println("point " + i +"  " + mapExtent.getPoint(i).getX() +  " x " + mapExtent.getPoint(i).getY() + " y ");

        }

        //PUT_YOUR POLYLINES SPATIAL REFERENCE WHERE IT IS SpatialReference.create(4326) NOW

       engine.project(lineGeometry, SpatialReference.create(4326), mapSpatialReference);

        mLocationLayer.addGraphic(lineGraphic);

    }

0 Kudos
thilibu
Deactivated User

thnk u very much..

0 Kudos