I am trying to get the points from the Arcgis map's toScreenPoint
Api but its returning me null
. I don't know where I am making a mistake. Please help me.
Here is my map in xml:
<com.esri.android.map.MapView
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/footer"
initExtent="-19332033.11, -3516.27, -1720941.80, 11737211.28"
>
</com.esri.android.map.MapView>
And here is how I am making an API call:
package com.example.demo;
public class DemoActivity extends Activity implements OnClickListener {
private Button mSearch;
private Button mDirection;
private MapView mMapView;
private final String map_url ="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSearch = (Button)findViewById(R.id.search);
mDirection = (Button)findViewById(R.id.directions);
/* mMapView = new MapView(this);_PARENT,
LinearLayout.Layout
mMapView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCHParams.MATCH_PARENT));*/
mMapView = (MapView)findViewById(R.id.map);
// Add dynamic layer to MapView
mMapView.addLayer(new ArcGISTiledMapServiceLayer(map_url));
GraphicsLayer gLayer = new GraphicsLayer();
// Add empty GraphicsLayer
mMapView.addLayer(gLayer,1);
//-----------------------
// create a simple marker symbol to be used by our graphic
PictureMarkerSymbol pms = new PictureMarkerSymbol(getResources().getDrawable(R.drawable.ic_launcher));
Point p1 = convertToEsri(-1720941.80,11737211.28);
Point p2 = new Point(-1720941.80,11737211.28);
System.out.println("screen point :"+mMapView.toScreenPoint(p2)); // returns null ,why?
gLayer.removeAll();
Graphic g = new Graphic(p1, pms);
gLayer.addGraphic(g);
mSearch.setOnClickListener(this);
mDirection.setOnClickListener(this);
}
/*
* Returns the ESRI std point for the given x and y
*/
private Point convertToEsri(double x, double y) {
Point wgsPoint = new Point();
Rect rect = new Rect();
mMapView.getDrawingRect(rect);
wgsPoint.setX(x);
wgsPoint.setY(y);
Point projectionPoint = (Point) GeometryEngine.project(wgsPoint, SpatialReference.create(4326), mMapView.getSpatialReference());
return projectionPoint;
}
@Override
public void onPause() {
super.onPause();
mMapView.pause();
}
@Override
public void onResume() {
super.onResume();
mMapView.unpause();
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
The MapView
only finishes initializing after onCreate
returns. That means that when you're calling toScreenPoint
, mMapView
doesn't yet have enough information to convert a map point to a screen point or vice versa.
Therefore, put your call to toScreenPoint
inside an OnStatusChangedListener
:
final Point p2 = new Point(-1720941.80, 11737211.28);
mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {
@Override
public void onStatusChanged(Object source, STATUS status) {
if (STATUS.INITIALIZED.equals(status)) {
System.out.println("screen point :"
+ mMapView.toScreenPoint(p2));//no longer null
}
}
});
Note that you'll have to declare p2
as final
.