我是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
答案 0 :(得分:0)
您正尝试在代码中设置标记值但未分配任何值,变量名称(CLIENT_SECRET和CLIENT_ID)表示使用OAuth2登录应用程序。
您有3个选项可用于访问路由服务,用户OAuth2登录,应用OAuth2登录或基于令牌的用户登录。首选方法是用户OAuth2登录,以便登录的用户将使用其ArcGIS Online命名用户帐户,并且所需的任何信用将来自其订阅。在开发人员门户网站上有一个用户基础OAuth2登录code sample。
如果您仍想使用基于令牌的登录,则需要设置UserCredential的用户名和密码,以便可以生成令牌或使用您正在调用的方法,但传入有效的令牌字符串。
如果您尚未阅读开发人员门户网站上的authentication documentation,我建议您阅读{{3}},因为它会对每个选项进行全面描述。