我正在开发一个计算两点之间路线的应用程序。下面给出的代码可以计算两点之间的路线。但我无法以图形方式在地图上显示它。
代码:
private final String routeTaskURL = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Network/USA/NAServer/Route";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMapView = (MapView) findViewById(R.id.map);
addGraphicLayers();
mMapView.enableWrapAround(true);
new Thread(new Runnable() {
@Override
public void run() {
try {
UserCredentials userCredentials = new UserCredentials();
userCredentials.setUserToken(token, clientID);
RouteTask routeTask = RouteTask.createOnlineRouteTask(routeTaskURL, userCredentials);
RouteParameters routeParameters = routeTask.retrieveDefaultRouteTaskParameters();
NAFeaturesAsFeature naFeatures = new NAFeaturesAsFeature();
Point startPoint = new Point(-119.866896, 36.793653);
Point stopPoint = new Point(-119.853345, 36.795488);
StopGraphic startPnt = new StopGraphic(startPoint);
StopGraphic stopPnt = new StopGraphic(stopPoint);
naFeatures.setFeatures(new Graphic[] {startPnt, stopPnt});
routeParameters.setStops(naFeatures);
RouteResult mResults = routeTask.solve(routeParameters);
List<Route> routes = mResults.getRoutes();
System.out.println(mResults.getRoutes());
Route mRoute = routes.get(0);
Geometry geometry = mRoute.getRouteGraphic().getGeometry();
Graphic symbolGraphic = new Graphic(geometry, new SimpleLineSymbol(Color.BLUE, 10));
mLocationLayer.addGraphic(symbolGraphic);
System.out.println(mResults.getStops());
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
void addGraphicLayers() {
// Add location layer
if (mLocationLayer == null) {
mLocationLayer = new GraphicsLayer();
}
mMapView.addLayer(mLocationLayer);
// Add the route graphic layer
if (mRouteLayer == null) {
mRouteLayer = new GraphicsLayer();
}
mMapView.addLayer(mRouteLayer);
}
@Override
protected void onPause() {
super.onPause();
mMapView.pause();
}
@Override
protected void onResume() {
super.onResume();
mMapView.unpause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, 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 super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// 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);
}
}
注意:mlocationLayer和mRouteLayer都是在第一步中声明的。
答案 0 :(得分:1)
您需要进行两项更改。 首先,当你添加你的图形图层时,底图图层可能已经添加,也可能没有添加。 (剧透警报:它没有。它是异步的,所以在技术上它可以是任何一种方式,但很明显内存图形层比基于Web服务的图层更快创建。)添加的最后一层位于顶部,因此您的底图图层可能(扰乱警报:将)在你的图形图层之上并隐藏它们。为确保在底图后添加图形图层,添加OnStatusChangedListener
:
mMapView = (MapView) findViewById(R.id.map);
mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {
@Override
public void onStatusChanged(Object o, STATUS status) {
if (STATUS.INITIALIZED.equals(status)) {
addGraphicLayers();
mMapView.enableWrapAround(true);
new Thread(new Runnable() {
//Your thread code as you wrote it
}).start();
}
}
});
其次,路线几何体具有与地图不同的空间参考。 route service的默认输出空间参考是WGS 1984,a.k.a。4326,a.k.a。纬度和经度。但ArcGIS Online底图服务 - 例如World Topo Map - 位于Web Mercator,a.k.a。3857或102100.有两种方法可以解决这个问题。一种方法是使用GeometryEngine
将几何体从4326投影到3857.但更好的方法是告诉路线服务您想要哪个空间参考。这是通过添加以下行来完成的获取RouteParameters
对象后的代码:
routeParameters.setOutSpatialReference(mMapView.getSpatialReference());