如何在android中获取示例路由arcgis?

时间:2016-07-22 07:05:19

标签: android arcgis

我想在两个地点之间找到路线,因为我找到了esri样本服务,即:http://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World。 但是,如果我使用此服务,我将收到错误,因为未经授权访问安全。 我无法使用此服务,请告诉我是否有任何免费服务获取arcgis地图上的路线 感谢。

我的代码:

public void getRouteFromSource(Geometry current_location,Geometry destination_point,boolean isCurrentLocation){
        routeLayer = new GraphicsLayer();
        mMapView.addLayer(routeLayer);

        // Initialize the RouteTask
        try {

        String routeTaskURL = "http://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";
            mRouteTask = RouteTask.createOnlineRouteTask(routeTaskURL, null);

        } catch (Exception e1) {
            e1.printStackTrace();                       
        }

    // Add the hidden segments layer (for highlighting route segments)
    hiddenSegmentsLayer = new GraphicsLayer();
    mMapView.addLayer(hiddenSegmentsLayer);     

    QueryDirections(current_location, destination_point,isCurrentLocation);
}
private void QueryDirections(final Geometry sourceGeometry, final Geometry destinationGeometry,boolean isCurrentLocation) {

    // Show that the route is calculating

    if(isCurrentLocation==false){
    dialog = ProgressDialog.show(mContext, PollingStationLocatorContant.plase_wait,
            "Calculating route...", true);
    }

//  Log.e("mLocation", "mLocation  "+sourceGeometry);
//  Log.e("POINTTT", "POINTTT"+p);
    // Spawn the request off in a new thread to keep UI responsive
    Thread t = new Thread() {
        private RouteResult mResults;

        @Override
        public void run() {
            try {
                // Start building up routing parameters

                /*Point startPoint = new Point(78.4867, 17.3850);
                Point stopPoint = new Point(79.5941, 17.9689);*/

        //      Log.e("mLocation.getX()",""+ p.getX()+"---"+ p.getY());
        //      Log.e("mLocation.getY()",""+ mLocation.getX() +"----"+ mLocation.getY());

                //Point startPoint = new Point(mLocation.getX(), mLocation.getY());
                //Point stopPoint = new Point(p.getX(), p.getY());

                StopGraphic point1 = new StopGraphic(sourceGeometry);
                StopGraphic point2 = new StopGraphic(destinationGeometry);                  

                Log.e("point1", ""+point1);
                Log.e("point2", ""+point2);
                NAFeaturesAsFeature rfaf = new NAFeaturesAsFeature();
                // Convert point to EGS (decimal degrees)
                // Create the stop points (start at our location, go
                // to pressed location)
                rfaf.setFeatures(new Graphic[] { point1, point2 });
                rfaf.setCompressedRequest(true);

        //  RouteParameters r = new RouteParameters();

                RouteParameters rp = mRouteTask.retrieveDefaultRouteTaskParameters();                   

                //rp.setImpedanceAttributeName("Length");
                rp.setReturnDirections(false);
                // Assign the first cost attribute as the impedance

                rp.setStops(rfaf);
                // Set the routing service output SR to our map
                // service's SR
                rp.setOutSpatialReference(mMapView.getSpatialReference());
                //rp.setImpedanceAttributeName("");

                // Solve the route and use the results to update UI
                // when received
                mResults = mRouteTask.solve(rp);                    

                List<Route> routes = mResults.getRoutes();
                Route mRoute = routes.get(0);

                Geometry routeGeom = mRoute.getRouteGraphic().getGeometry();
                Graphic symbolGraphic = new Graphic(routeGeom, new SimpleLineSymbol(Color.BLUE,5));
                //SimpleMarkerSymbol sls = new SimpleMarkerSymbol(Color.RED, 10,STYLE.CIRCLE);
                PictureMarkerSymbol pls=new PictureMarkerSymbol(mContext.getResources().getDrawable(R.drawable.animation_image));
                mMapView.setExtent(routeGeom, 20);
                Graphic destinatonGraphic = new Graphic(sourceGeometry, pls);
                mGraphicsLayer.addGraphic(symbolGraphic);
                mDestinationGraphicLayer.addGraphic(destinatonGraphic);
                mMapView.addLayer(mGraphicsLayer);
                mMapView.addLayer(mDestinationGraphicLayer);

            mHandler.post(mUpdateResults);
            } catch (Exception e) {
                mDestinationGraphicLayer.removeAll();
                noRouteFound=true;


                 e.printStackTrace();
                mHandler.post(mUpdateResults);

            }
        }
    };
    // Start the operation
    t.start();
}

void updateUI() {

    if(dialog!=null && dialog.isShowing()){
    dialog.dismiss();
    if(noRouteFound){

        Toast.makeText(mContext, "Unable to find route.Please select with in State", Toast.LENGTH_LONG).show();
    }
    }       
    }

2 个答案:

答案 0 :(得分:0)

无视地理编码服务(如果没有存储数据,可以免费调用),路由服务确实需要令牌。

documentation中所述:

  

必填参数令牌   使用此参数指定提供a标识的标记   具有访问服务权限的用户。访问   Esri提供的服务提供了有关此类服务的更多信息   可以获得访问令牌。

您可以做的是here并注册一个免费的开发者帐户。您将收到一个免费令牌及其相关数量的免费积分,您可以使用它来查询路由API。

但是,上面链接的文档显示了所有可能情况的响应样本(错误,路由正常,未找到路由)。

答案 1 :(得分:0)

创建免费开发者帐户后,请按照以下步骤操作。

在getRouteFromSource函数中,用此替换现有代码。

TOKEN = "The token you receive after you sign up";
CLIENT_ID = "The client_id you receive after you sign up";

try {
        UserCredentials authenticate= new UserCredentials();
        authenticate.setUserAccount("your username", "your password");
        authenticate.setUserToken(TOKEN, CLIENT_ID);
        mRouteTask = RouteTask
                .createOnlineRouteTask(
                        "http://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World",
                        authenticate);
    } catch (Exception e1) {
        e1.printStackTrace();
    }

这可以解决您的问题。