【发布时间】:2015-06-26 20:59:16
【问题描述】:
我是 arcgis 和 esri 地图的新手,我正在尝试在两点之间建立路线, 这是我的代码:
map.setOnLongPressListener(new OnLongPressListener() {
private static final long serialVersionUID = 1L;
public boolean onLongPress(final float x, final float y) {
grahpicslayer.removeAll();
final Point loc = map.toMapPoint(x, y);
grahpicslayer.addGraphic(new Graphic(loc, new SimpleMarkerSymbol(Color.RED, 20, SimpleMarkerSymbol.STYLE.CIRCLE)));
//Display the red color diamond graphics
map.addLayer(grahpicslayer);
//zoom the map to the location
map.zoomToResolution(loc, 20.0);
new Thread(new Runnable() {
@Override
public void run() {
// new code
try {
String CLIENT_SECRET = "";
String CLIENT_ID = "";
//Prepare user credentials
UserCredentials userCredentials = new UserCredentials();
userCredentials.setAuthenticationType(UserCredentials.AuthenticationType.TOKEN);
userCredentials.setSSLRequired(false);
userCredentials.setUserToken(CLIENT_ID, CLIENT_SECRET);
/*initialize RouteTask*/
String routeTaskURL = "https://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";
RouteTask rt = RouteTask.createOnlineRouteTask(routeTaskURL, userCredentials);
RouteParameters rp = rt.retrieveDefaultRouteTaskParameters();
NAFeaturesAsFeature rfaf = new NAFeaturesAsFeature();
// Convert point to EGS (decimal degrees)
Point p = (Point) GeometryEngine.project(loc, wm,
egs);
//mlocation is your latitude and longitude point provided by GPS location in decimal degrees
StopGraphic point1 = new StopGraphic(mLocation);
StopGraphic point2 = new StopGraphic(p);
String message = "point 1 == " + ((point1 == null) ? "null" : point1.getName()) + "point 2 == " + ((point2 == null) ? "null" : point2.getName());
Message msg = Message.obtain(); // Creates an new Message instance
msg.obj = message; // Put the string into Message, into "obj" field.
msg.setTarget(h); // Set the Handler
msg.sendToTarget(); //Send the message
rfaf.setFeatures(new Graphic[]{point1, point2});
rfaf.setCompressedRequest(true);
rp.setStops(rfaf);
rp.setOutSpatialReference(wm);
RouteResult mresults = rt.solve(rp);
} catch (Exception e) {
String message = "ex : " + e.toString();
Message msg = Message.obtain(); // Creates an new Message instance
msg.obj = message; // Put the string into Message, into "obj" field.
msg.setTarget(h); // Set the Handler
msg.sendToTarget(); //Send the message
}
}
}).start();
return true;
}
});
根据位置变化监听器获取当前位置的代码
/**
* If location changes, update our current location. If being found for
* the first time, zoom to our current position with a resolution of 20
*/
public void onLocationChanged(Location loc) {
if (loc == null)
return;
boolean zoomToMe = (mLocation == null) ? true : false;
mLocation = new Point(loc.getLongitude(), loc.getLatitude());
if (zoomToMe) {
Point p = (Point) GeometryEngine.project(mLocation, egs, wm);
map.zoomToResolution(p, 20.0);
}
}
运行此代码后,我遇到此错误:
com.esri.core.io.EsriSecurityException : 用于访问安全服务的令牌无效 - https://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World
【问题讨论】:
-
正如错误所说,可能您使用的令牌无效。
-
如何创建一个有效的token并使用它
-
阅读“请求令牌”部分:webhelp.esri.com/arcgisserver/9.3/java/…