I am new in the field. I am trying to develop an android app which is able to perform some spacial operations like
plot a poin, Dream a line, Polygon
But my problem is how can i get the lat and long values of a point that i plotted in the map.
I am referring the code is available on GitHub.
But i am not able to get the lat and long of a point.Please suggest some methods or code example to get this.
My code
Gradle Module app
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.panenviron.pocarcmap"
minSdkVersion 17
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/LGPL2.1'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
}
dependencies {
compile 'com.esri.arcgisruntime:arcgis-android:100.0.0'
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.2.0'
}
Gradle project
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
final def var = repositories {
jcenter()
maven{ url 'https://esri.bintray.com/arcgis' }
}
var
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Mainactivity.java
package com.panenviron.pocarcmap;
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.PointCollection;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.LocationDisplay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.panenviron.pocarcmap.sketching.SketchGraphicsOverlay;
import com.panenviron.pocarcmap.sketching.SketchGraphicsOverlayEventListener;
import java.util.ArrayList;
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};
Point TempmCurrentPointCollection;
Button _PlotNextPoint;
EditText _EdtTxtDirection,_EdtTxtDistance;
ArrayList<String> MyPolyGonList;
ArrayList<ArrayList<String>> MyMultiPolyGonList;
@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();
_EdtTxtDirection=(EditText)findViewById(R.id.EdtTxtDirection);
_EdtTxtDistance=(EditText)findViewById(R.id.EdtTxtDistance);
_PlotNextPoint=(Button)findViewById(R.id.PlotNextPoint);
_PlotNextPoint.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String tempEdtTxtDirection=_EdtTxtDirection.getText().toString();
String tempEdtTxtDistance=_EdtTxtDistance.getText().toString();
Toast.makeText(MainActivity.this, "Distance : "+tempEdtTxtDistance+" , " +
"Direction : "+tempEdtTxtDirection, Toast.LENGTH_LONG).show();
Point currentPoint=TempmCurrentPointCollection;
// create color and symbols for drawing graphics
SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.TRIANGLE, Color.BLUE, 14);
SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.CROSS, Color.BLUE, null);
SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.BLUE, 3);
//Point pt = new Point( lat,long, SpatialReferences.getWgs84());
Point pt = new Point(8565757.475800,947771.572170, SpatialReferences.getWgs84());
GraphicsOverlay overlay = new GraphicsOverlay();
mMapView.getGraphicsOverlays().add(overlay);
overlay.getGraphics().add(new Graphic(createPoint(pt), markerSymbol));
overlay.getGraphics().add(new Graphic(createPolyline(currentPoint,pt), lineSymbol));
//mMapView.setViewpointGeometryAsync(createEnvelope(), getResources().getDimension(R.dimen.viewpoint_padding));
//pointClick(mMapView);
//moveGeodetic (List<Point> points, double distance, LinearUnit distanceUnit, double azimuth, AngularUnit azimuthUnit, GeodeticCurveType curveType)
//mMapView.moveGeodetic(currentPoint,);
}
});
MyPolyGonList=new ArrayList<>();
MyMultiPolyGonList=new ArrayList<>();
}
private Point createPoint(Point point) {
Log.e(" point--", String.valueOf(point));
//[DocRef: Name=Create Point, Category=Fundamentals, Topic=Geometries]
// create a Point using x,y coordinates and a SpatialReference
Point pt = new Point(point.getX(), point.getY(), SpatialReferences.getWgs84());
//[DocRef: END]
return pt;
}
private Polyline createPolyline(Point one,Point two) {
Toast.makeText(MainActivity.this, "createPolyline---", Toast.LENGTH_LONG).show();
//[DocRef: Name=Create Polyline, Category=Fundamentals, Topic=Geometries]
// create a Polyline from a PointCollection
PointCollection borderCAtoNV = new PointCollection(SpatialReferences.getWgs84());
borderCAtoNV.add(one.getX(), one.getY());
borderCAtoNV.add(two.getX(), two.getY());
Polyline polyline = new Polyline(borderCAtoNV);
//[DocRef: END]
/* PointCollection borderCAtoNV = new PointCollection(SpatialReferences.getWgs84());
borderCAtoNV.add(76.956755,8.494911);
borderCAtoNV.add(76.856755,8.694911);
borderCAtoNV.add(76.456755,8.994911);
Polyline polyline = new Polyline(borderCAtoNV);*/
return polyline;
}
private Envelope createEnvelope() {
//[DocRef: Name=Create Envelope, Category=Fundamentals, Topic=Geometries]
// create an Envelope using minimum and maximum x,y coordinates and a SpatialReference
Envelope envelope = new Envelope( 12.0,78.0,9.0, 75.0, SpatialReferences.getWgs84());
//[DocRef: END]
return envelope;
}
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);
//mPolylineButton.setSelected(false);
Toast.makeText(MainActivity.this, "im at draw finished", Toast.LENGTH_LONG).show();
/*ArrayList<ArrayList<String>> rowList = new ArrayList<>();
rowList.add(MyPolyGonList);*/
MyMultiPolyGonList.add(MyPolyGonList);
Log.e("MyMultiPolyGonList--",MyMultiPolyGonList.toString());
mPolygonButton.setSelected(false);
}
@Override
public Point getThePoint(Point point){
Toast.makeText(MainActivity.this, "point="+point, Toast.LENGTH_LONG).show();
TempmCurrentPointCollection=point;
MyPolyGonList.add(point.toJson());
Log.e("MyPolyGonList--",MyPolyGonList.toString());
Log.e(" point.x--", String.valueOf(point.getX()));
Log.e(" point.y--", String.valueOf(point.getY()));
Log.e(" point.m--", String.valueOf(point.getM()));
Log.e(" point.json--", point.toJson());
return point;
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="5"
android:hint="Distance"
android:id="@+id/EdtTxtDistance"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="5"
android:hint="Direction"
android:id="@+id/EdtTxtDirection"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="plot"
android:id="@+id/PlotNextPoint"/>
</LinearLayout>
<!-- MapView -->
<LinearLayout
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="fill_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<com.esri.arcgisruntime.mapping.view.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="match_parent">
</com.esri.arcgisruntime.mapping.view.MapView>
</LinearLayout>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:background="#fff"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<ImageButton
android:contentDescription="@string/pointButtonDescription"
android:id="@+id/pointButton"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/current_loc" />
<ImageButton
android:contentDescription="@string/polylineButtonDescription"
android:id="@+id/ImgBtnCompass"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/pointButton"
android:layout_toRightOf="@+id/pointButton"
android:layout_width="wrap_content"
android:src="@drawable/compass"/>
<ImageButton
android:id="@+id/polygonButton"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/ImgBtnCompass"
android:layout_toRightOf="@+id/ImgBtnCompass"
android:layout_width="wrap_content"
android:src="@drawable/polygon"
android:onClick="polygonClick"/>
<ImageButton
android:contentDescription="@string/undoButtonDescription"
android:id="@+id/undoButton"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/polygonButton"
android:layout_toRightOf="@+id/polygonButton"
android:layout_width="wrap_content"
android:src="@drawable/undo"
android:onClick="undoClick"/>
<ImageButton
android:contentDescription="@string/redoButtonDescription"
android:id="@+id/redoButton"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/undoButton"
android:layout_toRightOf="@+id/undoButton"
android:layout_width="wrap_content"
android:src="@drawable/redo"
android:onClick="redoClick"/>
<ImageButton
android:contentDescription="@string/clearButtonDescription"
android:id="@+id/clearButton"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/redoButton"
android:layout_toRightOf="@+id/redoButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/delete"
android:onClick="clearClick"/>
</RelativeLayout>
</HorizontalScrollView>
</LinearLayout>
SketchGraphicsOverlay.java
code is here
SketchGraphicsOverlayEventListener.java
code is here