我试图从Arcgis地图的toScreenPoint Api中获取点,但是它返回了我null。我不知道我在哪里犯错。请帮我。

这是我在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>


这是我进行API调用的方式:

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

}
}

最佳答案

MapView仅在onCreate返回后完成初始化。这意味着在调用toScreenPoint时,mMapView尚无足够的信息将映射点转换为屏幕点,反之亦然。

因此,将您的呼叫放在toScreenPoint内:

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
        }
    }

});


请注意,您必须将OnStatusChangedListener声明为p2

关于android - Arcgis Map的toScreenPoint Api返回null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20376978/

10-08 23:27